UNPKG

5.84 kBTypeScriptView Raw
1// Type definitions for easy-table
2// Project: https://github.com/eldargab/easy-table
3// Definitions by: Niklas Mollenhauer <https://github.com/nikeee>
4// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
5
6
7declare class EasyTable {
8
9 /**
10 * String to separate columns
11 */
12 public separator: string;
13
14 /**
15 * Default printer
16 */
17 public static string(value: any): string;
18
19 /**
20 * Create a printer which right aligns the content by padding with `ch` on the left
21 *
22 * @param {String} ch
23 * @returns {Function}
24 */
25 public static leftPadder<T>(ch: string): CellPrinter<T>;
26
27 public static padLeft: CellPrinter<string>;
28
29 /**
30 * Create a printer which pads with `ch` on the right
31 *
32 * @param {String} ch
33 * @returns {Function}
34 */
35 public static rightPadder<T>(ch: string): CellPrinter<T>;
36
37 // public static padRight: CellPrinter<string>;
38
39 /**
40 * Create a printer for numbers
41 *
42 * Will do right alignment and optionally fix the number of digits after decimal point
43 *
44 * @param {Number} [digits] - Number of digits for fixpoint notation
45 * @returns {Function}
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 * Write cell in the current row
60 *
61 * @param {String} col - Column name
62 * @param {Any} val - Cell value
63 * @param {Function} [printer] - Printer function to format the value
64 * @returns {Table} `this`
65 */
66 public cell<T>(col: string, val: T, printer?: CellPrinter<T>): this;
67
68 /**
69 * Get list of columns in printing order
70 *
71 * @returns {string[]}
72 */
73 public columns(): string[];
74
75 /**
76 * Format just rows, i.e. print the table without headers and totals
77 *
78 * @returns {String} String representaion of the table
79 */
80 public print(): string;
81
82 /**
83 * Format the table
84 *
85 * @returns {String}
86 */
87 public toString(): string;
88
89 /**
90 * Push delimeter row to the table (with each cell filled with dashs during printing)
91 *
92 * @param {String[]} [cols]
93 * @returns {Table} `this`
94 */
95 public pushDelimeter(cols?: ReadonlyArray<string>): this;
96
97 /**
98 * Compute all totals and yield the results to `cb`
99 *
100 * @param {Function} cb - Callback function with signature `(column, value, printer)`
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
160type CellPrinter<T> = (val: T, width: number) => string;
161type CompareFunction<T> = (a: T, b: T) => number;
162type ReduceFunction<T> = (acc: T, val: T, idx: number, length: number) => T;
163type FormatFunction<T> = (obj: T, cell: (name: string, val: any) => void) => void;
164type TablePostProcessing = (result: EasyTable) => string;
165
166interface PrintColumnOptions<T> {
167 /**
168 * Column separation string
169 */
170 separator?: string;
171 /**
172 * Printer to format column names
173 */
174 namePrinter?: CellPrinter<T>;
175}
176
177interface Aggregators {
178 /**
179 * Create a printer which formats the value with `printer`,
180 * adds the `prefix` to it and right aligns the whole thing
181 *
182 * @param {String} prefix
183 * @param {Function} printer
184 * @returns {printer}
185 */
186 printer<T>(prefix: string, printer: CellPrinter<T>): CellPrinter<T>;
187 /**
188 * Sum reduction
189 */
190 sum: any;
191 /**
192 * Average reduction
193 */
194 avg: any;
195}
196
197interface TotalOptions<T> {
198 /**
199 * reduce(acc, val, idx, length) function to compute the total value
200 */
201 reduce?: ReduceFunction<T>;
202 /**
203 * Printer to format the total cell
204 */
205 printer?: CellPrinter<T>;
206 /**
207 * Initial value for reduction
208 */
209 init?: T;
210}
211
212interface FormatObject {
213 [key: string]: ColumnFormat<any>;
214}
215
216interface ColumnFormat<T> {
217 name?: string;
218 printer?: CellPrinter<T>
219}
220
221export = EasyTable;