-
Notifications
You must be signed in to change notification settings - Fork 31
/
table.d.ts
218 lines (190 loc) · 5.67 KB
/
table.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// Type definitions for easy-table
// Definitions by: Niklas Mollenhauer <https://github.com/nikeee>
declare class EasyTable {
/**
* String to separate columns
*/
public separator: string;
/**
* Default printer
*/
public static string(value: any): string;
/**
* Create a printer which right aligns the content by padding with `ch` on the left
*
* @param {String} ch
* @returns {Function}
*/
public static leftPadder<T>(ch: string): CellPrinter<T>;
public static padLeft: CellPrinter<string>;
/**
* Create a printer which pads with `ch` on the right
*
* @param {String} ch
* @returns {Function}
*/
public static rightPadder<T>(ch: string): CellPrinter<T>;
// public static padRight: CellPrinter<string>;
/**
* Create a printer for numbers
*
* Will do right alignment and optionally fix the number of digits after decimal point
*
* @param {Number} [digits] - Number of digits for fixpoint notation
* @returns {Function}
*/
public static number(digits?: number): CellPrinter<number>;
public constructor();
/**
* Push the current row to the table and start a new one
*
* @returns {Table} `this`
*/
public newRow(): this;
/**
* Write cell in the current row
*
* @param {String} col - Column name
* @param {Any} val - Cell value
* @param {Function} [printer] - Printer function to format the value
* @returns {Table} `this`
*/
public cell<T>(col: string, val: T, printer?: CellPrinter<T>): this;
/**
* Get list of columns in printing order
*
* @returns {string[]}
*/
public columns(): string[];
/**
* Format just rows, i.e. print the table without headers and totals
*
* @returns {String} String representaion of the table
*/
public print(): string;
/**
* Format the table
*
* @returns {String}
*/
public toString(): string;
/**
* Push delimeter row to the table (with each cell filled with dashs during printing)
*
* @param {String[]} [cols]
* @returns {Table} `this`
*/
public pushDelimeter(cols?: ReadonlyArray<string>): this;
/**
* Compute all totals and yield the results to `cb`
*
* @param {Function} cb - Callback function with signature `(column, value, printer)`
*/
public forEachTotal<T>(cb: (column: string, value: T, printer: CellPrinter<T>) => void): void;
/**
* Format the table so that each row represents column and each column represents row
*
* @param {IPrintColumnOptions} [opts]
* @returns {String}
*/
public printTransposed<T>(opts?: PrintColumnOptions<T>): string;
/**
* Sort the table
*
* @param {Function|string[]} [cmp] - Either compare function or a list of columns to sort on
* @returns {Table} `this`
*/
public sort(cmp?: ReadonlyArray<string>): this;
/**
* Sort the table
*
* @param {Function|string[]} [cmp] - Either compare function or a list of columns to sort on
* @returns {Table} `this`
*/
public sort<T>(cmp?: CompareFunction<T>): this;
/**
* Add a total for the column
*
* @param {String} col - column name
* @param {Object} [opts]
* @returns {Table} `this`
*/
public total<T>(col: string, opts?: TotalOptions<T>): this;
/**
* Predefined helpers for totals
*/
public static aggr: Aggregators;
/**
* Print the array or object
*
* @param {Array|Object} obj - Object to print
* @param {Function|Object} [format] - Format options
* @param {Function} [cb] - Table post processing and formating
* @returns {String}
*/
public static print<T>(obj: T | T[], format?: FormatFunction<T> | FormatObject, cb?: TablePostProcessing): string;
/**
* Same as `Table.print()` but yields the result to `console.log()`
*/
public static log<T>(obj: T | T[], format?: FormatFunction<T> | FormatObject, cb?: TablePostProcessing): void;
/**
* Same as `.toString()` but yields the result to `console.log()`
*/
public log(): void;
}
type CellPrinter<T> = (val: T, width: number) => string;
type CompareFunction<T> = (a: T, b: T) => number;
type ReduceFunction<T> = (acc: T, val: T, idx: number, length: number) => T;
type FormatFunction<T> = (obj: T, cell: (name: string, val: any) => void) => void;
type TablePostProcessing = (result: EasyTable) => string;
interface PrintColumnOptions<T> {
/**
* Column separation string
*/
separator?: string | undefined;
/**
* Printer to format column names
*/
namePrinter?: CellPrinter<T> | undefined;
}
interface Aggregators {
/**
* Create a printer which formats the value with `printer`,
* adds the `prefix` to it and right aligns the whole thing
*
* @param {String} prefix
* @param {Function} printer
* @returns {printer}
*/
printer<T>(prefix: string, printer: CellPrinter<T>): CellPrinter<T>;
/**
* Sum reduction
*/
sum: any;
/**
* Average reduction
*/
avg: any;
}
interface TotalOptions<T> {
/**
* reduce(acc, val, idx, length) function to compute the total value
*/
reduce?: ReduceFunction<T> | undefined;
/**
* Printer to format the total cell
*/
printer?: CellPrinter<T> | undefined;
/**
* Initial value for reduction
*/
init?: T | undefined;
}
interface FormatObject {
[key: string]: ColumnFormat<any>;
}
interface ColumnFormat<T> {
name?: string | undefined;
printer?: CellPrinter<T> | undefined
}
export = EasyTable;