svg-table 0.0.1 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. package/CHANGES.md +5 -0
  2. package/README.md +447 -1
  3. package/{tableData.ts → TableData.ts} +2 -3
  4. package/TableFormatter.ts +587 -0
  5. package/{tableStyler.ts → TableStyler.ts} +75 -6
  6. package/dist/{tableData.d.ts → TableData.d.ts} +2 -2
  7. package/dist/{tableData.d.ts.map → TableData.d.ts.map} +1 -1
  8. package/dist/{tableData.js → TableData.js} +10 -10
  9. package/dist/{tableData.js.map → TableData.js.map} +1 -1
  10. package/dist/TableFormatter.d.ts +459 -0
  11. package/dist/TableFormatter.d.ts.map +1 -0
  12. package/dist/TableFormatter.js +579 -0
  13. package/dist/TableFormatter.js.map +1 -0
  14. package/dist/{tableStyler.d.ts → TableStyler.d.ts} +58 -3
  15. package/dist/TableStyler.d.ts.map +1 -0
  16. package/dist/{tableStyler.js → TableStyler.js} +90 -31
  17. package/dist/TableStyler.js.map +1 -0
  18. package/dist/index.d.ts +5 -5
  19. package/dist/index.d.ts.map +1 -1
  20. package/dist/index.js +12 -12
  21. package/dist/index.js.map +1 -1
  22. package/dist/tableData.test.js +15 -15
  23. package/dist/tableFormatter.test.js +54 -15
  24. package/dist/tableFormatter.test.js.map +1 -1
  25. package/dist/tableStyler.test.js +236 -20
  26. package/dist/tableStyler.test.js.map +1 -1
  27. package/dist/tableSvg.d.ts +2 -2
  28. package/dist/tableSvg.js +7 -7
  29. package/index.ts +13 -12
  30. package/package.json +2 -2
  31. package/svg-table-0.0.1.tgz +0 -0
  32. package/tableData.test.ts +1 -1
  33. package/tableFormatter.test.ts +48 -4
  34. package/tableStyler.test.ts +133 -10
  35. package/tableSvg.ts +3 -3
  36. package/dist/tableFormatter.d.ts +0 -179
  37. package/dist/tableFormatter.d.ts.map +0 -1
  38. package/dist/tableFormatter.js +0 -298
  39. package/dist/tableFormatter.js.map +0 -1
  40. package/dist/tableStyler.d.ts.map +0 -1
  41. package/dist/tableStyler.js.map +0 -1
  42. package/svg-table-0.0.1-snapshot.tgz +0 -0
  43. package/tableFormatter.ts +0 -306
@@ -0,0 +1,459 @@
1
+ import { DataFrame, type Tag, type TagCoordinate, type TagValue } from "data-frame-ts";
2
+ import { type Result } from "result-fn";
3
+ import { TableData } from "./TableData";
4
+ /**
5
+ * Type representing a formatter function
6
+ */
7
+ export type Formatter<D> = (value: D) => string;
8
+ /**
9
+ * Default formatter that converts a {@link value} of type `V` to a string
10
+ * @param value The value to convert
11
+ * @return a string representation of the {@link value}
12
+ */
13
+ export declare function defaultFormatter<D>(value: D): string;
14
+ export type Formatting<V> = {
15
+ formatter: Formatter<V>;
16
+ priority: number;
17
+ };
18
+ export declare function defaultFormatting<D>(): Formatting<D>;
19
+ export declare enum TableFormatterType {
20
+ CELL = "cell-formatter",
21
+ COLUMN = "column-formatter",
22
+ ROW = "row-formatter"
23
+ }
24
+ export declare function isFormattingTag(tag: Tag<TagValue, TagCoordinate>): boolean;
25
+ /**
26
+ * Represents a formatter used to apply row, column, and cell formatting to a data table.
27
+ * This class is used to define custom formatters for specific rows, columns, or cells,
28
+ * with support for priority-based selection of formatters.
29
+ */
30
+ export declare class TableFormatter<V> {
31
+ private readonly dataFrame;
32
+ /**
33
+ * @param dataFrame A data-frame that will be tagged with the row, column, and
34
+ * cell formatters.
35
+ * @private
36
+ */
37
+ private constructor();
38
+ /**
39
+ * Constructs a new {@link TableFormatter} from a {@link TableData} object. Underneath,
40
+ * this factory method merely unwraps the {@link DataFrame} from the {@link TableData}
41
+ * and hands it to the constructor.
42
+ * @param tableData The table data object from which to construct the table formatter
43
+ * @return A new {@link TableFormatter} based on the {@link TableData}
44
+ * @see TableFormatter.fromDataFrame
45
+ */
46
+ static fromTableData<V>(tableData: TableData<V>): TableFormatter<V>;
47
+ /**
48
+ * Constructs a new {@link TableFormatter} from a {@link DataFrame} object
49
+ * @param dataFrame The data-frame object from which to construct the table formatter
50
+ * @return A new {@link TableFormatter} based on the {@link DataFrame}
51
+ @see TableFormatter.fromTableData
52
+ */
53
+ static fromDataFrame<V>(dataFrame: DataFrame<V>): TableFormatter<V>;
54
+ /**
55
+ * Formatters convert the column value types to formatted strings. The formatter used to format a given
56
+ * cell depends on the priority of each formatter associated with that cell. The formatter with the
57
+ * highest priority is used. If two or more formatters for a given cell have the same priority, the selected
58
+ * formatter is indeterminant.
59
+ * @param columnIndex The index of the column to which to add the formatter
60
+ * @param formatter The formatter
61
+ * @param [priority = 0] The priority of this formatter. If cells have more than one associated formatter,
62
+ * the one with the highest priority number is used.
63
+ * @see addColumnFormatters
64
+ * @see addRowFormatter
65
+ * @see addRowFormatters
66
+ * @example
67
+ * ```typescript
68
+ * // create the data
69
+ * const data = DataFrame.from<string | number | Date>([
70
+ * [dateTimeFor(1, 1), 12345, 'gnm-f234', 123.45, 4],
71
+ * [dateTimeFor(2, 2), 23456, 'gnm-g234', 23.45, 5],
72
+ * [dateTimeFor(3, 3), 34567, 'gnm-h234', 3.65, 40],
73
+ * [dateTimeFor(4, 4), 45678, 'gnm-i234', 314.15, 9],
74
+ * ]).getOrThrow()
75
+ *
76
+ * // create the table-data object from the data, and then hand the table-data
77
+ * // to the table formatter, add column formats, and format the table, getting
78
+ * // back a new TableData<string>
79
+ * const tableData: TableData<string> = TableData.fromDataFrame<string | number | Date>(data)
80
+ * // from the table-data, create a table-formatter
81
+ * .flatMap(tableData => createTableFormatterFrom(tableData)
82
+ * // add a column formatter for the first column of dates
83
+ * .addColumnFormatter(0, value => (value as Date).toLocaleDateString())
84
+ * // add a column formatter to the second column of number
85
+ * .flatMap(tf => tf.addColumnFormatter(1, value => defaultFormatter(value)))
86
+ * // add a column formatter to the fourth column of currencies
87
+ * .flatMap(tf => tf.addColumnFormatter(3, value => `$ ${(value as number).toFixed(2)}`))
88
+ * .flatMap(tf => tf.addColumnFormatter(4, value => `${(value as number).toFixed(0)}`))
89
+ * // format the table into a new TableData<string> object
90
+ * .flatMap(tf => tf.formatTable())
91
+ * )
92
+ * .getOrThrow()
93
+ *
94
+ * // we expect the data-frame in the table data to be the following
95
+ * const expectedData = DataFrame.from<string>([
96
+ * ['2/1/2021', '12345', 'gnm-f234', '$ 123.45', '4'],
97
+ * ['2/2/2021', '23456', 'gnm-g234', '$ 23.45', '5'],
98
+ * ['2/3/2021', '34567', 'GNM-H234', '$ 3.65', '40'],
99
+ * ['2/4/2021', '45678', 'gnm-i234', '$ 314.15', '9'],
100
+ * ]).getOrThrow()
101
+ * ```
102
+ */
103
+ addColumnFormatter(columnIndex: number, formatter: Formatter<V>, priority?: number): Result<TableFormatter<V>, string>;
104
+ /**
105
+ * Adds formatters to specified column indexes in a table formatter.
106
+ *
107
+ * @param columnIndexes An array of column indexes to which the formatter will be applied.
108
+ * @param formatter The formatting function to apply to the specified columns.
109
+ * @param [priority = 0] The priority level for the formatter. Higher priority formatters are applied first.
110
+ * @return A result indicating the success or failure of adding the column formatters.
111
+ * @see addColumnFormatter
112
+ * @see addRowFormatter
113
+ * @see addRowFormatters
114
+ * @see addCellFormatter
115
+ * @see addCellFormatters
116
+ *
117
+ * @example
118
+ * ```typescript
119
+ * const data = DataFrame.from<string | number | Date>([
120
+ * [dateTimeFor(1, 1), 12345, 'gnm-f234', 123.45, 4],
121
+ * [dateTimeFor(2, 2), 23456, 'gnm-g234', 23.45, 5],
122
+ * [dateTimeFor(3, 3), 34567, 'gnm-h234', 3.65, 40],
123
+ * [dateTimeFor(4, 4), 45678, 'gnm-i234', 314.15, 9],
124
+ * ]).getOrThrow()
125
+ * const columnHeader = ['Date-Time', 'Customer ID', 'Product ID', 'Purchase Price', 'Amount']
126
+ * const rowHeader = [1, 2, 3, 4]
127
+ *
128
+ * const tableData = TableData.fromDataFrame<string | number | Date>(data)
129
+ * .withColumnHeader(columnHeader)
130
+ * .flatMap(td => td.withRowHeader(rowHeader))
131
+ * .flatMap(tableData => TableFormatter.fromTableData(tableData)
132
+ * // add the default formatter for the column header, at the highest priority so that
133
+ * // it is the one that applies to the row representing the column header
134
+ * .addRowFormatters([0], defaultFormatter, Infinity)
135
+ * // add the default formatter for the row header, at the highest priority so that
136
+ * // it is the one that applies to the column representing the row header
137
+ * .flatMap(tf => tf.addColumnFormatters([0], defaultFormatter, Infinity))
138
+ * // add the column formatters for each column at the default (lowest) priority
139
+ * // (notice that the columns are shifted by one for the columns because the row-header
140
+ * // occupies the first column (index=0))
141
+ * .flatMap(tf => tf.addColumnFormatter(1, value => (value as Date).toLocaleDateString()))
142
+ * .flatMap(tf => tf.addColumnFormatter(2, value => defaultFormatter(value)))
143
+ * .flatMap(tf => tf.addColumnFormatter(4, value => `$ ${(value as number).toFixed(2)}`))
144
+ * .flatMap(tf => tf.addColumnFormatter(5, value => `${(value as number).toFixed(0)}`))
145
+ * // format the table data and get back a TableData<string>
146
+ * .flatMap(tf => tf.formatTable())
147
+ * )
148
+ * .getOrThrow()
149
+ *
150
+ * const expectedData = DataFrame.from<string>([
151
+ * ['2/1/2021', '12345', 'gnm-f234', '$ 123.45', '4'],
152
+ * ['2/2/2021', '23456', 'gnm-g234', '$ 23.45', '5'],
153
+ * ['2/3/2021', '34567', 'gnm-h234', '$ 3.65', '40'],
154
+ * ['2/4/2021', '45678', 'gnm-i234', '$ 314.15', '9'],
155
+ * ]).getOrThrow()
156
+ * ```
157
+ */
158
+ addColumnFormatters(columnIndexes: Array<number>, formatter: Formatter<V>, priority?: number): Result<TableFormatter<V>, string>;
159
+ private static addColumnFormatters;
160
+ /**
161
+ * Formatters convert the row value types to formatted strings. The formatter used to format each cell in
162
+ * a given row depends on the priority of each formatter associated with that cell. The formatter with the
163
+ * highest priority is used. If two or more formatters for a given cell have the same priority, the selected
164
+ * formatter is indeterminant.
165
+ * @param rowIndex The index of the row to which to add the formatter
166
+ * @param formatter The formatter
167
+ * @param [priority = 0] The priority of this formatter. If cells have more than one associated formatter,
168
+ * the one with the highest priority number is used.
169
+ * @see addColumnFormatter
170
+ * @see addColumnFormatters
171
+ * @see addRowFormatters
172
+ * @example
173
+ * ```typescript
174
+ * // create the data
175
+ * const data = DataFrame.from<string | number | Date>([
176
+ * [dateTimeFor(1, 1), 12345, 'gnm-f234', 123.45, 4],
177
+ * [dateTimeFor(2, 2), 23456, 'gnm-g234', 23.45, 5],
178
+ * [dateTimeFor(3, 3), 34567, 'gnm-h234', 3.65, 40],
179
+ * [dateTimeFor(4, 4), 45678, 'gnm-i234', 314.15, 9],
180
+ * ]).getOrThrow()
181
+ *
182
+ * // create the table-data object from the data, and then hand the table-data
183
+ * // to the table formatter, add column formats, and format the table, getting
184
+ * // back a new TableData<string>
185
+ * const tableData: TableData<string> = TableData.fromDataFrame<string | number | Date>(data)
186
+ * // from the table-data, create a table-formatter
187
+ * .flatMap(tableData => createTableFormatterFrom(tableData)
188
+ * // add a column formatter for the first column of dates
189
+ * .addColumnFormatter(0, value => (value as Date).toLocaleDateString())
190
+ * // add a column formatter to the second column of number
191
+ * .flatMap(tf => tf.addColumnFormatter(1, value => defaultFormatter(value)))
192
+ * // add a column formatter to the fourth column of currencies
193
+ * .flatMap(tf => tf.addColumnFormatter(3, value => `$ ${(value as number).toFixed(2)}`))
194
+ * .flatMap(tf => tf.addColumnFormatter(4, value => `${(value as number).toFixed(0)}`))
195
+ * // format the table into a new TableData<string> object
196
+ * .flatMap(tf => tf.formatTable())
197
+ * )
198
+ * .getOrThrow()
199
+ *
200
+ * // we expect the data-frame in the table data to be the following
201
+ * const expectedData = DataFrame.from<string>([
202
+ * ['2/1/2021', '12345', 'gnm-f234', '$ 123.45', '4'],
203
+ * ['2/2/2021', '23456', 'gnm-g234', '$ 23.45', '5'],
204
+ * ['2/3/2021', '34567', 'GNM-H234', '$ 3.65', '40'],
205
+ * ['2/4/2021', '45678', 'gnm-i234', '$ 314.15', '9'],
206
+ * ]).getOrThrow()
207
+ * ```
208
+ */
209
+ addRowFormatter(rowIndex: number, formatter: Formatter<V>, priority?: number): Result<TableFormatter<V>, string>;
210
+ /**
211
+ * Adds formatters to specified row indexes in a table formatter.
212
+ *
213
+ * @param rowIndexes An array of row indexes to which the formatter will be applied.
214
+ * @param formatter The formatting function to apply to the specified columns.
215
+ * @param [priority = 0] The priority level for the formatter. Higher priority formatters are applied first.
216
+ * @return A result indicating the success or failure of adding the row formatters.
217
+ * @see addColumnFormatter
218
+ * @see addRowFormatter
219
+ * @see addRowFormatters
220
+ * @see addCellFormatter
221
+ * @see addCellFormatters
222
+ * @example
223
+ * ```typescript
224
+ * const data = DataFrame.from<string | number | Date>([
225
+ * [dateTimeFor(1, 1), 12345, 'gnm-f234', 123.45, 4],
226
+ * [dateTimeFor(2, 2), 23456, 'gnm-g234', 23.45, 5],
227
+ * [dateTimeFor(3, 3), 34567, 'gnm-h234', 3.65, 40],
228
+ * [dateTimeFor(4, 4), 45678, 'gnm-i234', 314.15, 9],
229
+ * ]).getOrThrow()
230
+ * const columnHeader = ['Date-Time', 'Customer ID', 'Product ID', 'Purchase Price', 'Amount']
231
+ * const rowHeader = [1, 2, 3, 4]
232
+ *
233
+ * const tableData = TableData.fromDataFrame<string | number | Date>(data)
234
+ * .withColumnHeader(columnHeader)
235
+ * .flatMap(td => td.withRowHeader(rowHeader))
236
+ * .flatMap(tableData => TableFormatter.fromTableData(tableData)
237
+ * // add the default formatter for the column header, at the highest priority so that
238
+ * // it is the one that applies to the row representing the column header
239
+ * .addRowFormatters([0], defaultFormatter, Infinity)
240
+ * // add the default formatter for the row header, at the highest priority so that
241
+ * // it is the one that applies to the column representing the row header
242
+ * .flatMap(tf => tf.addColumnFormatters([0], defaultFormatter, Infinity))
243
+ * // add the column formatters for each column at the default (lowest) priority
244
+ * // (notice that the columns are shifted by one for the columns because the row-header
245
+ * // occupies the first column (index=0))
246
+ * .flatMap(tf => tf.addColumnFormatter(1, value => (value as Date).toLocaleDateString()))
247
+ * .flatMap(tf => tf.addColumnFormatter(2, value => defaultFormatter(value)))
248
+ * .flatMap(tf => tf.addColumnFormatter(4, value => `$ ${(value as number).toFixed(2)}`))
249
+ * .flatMap(tf => tf.addColumnFormatter(5, value => `${(value as number).toFixed(0)}`))
250
+ * // format the table data and get back a TableData<string>
251
+ * .flatMap(tf => tf.formatTable())
252
+ * )
253
+ * .getOrThrow()
254
+ *
255
+ * const expectedData = DataFrame.from<string>([
256
+ * ['2/1/2021', '12345', 'gnm-f234', '$ 123.45', '4'],
257
+ * ['2/2/2021', '23456', 'gnm-g234', '$ 23.45', '5'],
258
+ * ['2/3/2021', '34567', 'gnm-h234', '$ 3.65', '40'],
259
+ * ['2/4/2021', '45678', 'gnm-i234', '$ 314.15', '9'],
260
+ * ]).getOrThrow()
261
+ * ```
262
+ */
263
+ addRowFormatters(rowIndexes: Array<number>, formatter: Formatter<V>, priority?: number): Result<TableFormatter<V>, string>;
264
+ private static addRowFormatters;
265
+ /**
266
+ * Adds a formatter to a specified cell based on the row and column indexes.
267
+ * @param rowIndex The row index of the cell.
268
+ * @param columnIndex The column index of the cell.
269
+ * @param formatter The formatter to apply to the cell.
270
+ * @param [priority = 0] The priority level for the formatter. Higher priority formatters are applied first.
271
+ * @return A result indicating the success or failure of adding the cell formatter.
272
+ * @see addColumnFormatter
273
+ * @see addRowFormatter
274
+ * @see addRowFormatters
275
+ * @see addCellFormatters
276
+ * @see addCellFormatters
277
+ * @example
278
+ * ```typescript
279
+ * const data = DataFrame.from<string | number | Date>([
280
+ * [dateTimeFor(1, 1), 12345, 'gnm-f234', 123.45, 4],
281
+ * [dateTimeFor(2, 2), 23456, 'gnm-g234', 23.45, 5],
282
+ * [dateTimeFor(3, 3), 34567, 'gnm-h234', 3.65, 40],
283
+ * [dateTimeFor(4, 4), 45678, 'gnm-i234', 314.15, 9],
284
+ * ]).getOrThrow()
285
+ * const columnHeader = ['Date-Time', 'Customer ID', 'Product ID', 'Purchase Price', 'Amount']
286
+ * const rowHeader = [1, 2, 3, 4]
287
+ *
288
+ * const tableData = TableData.fromDataFrame<string | number | Date>(data)
289
+ * .withColumnHeader(columnHeader)
290
+ * .flatMap(td => td.withRowHeader(rowHeader))
291
+ * .flatMap(tableData => TableFormatter.fromTableData(tableData)
292
+ * // add the default formatter for the column header, at the highest priority so that
293
+ * // it is the one that applies to the row representing the column header
294
+ * .addRowFormatters([0], defaultFormatter, Infinity)
295
+ * // add the default formatter for the row header, at the highest priority so that
296
+ * // it is the one that applies to the column representing the row header
297
+ * .flatMap(tf => tf.addColumnFormatters([0], defaultFormatter, Infinity))
298
+ * // add the column formatters for each column at the default (lowest) priority
299
+ * // (notice that the columns are shifted by one for the columns because the row-header
300
+ * // occupies the first column (index=0))
301
+ * .flatMap(tf => tf.addColumnFormatter(1, value => (value as Date).toLocaleDateString()))
302
+ * .flatMap(tf => tf.addColumnFormatter(2, value => defaultFormatter(value)))
303
+ * .flatMap(tf => tf.addColumnFormatter(4, value => `$ ${(value as number).toFixed(2)}`))
304
+ * .flatMap(tf => tf.addColumnFormatter(5, value => `${(value as number).toFixed(0)}`))
305
+ * // override the cell formatter for a select set of cells
306
+ * .flatMap(tf => tf.addCellFormatter(1, 4, value => `${(value as number).toFixed(2)}`, 1000))
307
+ * .flatMap(tf => tf.addCellFormatters([[3, 5], [4, 5]], value => `${((value as number) * 10).toFixed(0)}`, 1000))
308
+ * // format the table data and get back a TableData<string>
309
+ * .flatMap(tf => tf.formatTable())
310
+ * )
311
+ * .getOrThrow()
312
+ *
313
+ * const expectedData = DataFrame.from<string>([
314
+ * ['2/1/2021', '12345', 'gnm-f234', '123.45', '4'], // overwrite (1, 4) to remove "$"
315
+ * ['2/2/2021', '23456', 'gnm-g234', '$ 23.45', '5'],
316
+ * ['2/3/2021', '34567', 'gnm-h234', '$ 3.65', '400'], // overwrite (3, 5) to multiply by 10
317
+ * ['2/4/2021', '45678', 'gnm-i234', '$ 314.15', '90'], // overwrite (4, 5) to multiply by 10
318
+ * ]).getOrThrow()
319
+ * ```
320
+ */
321
+ addCellFormatter(rowIndex: number, columnIndex: number, formatter: Formatter<V>, priority?: number): Result<TableFormatter<V>, string>;
322
+ /**
323
+ * Adds formatters to specified cell indexes in a table formatter.
324
+ * @param cellIndexes An array of cell indexes to which the formatter will be applied.
325
+ * @param formatter The formatting function to apply to the specified cells.
326
+ * @param [priority = 0] The priority level for the formatter. Higher priority formatters are applied first.
327
+ * @return A result indicating the success or failure of adding the cell formatters.
328
+ * @see addColumnFormatter
329
+ * @see addRowFormatter
330
+ * @see addRowFormatters
331
+ * @see addCellFormatters
332
+ * @see addCellFormatters
333
+ * @example
334
+ * ```typescript
335
+ * const data = DataFrame.from<string | number | Date>([
336
+ * [dateTimeFor(1, 1), 12345, 'gnm-f234', 123.45, 4],
337
+ * [dateTimeFor(2, 2), 23456, 'gnm-g234', 23.45, 5],
338
+ * [dateTimeFor(3, 3), 34567, 'gnm-h234', 3.65, 40],
339
+ * [dateTimeFor(4, 4), 45678, 'gnm-i234', 314.15, 9],
340
+ * ]).getOrThrow()
341
+ * const columnHeader = ['Date-Time', 'Customer ID', 'Product ID', 'Purchase Price', 'Amount']
342
+ * const rowHeader = [1, 2, 3, 4]
343
+ *
344
+ * const tableData = TableData.fromDataFrame<string | number | Date>(data)
345
+ * .withColumnHeader(columnHeader)
346
+ * .flatMap(td => td.withRowHeader(rowHeader))
347
+ * .flatMap(tableData => TableFormatter.fromTableData(tableData)
348
+ * // add the default formatter for the column header, at the highest priority so that
349
+ * // it is the one that applies to the row representing the column header
350
+ * .addRowFormatters([0], defaultFormatter, Infinity)
351
+ * // add the default formatter for the row header, at the highest priority so that
352
+ * // it is the one that applies to the column representing the row header
353
+ * .flatMap(tf => tf.addColumnFormatters([0], defaultFormatter, Infinity))
354
+ * // add the column formatters for each column at the default (lowest) priority
355
+ * // (notice that the columns are shifted by one for the columns because the row-header
356
+ * // occupies the first column (index=0))
357
+ * .flatMap(tf => tf.addColumnFormatter(1, value => (value as Date).toLocaleDateString()))
358
+ * .flatMap(tf => tf.addColumnFormatter(2, value => defaultFormatter(value)))
359
+ * .flatMap(tf => tf.addColumnFormatter(4, value => `$ ${(value as number).toFixed(2)}`))
360
+ * .flatMap(tf => tf.addColumnFormatter(5, value => `${(value as number).toFixed(0)}`))
361
+ * // override the cell formatter for a select set of cells
362
+ * .flatMap(tf => tf.addCellFormatter(1, 4, value => `${(value as number).toFixed(2)}`, 1000))
363
+ * .flatMap(tf => tf.addCellFormatters([[3, 5], [4, 5]], value => `${((value as number) * 10).toFixed(0)}`, 1000))
364
+ * // format the table data and get back a TableData<string>
365
+ * .flatMap(tf => tf.formatTable())
366
+ * )
367
+ * .getOrThrow()
368
+ *
369
+ * const expectedData = DataFrame.from<string>([
370
+ * ['2/1/2021', '12345', 'gnm-f234', '123.45', '4'], // overwrite (1, 4) to remove "$"
371
+ * ['2/2/2021', '23456', 'gnm-g234', '$ 23.45', '5'],
372
+ * ['2/3/2021', '34567', 'gnm-h234', '$ 3.65', '400'], // overwrite (3, 5) to multiply by 10
373
+ * ['2/4/2021', '45678', 'gnm-i234', '$ 314.15', '90'], // overwrite (4, 5) to multiply by 10
374
+ * ]).getOrThrow()
375
+ * ```
376
+ */
377
+ addCellFormatters(cellIndexes: Array<[x: number, y: number]>, formatter: Formatter<V>, priority?: number): Result<TableFormatter<V>, string>;
378
+ private static addCellFormatters;
379
+ /**
380
+ * Formats the table headers, footer, and values using the formatters that have been added
381
+ * to this `TableData<V>` object and returns a new `TableData<string>` object where all the
382
+ * elements have been converted to a formatted string.
383
+ * @return a new `TableData<string>` object where all the elements have been converted to a
384
+ * formatted string.
385
+ * @example
386
+ * ```typescript
387
+ * function dateTimeFor(day: number, hour: number): Date {
388
+ * return new Date(2021, 1, day, hour, 0, 0, 0);
389
+ * }
390
+ *
391
+ * // the headers for the table
392
+ * const columnHeader = ['Date-Time', 'Customer ID', 'Product ID', 'Purchase Price', 'Amount']
393
+ * const rowHeader = [1, 2, 3, 4]
394
+ *
395
+ * // this is the actual data used to creat the data table (in row-form)
396
+ * const data = DataFrame.from<string | number | Date>([
397
+ * [dateTimeFor(1, 1), 12345, 'gnm-f234', 123.45, 4],
398
+ * [dateTimeFor(2, 2), 23456, 'gnm-g234', 23.45, 5],
399
+ * [dateTimeFor(3, 3), 34567, 'gnm-h234', 3.65, 40],
400
+ * [dateTimeFor(4, 4), 45678, 'gnm-i234', 314.15, 9],
401
+ * ]).getOrThrow()
402
+ *
403
+ * // this is what we expect that formatted data to look like
404
+ * const expectedData = DataFrame.from<string>([
405
+ * ['2/1/2021', '12345', 'gnm-f234', '$ 123.45', '4'],
406
+ * ['2/2/2021', '23456', 'gnm-g234', '$ 23.45', '5'],
407
+ * ['2/3/2021', '34567', 'gnm-h234', '$ 3.65', '40'],
408
+ * ['2/4/2021', '45678', 'gnm-i234', '$ 314.15', '9'],
409
+ * ]).getOrThrow()
410
+ *
411
+ * // 1. create a data-table that has a column-header and a row-header and mixed-type
412
+ * // data (number, string, Date)
413
+ * // 2. add formatters for the row and column headers (at highest priority)
414
+ * // 3. add formatters for some of the other data columns
415
+ * // 4. format the table
416
+ * const tableData = TableData.fromDataFrame<string | number | Date>(data)
417
+ * .withColumnHeader(columnHeader)
418
+ * .flatMap(table => table.withRowHeader(rowHeader))
419
+ * // add the default formatter for the column header, at the highest priority so that
420
+ * // it is the one that applies to the row representing the column header
421
+ * .flatMap(td => td.addRowFormatter(0, defaultFormatter, Infinity))
422
+ * // add the default formatter for the row header, at the highest priority so that
423
+ * // it is the one that applies to the column representing the row header
424
+ * .flatMap(td => td.addColumnFormatter(0, defaultFormatter, Infinity))
425
+ * // add the column formatters for each column at the default (lowest) priority
426
+ * // (notice that the columns are shifted by one for the columns because the row-header
427
+ * // occupies the first column (index=0))
428
+ * .flatMap(td => td.addColumnFormatter(1, value => (value as Date).toLocaleDateString()))
429
+ * .flatMap(td => td.addColumnFormatter(2, value => defaultFormatter(value)))
430
+ * .flatMap(td => td.addColumnFormatter(4, value => `$ ${(value as number).toFixed(2)}`))
431
+ * .flatMap(td => td.addColumnFormatter(5, value => `${(value as number).toFixed(0)}`))
432
+ * // format the table data and get back a TableData<string>
433
+ * .map(td => td.formatTable())
434
+ * .getOrThrow()
435
+ *
436
+ * // the column header of the formatted table should be the same as the one specified
437
+ * expect(tableData.columnHeader().getOrThrow()).toEqual(columnHeader)
438
+ *
439
+ * // the row header of the formatted table should be the same as the one specified
440
+ * expect(tableData.rowHeader().getOrThrow()).toEqual(rowHeader.map(hdr => defaultFormatter(hdr)))
441
+ *
442
+ * // the data should be equal to the expected data
443
+ * expect(tableData.data().map(df => df.equals(expectedData)).getOrThrow()).toBeTruthy()
444
+ * ```
445
+ */
446
+ formatTable<C extends TagCoordinate>(): Result<TableData<string>, string>;
447
+ /**
448
+ * Generally, the {@link TableFormatter} formats the {@link DataFrame} into a `TableData<string>`
449
+ * where all the elements of the {@link TableData} are a string. Because a formatted table does
450
+ * not necessarily have to be a `TableData<string>` (which would contain a `DataFrame<string>`,
451
+ * this method allows mapping a {@link DataFrame} into any desired type.
452
+ * @param mapper A function that takes a `DataFrame<string>` and returns a desired type (`D`).
453
+ * @return A `Result<D, string>` where the `Result<D, string>` is either success, containing the
454
+ * desired type (`D`), or failure, containing an error message.
455
+ * @see formatTable
456
+ */
457
+ formatTableInto<C extends TagCoordinate, D = TableData<string>>(mapper: (dataFrame: DataFrame<string>) => D): Result<D, string>;
458
+ }
459
+ //# sourceMappingURL=TableFormatter.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"TableFormatter.d.ts","sourceRoot":"","sources":["../TableFormatter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,SAAS,EAAE,KAAK,GAAG,EAAE,KAAK,aAAa,EAAE,KAAK,QAAQ,EAAC,MAAM,eAAe,CAAC;AACrF,OAAO,EAAgB,KAAK,MAAM,EAAgB,MAAM,WAAW,CAAC;AACpE,OAAO,EAAC,SAAS,EAAC,MAAM,aAAa,CAAC;AAEtC;;GAEG;AACH,MAAM,MAAM,SAAS,CAAC,CAAC,IAAI,CAAC,KAAK,EAAE,CAAC,KAAK,MAAM,CAAA;AAE/C;;;;GAIG;AACH,wBAAgB,gBAA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