1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 | declare class EasyTable {
|
8 |
|
9 | |
10 |
|
11 |
|
12 | public separator: string;
|
13 |
|
14 | |
15 |
|
16 |
|
17 | public static string(value: any): string;
|
18 |
|
19 | |
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 | public static leftPadder<T>(ch: string): CellPrinter<T>;
|
26 |
|
27 | public static padLeft: CellPrinter<string>;
|
28 |
|
29 | |
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 | public static rightPadder<T>(ch: string): CellPrinter<T>;
|
36 |
|
37 |
|
38 |
|
39 | |
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 | public static number(digits?: number): CellPrinter<number>;
|
48 |
|
49 | public constructor();
|
50 |
|
51 | /**
|
52 | * Push the current row to the table and start a new one
|
53 | *
|
54 | * @returns {Table} `this`
|
55 | */
|
56 | public newRow(): this;
|
57 |
|
58 | |
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 | public cell<T>(col: string, val: T, printer?: CellPrinter<T>): this;
|
67 |
|
68 | |
69 |
|
70 |
|
71 |
|
72 |
|
73 | public columns(): string[];
|
74 |
|
75 | |
76 |
|
77 |
|
78 |
|
79 |
|
80 | public print(): string;
|
81 |
|
82 | |
83 |
|
84 |
|
85 |
|
86 |
|
87 | public toString(): string;
|
88 |
|
89 | |
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 | public pushDelimeter(cols?: ReadonlyArray<string>): this;
|
96 |
|
97 | |
98 |
|
99 |
|
100 |
|
101 |
|
102 | public forEachTotal<T>(cb: (column: string, value: T, printer: CellPrinter<T>) => void): void;
|
103 |
|
104 | /**
|
105 | * Format the table so that each row represents column and each column represents row
|
106 | *
|
107 | * @param {IPrintColumnOptions} [opts]
|
108 | * @returns {String}
|
109 | */
|
110 | public printTransposed<T>(opts?: PrintColumnOptions<T>): string;
|
111 |
|
112 | /**
|
113 | * Sort the table
|
114 | *
|
115 | * @param {Function|string[]} [cmp] - Either compare function or a list of columns to sort on
|
116 | * @returns {Table} `this`
|
117 | */
|
118 | public sort(cmp?: ReadonlyArray<string>): this;
|
119 | /**
|
120 | * Sort the table
|
121 | *
|
122 | * @param {Function|string[]} [cmp] - Either compare function or a list of columns to sort on
|
123 | * @returns {Table} `this`
|
124 | */
|
125 | public sort<T>(cmp?: CompareFunction<T>): this;
|
126 |
|
127 | /**
|
128 | * Add a total for the column
|
129 | *
|
130 | * @param {String} col - column name
|
131 | * @param {Object} [opts]
|
132 | * @returns {Table} `this`
|
133 | */
|
134 | public total<T>(col: string, opts?: TotalOptions<T>): this;
|
135 | /**
|
136 | * Predefined helpers for totals
|
137 | */
|
138 | public static aggr: Aggregators;
|
139 |
|
140 | /**
|
141 | * Print the array or object
|
142 | *
|
143 | * @param {Array|Object} obj - Object to print
|
144 | * @param {Function|Object} [format] - Format options
|
145 | * @param {Function} [cb] - Table post processing and formating
|
146 | * @returns {String}
|
147 | */
|
148 | public static print<T>(obj: T | T[], format?: FormatFunction<T> | FormatObject, cb?: TablePostProcessing): string;
|
149 |
|
150 | /**
|
151 | * Same as `Table.print()` but yields the result to `console.log()`
|
152 | */
|
153 | public static log<T>(obj: T | T[], format?: FormatFunction<T> | FormatObject, cb?: TablePostProcessing): void;
|
154 | /**
|
155 | * Same as `.toString()` but yields the result to `console.log()`
|
156 | */
|
157 | public log(): void;
|
158 | }
|
159 |
|
160 | type CellPrinter<T> = (val: T, width: number) => string;
|
161 | type CompareFunction<T> = (a: T, b: T) => number;
|
162 | type ReduceFunction<T> = (acc: T, val: T, idx: number, length: number) => T;
|
163 | type FormatFunction<T> = (obj: T, cell: (name: string, val: any) => void) => void;
|
164 | type TablePostProcessing = (result: EasyTable) => string;
|
165 |
|
166 | interface PrintColumnOptions<T> {
|
167 | |
168 |
|
169 |
|
170 | separator?: string;
|
171 | |
172 |
|
173 |
|
174 | namePrinter?: CellPrinter<T>;
|
175 | }
|
176 |
|
177 | interface Aggregators {
|
178 | |
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 | printer<T>(prefix: string, printer: CellPrinter<T>): CellPrinter<T>;
|
187 | |
188 |
|
189 |
|
190 | sum: any;
|
191 | |
192 |
|
193 |
|
194 | avg: any;
|
195 | }
|
196 |
|
197 | interface TotalOptions<T> {
|
198 | |
199 |
|
200 |
|
201 | reduce?: ReduceFunction<T>;
|
202 | |
203 |
|
204 |
|
205 | printer?: CellPrinter<T>;
|
206 | |
207 |
|
208 |
|
209 | init?: T;
|
210 | }
|
211 |
|
212 | interface FormatObject {
|
213 | [key: string]: ColumnFormat<any>;
|
214 | }
|
215 |
|
216 | interface ColumnFormat<T> {
|
217 | name?: string;
|
218 | printer?: CellPrinter<T>
|
219 | }
|
220 |
|
221 | export = EasyTable;
|