svg-table 0.0.2 → 0.1.1
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.
- package/CHANGES.md +9 -0
- package/README.md +447 -1
- package/TableFormatter.ts +286 -5
- package/TableStyler.ts +74 -5
- package/dist/TableFormatter.d.ts +280 -0
- package/dist/TableFormatter.d.ts.map +1 -1
- package/dist/TableFormatter.js +286 -5
- package/dist/TableFormatter.js.map +1 -1
- package/dist/TableStyler.d.ts +56 -1
- package/dist/TableStyler.d.ts.map +1 -1
- package/dist/TableStyler.js +61 -2
- package/dist/TableStyler.js.map +1 -1
- package/dist/tableFormatter.test.js +41 -2
- package/dist/tableFormatter.test.js.map +1 -1
- package/dist/tableStyler.test.js +222 -6
- package/dist/tableStyler.test.js.map +1 -1
- package/dist/tableSvg.js +4 -2
- package/dist/tableSvg.js.map +1 -1
- package/package.json +1 -1
- package/tableFormatter.test.ts +46 -2
- package/tableStyler.test.ts +130 -7
- package/tableSvg.ts +4 -2
package/TableFormatter.ts
CHANGED
|
@@ -88,6 +88,9 @@ export class TableFormatter<V> {
|
|
|
88
88
|
* @param formatter The formatter
|
|
89
89
|
* @param [priority = 0] The priority of this formatter. If cells have more than one associated formatter,
|
|
90
90
|
* the one with the highest priority number is used.
|
|
91
|
+
* @see addColumnFormatters
|
|
92
|
+
* @see addRowFormatter
|
|
93
|
+
* @see addRowFormatters
|
|
91
94
|
* @example
|
|
92
95
|
* ```typescript
|
|
93
96
|
* // create the data
|
|
@@ -131,8 +134,62 @@ export class TableFormatter<V> {
|
|
|
131
134
|
.map(data => new TableFormatter<V>(data))
|
|
132
135
|
}
|
|
133
136
|
|
|
137
|
+
/**
|
|
138
|
+
* Adds formatters to specified column indexes in a table formatter.
|
|
139
|
+
*
|
|
140
|
+
* @param columnIndexes An array of column indexes to which the formatter will be applied.
|
|
141
|
+
* @param formatter The formatting function to apply to the specified columns.
|
|
142
|
+
* @param [priority = 0] The priority level for the formatter. Higher priority formatters are applied first.
|
|
143
|
+
* @return A result indicating the success or failure of adding the column formatters.
|
|
144
|
+
* @see addColumnFormatter
|
|
145
|
+
* @see addRowFormatter
|
|
146
|
+
* @see addRowFormatters
|
|
147
|
+
* @see addCellFormatter
|
|
148
|
+
* @see addCellFormatters
|
|
149
|
+
*
|
|
150
|
+
* @example
|
|
151
|
+
* ```typescript
|
|
152
|
+
* const data = DataFrame.from<string | number | Date>([
|
|
153
|
+
* [dateTimeFor(1, 1), 12345, 'gnm-f234', 123.45, 4],
|
|
154
|
+
* [dateTimeFor(2, 2), 23456, 'gnm-g234', 23.45, 5],
|
|
155
|
+
* [dateTimeFor(3, 3), 34567, 'gnm-h234', 3.65, 40],
|
|
156
|
+
* [dateTimeFor(4, 4), 45678, 'gnm-i234', 314.15, 9],
|
|
157
|
+
* ]).getOrThrow()
|
|
158
|
+
* const columnHeader = ['Date-Time', 'Customer ID', 'Product ID', 'Purchase Price', 'Amount']
|
|
159
|
+
* const rowHeader = [1, 2, 3, 4]
|
|
160
|
+
*
|
|
161
|
+
* const tableData = TableData.fromDataFrame<string | number | Date>(data)
|
|
162
|
+
* .withColumnHeader(columnHeader)
|
|
163
|
+
* .flatMap(td => td.withRowHeader(rowHeader))
|
|
164
|
+
* .flatMap(tableData => TableFormatter.fromTableData(tableData)
|
|
165
|
+
* // add the default formatter for the column header, at the highest priority so that
|
|
166
|
+
* // it is the one that applies to the row representing the column header
|
|
167
|
+
* .addRowFormatters([0], defaultFormatter, Infinity)
|
|
168
|
+
* // add the default formatter for the row header, at the highest priority so that
|
|
169
|
+
* // it is the one that applies to the column representing the row header
|
|
170
|
+
* .flatMap(tf => tf.addColumnFormatters([0], defaultFormatter, Infinity))
|
|
171
|
+
* // add the column formatters for each column at the default (lowest) priority
|
|
172
|
+
* // (notice that the columns are shifted by one for the columns because the row-header
|
|
173
|
+
* // occupies the first column (index=0))
|
|
174
|
+
* .flatMap(tf => tf.addColumnFormatter(1, value => (value as Date).toLocaleDateString()))
|
|
175
|
+
* .flatMap(tf => tf.addColumnFormatter(2, value => defaultFormatter(value)))
|
|
176
|
+
* .flatMap(tf => tf.addColumnFormatter(4, value => `$ ${(value as number).toFixed(2)}`))
|
|
177
|
+
* .flatMap(tf => tf.addColumnFormatter(5, value => `${(value as number).toFixed(0)}`))
|
|
178
|
+
* // format the table data and get back a TableData<string>
|
|
179
|
+
* .flatMap(tf => tf.formatTable())
|
|
180
|
+
* )
|
|
181
|
+
* .getOrThrow()
|
|
182
|
+
*
|
|
183
|
+
* const expectedData = DataFrame.from<string>([
|
|
184
|
+
* ['2/1/2021', '12345', 'gnm-f234', '$ 123.45', '4'],
|
|
185
|
+
* ['2/2/2021', '23456', 'gnm-g234', '$ 23.45', '5'],
|
|
186
|
+
* ['2/3/2021', '34567', 'gnm-h234', '$ 3.65', '40'],
|
|
187
|
+
* ['2/4/2021', '45678', 'gnm-i234', '$ 314.15', '9'],
|
|
188
|
+
* ]).getOrThrow()
|
|
189
|
+
* ```
|
|
190
|
+
*/
|
|
134
191
|
addColumnFormatters(columnIndexes: Array<number>, formatter: Formatter<V>, priority: number = 0): Result<TableFormatter<V>, string> {
|
|
135
|
-
return TableFormatter.addColumnFormatters(this, columnIndexes, formatter, priority)
|
|
192
|
+
return TableFormatter.addColumnFormatters(this, columnIndexes.slice(), formatter, priority)
|
|
136
193
|
}
|
|
137
194
|
|
|
138
195
|
private static addColumnFormatters<V>(
|
|
@@ -142,7 +199,7 @@ export class TableFormatter<V> {
|
|
|
142
199
|
priority: number = 0
|
|
143
200
|
): Result<TableFormatter<V>, string> {
|
|
144
201
|
if (columnIndexes.length > 0) {
|
|
145
|
-
const columnIndex = columnIndexes.
|
|
202
|
+
const columnIndex = columnIndexes.shift()
|
|
146
203
|
if (columnIndex != null) {
|
|
147
204
|
return tableFormatter
|
|
148
205
|
.addColumnFormatter(columnIndex, formatter, priority)
|
|
@@ -152,12 +209,114 @@ export class TableFormatter<V> {
|
|
|
152
209
|
return successResult(tableFormatter)
|
|
153
210
|
}
|
|
154
211
|
|
|
212
|
+
/**
|
|
213
|
+
* Formatters convert the row value types to formatted strings. The formatter used to format each cell in
|
|
214
|
+
* a given row depends on the priority of each formatter associated with that cell. The formatter with the
|
|
215
|
+
* highest priority is used. If two or more formatters for a given cell have the same priority, the selected
|
|
216
|
+
* formatter is indeterminant.
|
|
217
|
+
* @param rowIndex The index of the row to which to add the formatter
|
|
218
|
+
* @param formatter The formatter
|
|
219
|
+
* @param [priority = 0] The priority of this formatter. If cells have more than one associated formatter,
|
|
220
|
+
* the one with the highest priority number is used.
|
|
221
|
+
* @see addColumnFormatter
|
|
222
|
+
* @see addColumnFormatters
|
|
223
|
+
* @see addRowFormatters
|
|
224
|
+
* @example
|
|
225
|
+
* ```typescript
|
|
226
|
+
* // create the data
|
|
227
|
+
* const data = DataFrame.from<string | number | Date>([
|
|
228
|
+
* [dateTimeFor(1, 1), 12345, 'gnm-f234', 123.45, 4],
|
|
229
|
+
* [dateTimeFor(2, 2), 23456, 'gnm-g234', 23.45, 5],
|
|
230
|
+
* [dateTimeFor(3, 3), 34567, 'gnm-h234', 3.65, 40],
|
|
231
|
+
* [dateTimeFor(4, 4), 45678, 'gnm-i234', 314.15, 9],
|
|
232
|
+
* ]).getOrThrow()
|
|
233
|
+
*
|
|
234
|
+
* // create the table-data object from the data, and then hand the table-data
|
|
235
|
+
* // to the table formatter, add column formats, and format the table, getting
|
|
236
|
+
* // back a new TableData<string>
|
|
237
|
+
* const tableData: TableData<string> = TableData.fromDataFrame<string | number | Date>(data)
|
|
238
|
+
* // from the table-data, create a table-formatter
|
|
239
|
+
* .flatMap(tableData => createTableFormatterFrom(tableData)
|
|
240
|
+
* // add a column formatter for the first column of dates
|
|
241
|
+
* .addColumnFormatter(0, value => (value as Date).toLocaleDateString())
|
|
242
|
+
* // add a column formatter to the second column of number
|
|
243
|
+
* .flatMap(tf => tf.addColumnFormatter(1, value => defaultFormatter(value)))
|
|
244
|
+
* // add a column formatter to the fourth column of currencies
|
|
245
|
+
* .flatMap(tf => tf.addColumnFormatter(3, value => `$ ${(value as number).toFixed(2)}`))
|
|
246
|
+
* .flatMap(tf => tf.addColumnFormatter(4, value => `${(value as number).toFixed(0)}`))
|
|
247
|
+
* // format the table into a new TableData<string> object
|
|
248
|
+
* .flatMap(tf => tf.formatTable())
|
|
249
|
+
* )
|
|
250
|
+
* .getOrThrow()
|
|
251
|
+
*
|
|
252
|
+
* // we expect the data-frame in the table data to be the following
|
|
253
|
+
* const expectedData = DataFrame.from<string>([
|
|
254
|
+
* ['2/1/2021', '12345', 'gnm-f234', '$ 123.45', '4'],
|
|
255
|
+
* ['2/2/2021', '23456', 'gnm-g234', '$ 23.45', '5'],
|
|
256
|
+
* ['2/3/2021', '34567', 'GNM-H234', '$ 3.65', '40'],
|
|
257
|
+
* ['2/4/2021', '45678', 'gnm-i234', '$ 314.15', '9'],
|
|
258
|
+
* ]).getOrThrow()
|
|
259
|
+
* ```
|
|
260
|
+
*/
|
|
155
261
|
addRowFormatter(rowIndex: number, formatter: Formatter<V>, priority: number = 0): Result<TableFormatter<V>, string> {
|
|
156
262
|
return this.dataFrame
|
|
157
263
|
.tagRow<Formatting<V>>(rowIndex, TableFormatterType.ROW, {formatter, priority})
|
|
158
264
|
.map(data => new TableFormatter<V>(data))
|
|
159
265
|
}
|
|
160
266
|
|
|
267
|
+
/**
|
|
268
|
+
* Adds formatters to specified row indexes in a table formatter.
|
|
269
|
+
*
|
|
270
|
+
* @param rowIndexes An array of row indexes to which the formatter will be applied.
|
|
271
|
+
* @param formatter The formatting function to apply to the specified columns.
|
|
272
|
+
* @param [priority = 0] The priority level for the formatter. Higher priority formatters are applied first.
|
|
273
|
+
* @return A result indicating the success or failure of adding the row formatters.
|
|
274
|
+
* @see addColumnFormatter
|
|
275
|
+
* @see addRowFormatter
|
|
276
|
+
* @see addRowFormatters
|
|
277
|
+
* @see addCellFormatter
|
|
278
|
+
* @see addCellFormatters
|
|
279
|
+
* @example
|
|
280
|
+
* ```typescript
|
|
281
|
+
* const data = DataFrame.from<string | number | Date>([
|
|
282
|
+
* [dateTimeFor(1, 1), 12345, 'gnm-f234', 123.45, 4],
|
|
283
|
+
* [dateTimeFor(2, 2), 23456, 'gnm-g234', 23.45, 5],
|
|
284
|
+
* [dateTimeFor(3, 3), 34567, 'gnm-h234', 3.65, 40],
|
|
285
|
+
* [dateTimeFor(4, 4), 45678, 'gnm-i234', 314.15, 9],
|
|
286
|
+
* ]).getOrThrow()
|
|
287
|
+
* const columnHeader = ['Date-Time', 'Customer ID', 'Product ID', 'Purchase Price', 'Amount']
|
|
288
|
+
* const rowHeader = [1, 2, 3, 4]
|
|
289
|
+
*
|
|
290
|
+
* const tableData = TableData.fromDataFrame<string | number | Date>(data)
|
|
291
|
+
* .withColumnHeader(columnHeader)
|
|
292
|
+
* .flatMap(td => td.withRowHeader(rowHeader))
|
|
293
|
+
* .flatMap(tableData => TableFormatter.fromTableData(tableData)
|
|
294
|
+
* // add the default formatter for the column header, at the highest priority so that
|
|
295
|
+
* // it is the one that applies to the row representing the column header
|
|
296
|
+
* .addRowFormatters([0], defaultFormatter, Infinity)
|
|
297
|
+
* // add the default formatter for the row header, at the highest priority so that
|
|
298
|
+
* // it is the one that applies to the column representing the row header
|
|
299
|
+
* .flatMap(tf => tf.addColumnFormatters([0], defaultFormatter, Infinity))
|
|
300
|
+
* // add the column formatters for each column at the default (lowest) priority
|
|
301
|
+
* // (notice that the columns are shifted by one for the columns because the row-header
|
|
302
|
+
* // occupies the first column (index=0))
|
|
303
|
+
* .flatMap(tf => tf.addColumnFormatter(1, value => (value as Date).toLocaleDateString()))
|
|
304
|
+
* .flatMap(tf => tf.addColumnFormatter(2, value => defaultFormatter(value)))
|
|
305
|
+
* .flatMap(tf => tf.addColumnFormatter(4, value => `$ ${(value as number).toFixed(2)}`))
|
|
306
|
+
* .flatMap(tf => tf.addColumnFormatter(5, value => `${(value as number).toFixed(0)}`))
|
|
307
|
+
* // format the table data and get back a TableData<string>
|
|
308
|
+
* .flatMap(tf => tf.formatTable())
|
|
309
|
+
* )
|
|
310
|
+
* .getOrThrow()
|
|
311
|
+
*
|
|
312
|
+
* const expectedData = DataFrame.from<string>([
|
|
313
|
+
* ['2/1/2021', '12345', 'gnm-f234', '$ 123.45', '4'],
|
|
314
|
+
* ['2/2/2021', '23456', 'gnm-g234', '$ 23.45', '5'],
|
|
315
|
+
* ['2/3/2021', '34567', 'gnm-h234', '$ 3.65', '40'],
|
|
316
|
+
* ['2/4/2021', '45678', 'gnm-i234', '$ 314.15', '9'],
|
|
317
|
+
* ]).getOrThrow()
|
|
318
|
+
* ```
|
|
319
|
+
*/
|
|
161
320
|
addRowFormatters(rowIndexes: Array<number>, formatter: Formatter<V>, priority: number = 0): Result<TableFormatter<V>, string> {
|
|
162
321
|
return TableFormatter.addRowFormatters(this, rowIndexes.slice(), formatter, priority)
|
|
163
322
|
}
|
|
@@ -179,14 +338,125 @@ export class TableFormatter<V> {
|
|
|
179
338
|
return successResult(tableFormatter)
|
|
180
339
|
}
|
|
181
340
|
|
|
341
|
+
/**
|
|
342
|
+
* Adds a formatter to a specified cell based on the row and column indexes.
|
|
343
|
+
* @param rowIndex The row index of the cell.
|
|
344
|
+
* @param columnIndex The column index of the cell.
|
|
345
|
+
* @param formatter The formatter to apply to the cell.
|
|
346
|
+
* @param [priority = 0] The priority level for the formatter. Higher priority formatters are applied first.
|
|
347
|
+
* @return A result indicating the success or failure of adding the cell formatter.
|
|
348
|
+
* @see addColumnFormatter
|
|
349
|
+
* @see addRowFormatter
|
|
350
|
+
* @see addRowFormatters
|
|
351
|
+
* @see addCellFormatters
|
|
352
|
+
* @see addCellFormatters
|
|
353
|
+
* @example
|
|
354
|
+
* ```typescript
|
|
355
|
+
* const data = DataFrame.from<string | number | Date>([
|
|
356
|
+
* [dateTimeFor(1, 1), 12345, 'gnm-f234', 123.45, 4],
|
|
357
|
+
* [dateTimeFor(2, 2), 23456, 'gnm-g234', 23.45, 5],
|
|
358
|
+
* [dateTimeFor(3, 3), 34567, 'gnm-h234', 3.65, 40],
|
|
359
|
+
* [dateTimeFor(4, 4), 45678, 'gnm-i234', 314.15, 9],
|
|
360
|
+
* ]).getOrThrow()
|
|
361
|
+
* const columnHeader = ['Date-Time', 'Customer ID', 'Product ID', 'Purchase Price', 'Amount']
|
|
362
|
+
* const rowHeader = [1, 2, 3, 4]
|
|
363
|
+
*
|
|
364
|
+
* const tableData = TableData.fromDataFrame<string | number | Date>(data)
|
|
365
|
+
* .withColumnHeader(columnHeader)
|
|
366
|
+
* .flatMap(td => td.withRowHeader(rowHeader))
|
|
367
|
+
* .flatMap(tableData => TableFormatter.fromTableData(tableData)
|
|
368
|
+
* // add the default formatter for the column header, at the highest priority so that
|
|
369
|
+
* // it is the one that applies to the row representing the column header
|
|
370
|
+
* .addRowFormatters([0], defaultFormatter, Infinity)
|
|
371
|
+
* // add the default formatter for the row header, at the highest priority so that
|
|
372
|
+
* // it is the one that applies to the column representing the row header
|
|
373
|
+
* .flatMap(tf => tf.addColumnFormatters([0], defaultFormatter, Infinity))
|
|
374
|
+
* // add the column formatters for each column at the default (lowest) priority
|
|
375
|
+
* // (notice that the columns are shifted by one for the columns because the row-header
|
|
376
|
+
* // occupies the first column (index=0))
|
|
377
|
+
* .flatMap(tf => tf.addColumnFormatter(1, value => (value as Date).toLocaleDateString()))
|
|
378
|
+
* .flatMap(tf => tf.addColumnFormatter(2, value => defaultFormatter(value)))
|
|
379
|
+
* .flatMap(tf => tf.addColumnFormatter(4, value => `$ ${(value as number).toFixed(2)}`))
|
|
380
|
+
* .flatMap(tf => tf.addColumnFormatter(5, value => `${(value as number).toFixed(0)}`))
|
|
381
|
+
* // override the cell formatter for a select set of cells
|
|
382
|
+
* .flatMap(tf => tf.addCellFormatter(1, 4, value => `${(value as number).toFixed(2)}`, 1000))
|
|
383
|
+
* .flatMap(tf => tf.addCellFormatters([[3, 5], [4, 5]], value => `${((value as number) * 10).toFixed(0)}`, 1000))
|
|
384
|
+
* // format the table data and get back a TableData<string>
|
|
385
|
+
* .flatMap(tf => tf.formatTable())
|
|
386
|
+
* )
|
|
387
|
+
* .getOrThrow()
|
|
388
|
+
*
|
|
389
|
+
* const expectedData = DataFrame.from<string>([
|
|
390
|
+
* ['2/1/2021', '12345', 'gnm-f234', '123.45', '4'], // overwrite (1, 4) to remove "$"
|
|
391
|
+
* ['2/2/2021', '23456', 'gnm-g234', '$ 23.45', '5'],
|
|
392
|
+
* ['2/3/2021', '34567', 'gnm-h234', '$ 3.65', '400'], // overwrite (3, 5) to multiply by 10
|
|
393
|
+
* ['2/4/2021', '45678', 'gnm-i234', '$ 314.15', '90'], // overwrite (4, 5) to multiply by 10
|
|
394
|
+
* ]).getOrThrow()
|
|
395
|
+
* ```
|
|
396
|
+
*/
|
|
182
397
|
addCellFormatter(rowIndex: number, columnIndex: number, formatter: Formatter<V>, priority: number = 0): Result<TableFormatter<V>, string> {
|
|
183
398
|
return this.dataFrame
|
|
184
399
|
.tagCell(rowIndex, columnIndex, TableFormatterType.CELL, {formatter, priority})
|
|
185
400
|
.map(data => new TableFormatter<V>(data))
|
|
186
401
|
}
|
|
187
402
|
|
|
403
|
+
/**
|
|
404
|
+
* Adds formatters to specified cell indexes in a table formatter.
|
|
405
|
+
* @param cellIndexes An array of cell indexes to which the formatter will be applied.
|
|
406
|
+
* @param formatter The formatting function to apply to the specified cells.
|
|
407
|
+
* @param [priority = 0] The priority level for the formatter. Higher priority formatters are applied first.
|
|
408
|
+
* @return A result indicating the success or failure of adding the cell formatters.
|
|
409
|
+
* @see addColumnFormatter
|
|
410
|
+
* @see addRowFormatter
|
|
411
|
+
* @see addRowFormatters
|
|
412
|
+
* @see addCellFormatters
|
|
413
|
+
* @see addCellFormatters
|
|
414
|
+
* @example
|
|
415
|
+
* ```typescript
|
|
416
|
+
* const data = DataFrame.from<string | number | Date>([
|
|
417
|
+
* [dateTimeFor(1, 1), 12345, 'gnm-f234', 123.45, 4],
|
|
418
|
+
* [dateTimeFor(2, 2), 23456, 'gnm-g234', 23.45, 5],
|
|
419
|
+
* [dateTimeFor(3, 3), 34567, 'gnm-h234', 3.65, 40],
|
|
420
|
+
* [dateTimeFor(4, 4), 45678, 'gnm-i234', 314.15, 9],
|
|
421
|
+
* ]).getOrThrow()
|
|
422
|
+
* const columnHeader = ['Date-Time', 'Customer ID', 'Product ID', 'Purchase Price', 'Amount']
|
|
423
|
+
* const rowHeader = [1, 2, 3, 4]
|
|
424
|
+
*
|
|
425
|
+
* const tableData = TableData.fromDataFrame<string | number | Date>(data)
|
|
426
|
+
* .withColumnHeader(columnHeader)
|
|
427
|
+
* .flatMap(td => td.withRowHeader(rowHeader))
|
|
428
|
+
* .flatMap(tableData => TableFormatter.fromTableData(tableData)
|
|
429
|
+
* // add the default formatter for the column header, at the highest priority so that
|
|
430
|
+
* // it is the one that applies to the row representing the column header
|
|
431
|
+
* .addRowFormatters([0], defaultFormatter, Infinity)
|
|
432
|
+
* // add the default formatter for the row header, at the highest priority so that
|
|
433
|
+
* // it is the one that applies to the column representing the row header
|
|
434
|
+
* .flatMap(tf => tf.addColumnFormatters([0], defaultFormatter, Infinity))
|
|
435
|
+
* // add the column formatters for each column at the default (lowest) priority
|
|
436
|
+
* // (notice that the columns are shifted by one for the columns because the row-header
|
|
437
|
+
* // occupies the first column (index=0))
|
|
438
|
+
* .flatMap(tf => tf.addColumnFormatter(1, value => (value as Date).toLocaleDateString()))
|
|
439
|
+
* .flatMap(tf => tf.addColumnFormatter(2, value => defaultFormatter(value)))
|
|
440
|
+
* .flatMap(tf => tf.addColumnFormatter(4, value => `$ ${(value as number).toFixed(2)}`))
|
|
441
|
+
* .flatMap(tf => tf.addColumnFormatter(5, value => `${(value as number).toFixed(0)}`))
|
|
442
|
+
* // override the cell formatter for a select set of cells
|
|
443
|
+
* .flatMap(tf => tf.addCellFormatter(1, 4, value => `${(value as number).toFixed(2)}`, 1000))
|
|
444
|
+
* .flatMap(tf => tf.addCellFormatters([[3, 5], [4, 5]], value => `${((value as number) * 10).toFixed(0)}`, 1000))
|
|
445
|
+
* // format the table data and get back a TableData<string>
|
|
446
|
+
* .flatMap(tf => tf.formatTable())
|
|
447
|
+
* )
|
|
448
|
+
* .getOrThrow()
|
|
449
|
+
*
|
|
450
|
+
* const expectedData = DataFrame.from<string>([
|
|
451
|
+
* ['2/1/2021', '12345', 'gnm-f234', '123.45', '4'], // overwrite (1, 4) to remove "$"
|
|
452
|
+
* ['2/2/2021', '23456', 'gnm-g234', '$ 23.45', '5'],
|
|
453
|
+
* ['2/3/2021', '34567', 'gnm-h234', '$ 3.65', '400'], // overwrite (3, 5) to multiply by 10
|
|
454
|
+
* ['2/4/2021', '45678', 'gnm-i234', '$ 314.15', '90'], // overwrite (4, 5) to multiply by 10
|
|
455
|
+
* ]).getOrThrow()
|
|
456
|
+
* ```
|
|
457
|
+
*/
|
|
188
458
|
addCellFormatters(cellIndexes: Array<[x: number, y: number]>, formatter: Formatter<V>, priority: number = 0): Result<TableFormatter<V>, string> {
|
|
189
|
-
return TableFormatter.addCellFormatters(this, cellIndexes, formatter, priority)
|
|
459
|
+
return TableFormatter.addCellFormatters(this, cellIndexes.slice(), formatter, priority)
|
|
190
460
|
}
|
|
191
461
|
|
|
192
462
|
private static addCellFormatters<V>(
|
|
@@ -196,7 +466,7 @@ export class TableFormatter<V> {
|
|
|
196
466
|
priority: number = 0
|
|
197
467
|
): Result<TableFormatter<V>, string> {
|
|
198
468
|
if (cellIndexes.length > 0) {
|
|
199
|
-
const [
|
|
469
|
+
const [rowIndex, columnIndex] = cellIndexes.shift() ?? [undefined, undefined]
|
|
200
470
|
if (rowIndex != null && columnIndex != null) {
|
|
201
471
|
return tableFormatter
|
|
202
472
|
.addCellFormatter(rowIndex, columnIndex, formatter, priority)
|
|
@@ -277,9 +547,20 @@ export class TableFormatter<V> {
|
|
|
277
547
|
return this.formatTableInto<C, TableData<string>>(dataFrame => TableData.fromDataFrame<string>(dataFrame))
|
|
278
548
|
}
|
|
279
549
|
|
|
550
|
+
/**
|
|
551
|
+
* Generally, the {@link TableFormatter} formats the {@link DataFrame} into a `TableData<string>`
|
|
552
|
+
* where all the elements of the {@link TableData} are a string. Because a formatted table does
|
|
553
|
+
* not necessarily have to be a `TableData<string>` (which would contain a `DataFrame<string>`,
|
|
554
|
+
* this method allows mapping a {@link DataFrame} into any desired type.
|
|
555
|
+
* @param mapper A function that takes a `DataFrame<string>` and returns a desired type (`D`).
|
|
556
|
+
* @return A `Result<D, string>` where the `Result<D, string>` is either success, containing the
|
|
557
|
+
* desired type (`D`), or failure, containing an error message.
|
|
558
|
+
* @see formatTable
|
|
559
|
+
*/
|
|
280
560
|
formatTableInto<C extends TagCoordinate, D = TableData<string>>(mapper: (dataFrame: DataFrame<string>) => D): Result<D, string> {
|
|
281
561
|
const formattingFailures: Array<string> = []
|
|
282
|
-
const formattedDataFrame = this
|
|
562
|
+
const formattedDataFrame = this
|
|
563
|
+
.dataFrame
|
|
283
564
|
.mapElements<string>((elem, row, col) => {
|
|
284
565
|
const tags = this.dataFrame
|
|
285
566
|
.tagsFor(row, col)
|
package/TableStyler.ts
CHANGED
|
@@ -7,6 +7,7 @@ import {
|
|
|
7
7
|
type CellStyle,
|
|
8
8
|
type ColumnHeaderStyle,
|
|
9
9
|
type ColumnStyle,
|
|
10
|
+
defaultBorder,
|
|
10
11
|
defaultCellStyle,
|
|
11
12
|
defaultColumnHeaderStyle,
|
|
12
13
|
defaultColumnStyle,
|
|
@@ -20,6 +21,7 @@ import {
|
|
|
20
21
|
defaultTablePadding,
|
|
21
22
|
type Dimension,
|
|
22
23
|
type FooterStyle,
|
|
24
|
+
Margin,
|
|
23
25
|
type Padding,
|
|
24
26
|
type RowHeaderStyle,
|
|
25
27
|
type RowStyle,
|
|
@@ -28,7 +30,7 @@ import {
|
|
|
28
30
|
type Stylings,
|
|
29
31
|
type TableFont,
|
|
30
32
|
type TableStylerProps,
|
|
31
|
-
TableStyleType
|
|
33
|
+
TableStyleType
|
|
32
34
|
} from "./stylings";
|
|
33
35
|
|
|
34
36
|
/**
|
|
@@ -168,14 +170,26 @@ export class StyledTable<V> {
|
|
|
168
170
|
return successResult(tags[0])
|
|
169
171
|
}
|
|
170
172
|
|
|
173
|
+
/**
|
|
174
|
+
* Checks if the table has a row header.
|
|
175
|
+
* @returns `true` if the table has a row header, `false` otherwise
|
|
176
|
+
*/
|
|
171
177
|
hasRowHeader(): boolean {
|
|
172
178
|
return TableData.hasRowHeader(this.dataFrame)
|
|
173
179
|
}
|
|
174
180
|
|
|
181
|
+
/**
|
|
182
|
+
* Checks if the table has a column header.
|
|
183
|
+
* @returns `true` if the table has a row header, `false` otherwise
|
|
184
|
+
*/
|
|
175
185
|
hasColumnHeader(): boolean {
|
|
176
186
|
return TableData.hasColumnHeader(this.dataFrame)
|
|
177
187
|
}
|
|
178
188
|
|
|
189
|
+
/**
|
|
190
|
+
* Checks if the table has a footer header.
|
|
191
|
+
* @returns `true` if the table has a row header, `false` otherwise
|
|
192
|
+
*/
|
|
179
193
|
hasFooter(): boolean {
|
|
180
194
|
return TableData.hasFooter(this.dataFrame)
|
|
181
195
|
}
|
|
@@ -206,6 +220,10 @@ export class StyledTable<V> {
|
|
|
206
220
|
.map(tag => tag.value as Styling<ColumnHeaderStyle>)
|
|
207
221
|
}
|
|
208
222
|
|
|
223
|
+
/**
|
|
224
|
+
* Gets the style for the footer.
|
|
225
|
+
* @returns A Result containing the footer style if found, or an error message
|
|
226
|
+
*/
|
|
209
227
|
footerStyle(): Result<Styling<FooterStyle>, string> {
|
|
210
228
|
if (!TableData.hasFooter(this.dataFrame)) {
|
|
211
229
|
return failureResult("(StyledTable::footerStyle) The table data does not have a footer")
|
|
@@ -493,6 +511,14 @@ export class TableStyler<V> {
|
|
|
493
511
|
)
|
|
494
512
|
}
|
|
495
513
|
|
|
514
|
+
/**
|
|
515
|
+
* Applies the specified font settings for the table and returns a new {@link TableStyler}
|
|
516
|
+
* instance with the updated font configuration.
|
|
517
|
+
*
|
|
518
|
+
* @param font - The font configuration to be applied. This object can include partial
|
|
519
|
+
* properties of the TableFont.
|
|
520
|
+
* @return A new {@link TableStyler} instance with the updated font settings.
|
|
521
|
+
*/
|
|
496
522
|
withTableFont(font: Partial<TableFont>): TableStyler<V> {
|
|
497
523
|
const builder = this.copy()
|
|
498
524
|
builder.font = {...defaultTableFont, ...font}
|
|
@@ -670,8 +696,25 @@ export class TableStyler<V> {
|
|
|
670
696
|
.getOrElse(this)
|
|
671
697
|
}
|
|
672
698
|
|
|
673
|
-
|
|
674
|
-
|
|
699
|
+
/**
|
|
700
|
+
* Applies specific styles to rows in a table based on the provided row indexes.
|
|
701
|
+
*
|
|
702
|
+
* @param rowIndexes - An array of row indexes to which the styles will be applied. If the
|
|
703
|
+
* array is empty, all rows will be styled.
|
|
704
|
+
* @param rowStyle - An object representing the styles to apply to the specified rows.
|
|
705
|
+
* @param [priority=0] - An optional priority value for the styles. Higher priority values
|
|
706
|
+
* override lower ones.
|
|
707
|
+
* @return A new TableStyler instance with the specified row styles applied.
|
|
708
|
+
* @see withRowStyle
|
|
709
|
+
*/
|
|
710
|
+
withRowStyles(
|
|
711
|
+
rowIndexes: Array<number>,
|
|
712
|
+
rowStyle: Partial<RowStyle>,
|
|
713
|
+
priority: number = 0
|
|
714
|
+
): TableStyler<V> {
|
|
715
|
+
const indexes = rowIndexes.length > 0 ?
|
|
716
|
+
rowIndexes :
|
|
717
|
+
new Array(this.dataFrame.rowCount()).fill(0).map((_, i) => i)
|
|
675
718
|
return TableStyler.withRowStyles(this, indexes, rowStyle, priority)
|
|
676
719
|
}
|
|
677
720
|
|
|
@@ -712,8 +755,24 @@ export class TableStyler<V> {
|
|
|
712
755
|
.getOrElse(this)
|
|
713
756
|
}
|
|
714
757
|
|
|
715
|
-
|
|
716
|
-
|
|
758
|
+
/**
|
|
759
|
+
* Applies specified styles to the columns of a table.
|
|
760
|
+
*
|
|
761
|
+
* @param columnIndexes - Array of column indexes to which the style should be applied. If the array
|
|
762
|
+
* is empty, styles will be applied to all columns.
|
|
763
|
+
* @param columnStyle - Partial column style configuration object defining the styles to be applied.
|
|
764
|
+
* @param [priority=0] - Optional priority value to determine the precedence of this style over others.
|
|
765
|
+
* @return Returns an instance of TableStyler with the updated column styles applied.
|
|
766
|
+
* @see withColumnStyle
|
|
767
|
+
*/
|
|
768
|
+
withColumnStyles(
|
|
769
|
+
columnIndexes: Array<number>,
|
|
770
|
+
columnStyle: Partial<ColumnStyle>,
|
|
771
|
+
priority: number = 0
|
|
772
|
+
): TableStyler<V> {
|
|
773
|
+
const indexes = columnIndexes.length > 0 ?
|
|
774
|
+
columnIndexes :
|
|
775
|
+
new Array(this.dataFrame.columnCount()).fill(0).map((_, i) => i)
|
|
717
776
|
return TableStyler.withColumnStyles(this, indexes, columnStyle, priority)
|
|
718
777
|
}
|
|
719
778
|
|
|
@@ -762,6 +821,16 @@ export class TableStyler<V> {
|
|
|
762
821
|
.getOrElse(this)
|
|
763
822
|
}
|
|
764
823
|
|
|
824
|
+
/**
|
|
825
|
+
* Sets the style for a specific cell based on a predicate.
|
|
826
|
+
* @param predicate A function that accepts the value, row-index, and column-index of the cell, and
|
|
827
|
+
* returns `true` if the cell should be styled, or `false` otherwise.
|
|
828
|
+
* @param cellStyle The style to apply to the cell if the predicate is `true`.
|
|
829
|
+
* @param priority The style's priority. Higher priority values override lower ones.
|
|
830
|
+
* @returns A new TableStyler instance with the cell style applied.
|
|
831
|
+
* @see withCellStyle
|
|
832
|
+
* @see withCellStyles
|
|
833
|
+
*/
|
|
765
834
|
withCellStyleWhen(
|
|
766
835
|
predicate: (value: V, rowIndex: number, columnIndex: number) => boolean,
|
|
767
836
|
cellStyle: Partial<CellStyle>,
|