-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcore.ts
More file actions
460 lines (422 loc) · 10.6 KB
/
core.ts
File metadata and controls
460 lines (422 loc) · 10.6 KB
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
import isNumber from "lodash/isNumber";
import has from "lodash/has";
import get from "lodash/get";
import set from "lodash/set";
import { acceptAllowedValue } from "./utils";
type Optional<T, K extends keyof T> = Pick<Partial<T>, K> & Omit<T, K>;
type Falsey = 0 | false | null | undefined;
export type GenericData = string | number | boolean | null | undefined;
export type RowData<Data = GenericData> = Data[];
export type TableData<Data = GenericData> = RowData<Data>[];
export type TableDataHeaderType = "row" | "column";
export interface TableDataHeader {
header: TableDataConfigHeader;
name: string;
c: number;
r: number;
width: number;
height: number;
}
export type TableDataHeaders = TableDataHeader[][];
export type TableDataConfigHeader = {
type: TableDataHeaderType;
c: number;
r: number;
depth: number;
};
export type TableDataConfigHeaders = Optional<TableDataConfigHeader, "depth">[];
export type TableDataConfigPreset =
| "row"
| "column"
| "row.column"
| "column.row"
| "row.row"
| "column.column"
| Falsey;
export type TableDataConfigModifyHeaders = (headers: TableDataHeaders) => void;
export interface TableDataConfig {
preset?: TableDataConfigPreset;
headers?: TableDataConfigHeaders;
modifyHeaders?: TableDataConfigModifyHeaders;
}
/**
* Convert table-formatted data to a nested JSON object.
*
* Table data is structured as array of rows (Y axis), which
* are each then an array of columns (X axis).
*
* Imagine a table:
*
* | a | b | c |
* |----|----|----|
* | 1 | 2 | 3 |
* | do | re | mi |
*
* Would be represented as table data like so:
*
* ```js
* [
* [
* "a", "b", "c"
* ],
* [
* 1, 2, 3
* ],
* [
* "do", "re", "mi"
* ]
* ]
* ```
*
* If the first row is the header, then the output would
* look like this:
*
* ```json
* [
* {
* "a": 1,
* "b": 2,
* "c": 3
* },
* {
* "a": "do"
* "b": "re",
* "c": "mi"
* }
* ]
* ```
*
* If the first column is the header, then the output would
* look like this:
*
* ```json
* [
* {
* "a": "b",
* "1": 2,
* "do": "re"
* },
* {
* "a": "c",
* "1": 3,
* "do": "mi"
* }
* ]
* ```
*
* We can also combine row and column headers to make
* nested objects. We can provide the `headers` array of
* objects to define type of header and column/row co-ords
* of where the first cell of the header starts:
*
* ```js
* headers = [
* {
* type: "row",
* c: 0,
* r: 0,
* },
* {
* type: "column",
* c: 0,
* r: 0,
* },
* ];
* ```
*
* As a shorthand, we can use the `preset` option which has
* pre-configured `headers`:
*
* - `row`
* - `column`
* - `row.column`
* - `column.row`
* - `row.row`
* - `column.column`
*/
export function convertTableDataToJSON<Data = GenericData, Output = object>(
input: TableData<Data>,
options?: TableDataConfig
): Output | Output[] {
const _options: TableDataConfig = {
preset: "row",
headers: undefined,
...options,
};
acceptAllowedValue(
_options.preset,
[
undefined,
false,
null,
"row",
"column",
"row.column",
"column.row",
"row.row",
"column.column",
],
true
);
if (_options.headers) {
_options.headers.forEach((h) => {
acceptAllowedValue(
h.type,
[undefined, false, null, "row", "column"],
true
);
acceptAllowedValue(h.c, isNumber, true);
acceptAllowedValue(h.r, isNumber, true);
});
}
// Configure first/second header defaults using preset
if (!_options.headers || !_options.headers.length) {
switch (_options.preset) {
case "row":
default:
_options.headers = [
{
type: "row",
c: 0,
r: 0,
},
];
break;
case "column":
_options.headers = [
{
type: "column",
c: 0,
r: 0,
},
];
break;
case "row.column":
_options.headers = [
{
type: "row",
c: 0,
r: 0,
},
{
type: "column",
c: 0,
r: 0,
},
];
break;
case "column.row":
_options.headers = [
{
type: "column",
c: 0,
r: 0,
},
{
type: "row",
c: 0,
r: 0,
},
];
break;
case "row.row":
_options.headers = [
{
type: "row",
c: 0,
r: 0,
},
{
type: "row",
c: 0,
r: 1,
},
];
break;
case "column.column":
_options.headers = [
{
type: "column",
c: 0,
r: 0,
},
{
type: "column",
c: 1,
r: 0,
},
];
break;
}
}
// Ensure each header config has a depth value based on the order they appear in the headers array
_options.headers.forEach((h, index) => {
h.depth = h.depth === undefined || h.depth !== index ? index : h.depth;
});
const _headers = _options.headers as TableDataConfigHeader[];
const getHeaderRow = (c: number, r: number): TableDataConfigHeader[] =>
_headers.filter(
({ type, c: x, r: y }) => type === "row" && c >= x && r === y
);
const getHeaderColumn = (c: number, r: number): TableDataConfigHeader[] =>
_headers.filter(
({ type, c: x, r: y }) => type === "column" && c === x && r >= y
);
// Extract headers to determine data mapping and nesting
const headers: TableDataHeaders = [];
const maxColumns = input.reduce(
(acc: number, row: RowData<Data>) => Math.max(acc, row.length),
0
);
const maxRows = input.length;
let startDataRIndex = Infinity;
let startDataCIndex = Infinity;
for (let r = 0; r < maxRows; r++) {
for (let c = 0; c < maxColumns; c++) {
const headerRows = getHeaderRow(c, r);
const headerColumns = getHeaderColumn(c, r);
// Check contents exist, if so check if relevant header row/column and add entry
const cell = get(input, [r, c]);
if (cell !== undefined && cell !== null && cell !== "") {
if (headerRows.length > 0) {
headerRows.forEach((h) => {
if (!headers[h.depth]) headers[h.depth] = [];
headers[h.depth].push({
header: h,
name: (cell + "").trim(),
c,
r,
width: 1,
height: 1,
});
startDataRIndex = Math.min(startDataRIndex, r + 1);
});
}
if (headerColumns.length > 0) {
headerColumns.forEach((h) => {
if (!headers[h.depth]) headers[h.depth] = [];
headers[h.depth].push({
header: h,
name: (cell + "").trim(),
c,
r,
width: 1,
height: 1,
});
startDataCIndex = Math.min(startDataCIndex, c + 1);
});
}
}
}
}
// Extra plugin functionality to modify the headers before
// processing output data.
if (typeof _options.modifyHeaders === "function") {
_options.modifyHeaders(headers);
}
startDataRIndex = startDataRIndex === Infinity ? 0 : startDataRIndex;
startDataCIndex = startDataCIndex === Infinity ? 0 : startDataCIndex;
// Get the cell headers for a specific cell
const getCellHeaders = (c: number, r: number) => {
let cellHeaders: TableDataHeader[][] = [];
headers.forEach((cells, depth) => {
const _cellHeaders = cells.filter((h) => {
const cMin = h.c;
const cMax = h.c + h.width;
const rMin = h.r;
const rMax = h.r + h.height;
const isWithin =
h.header.type === "row"
? c >= cMin && c < cMax
: r >= rMin && r < rMax;
return isWithin;
});
cellHeaders[depth] = _cellHeaders;
});
// Ensure to remove any empty axes
return cellHeaders.filter((cells) => cells.length);
};
const addCellToCollection = ({
c,
r,
collection,
}: {
c: number;
r: number;
collection: Output;
}) => {
const cell = get(input, [r, c]);
const cellHeaders = getCellHeaders(c, r);
// Avoid setting header values as cell values
let setCell = true;
for (let depth = 0; depth < cellHeaders.length; depth++) {
for (let index = 0; index < cellHeaders[depth].length; index++) {
const headerName = cellHeaders[depth][index].name;
if (cell === headerName) {
setCell = false;
break;
}
}
if (!setCell) break;
}
if (!setCell) return;
// Make sure any nested objects exist
const cellObjectPath: (string | number)[] = [];
cellHeaders.forEach((cells, depth) => {
cells.forEach((h) => {
cellObjectPath[depth] = h.name;
if (!has(collection, cellObjectPath))
set(collection as object, cellObjectPath, {});
});
});
// Set the cell value
set(collection as object, cellObjectPath, cell);
};
const entryAxis = headers[0][0].header.type;
// Perpendicular axis (e.g. `row.column`, `column.row`) affects
// output, whether it is array of entries, or just an object
// with entries as props.
const hasPerpendicularAxis =
headers.length > 0 &&
new Set(headers.map((headerCells) => headerCells[0].header.type)).size > 1;
const output = hasPerpendicularAxis ? ({} as Output) : ([] as Output[]);
// Entries are represented by rows, individual cells by columns
if (entryAxis === "row") {
for (let r = startDataRIndex; r < maxRows; r++) {
const collection = output instanceof Array ? ({} as Output) : output;
for (let c = startDataCIndex; c < maxColumns; c++) {
addCellToCollection({
c,
r,
collection,
});
}
if (
output instanceof Array &&
Object.keys(collection as Output[]).length
) {
output.push(collection);
}
}
} else {
// Entries are represented by columns, individual cells by rows
for (let c = startDataCIndex; c < maxColumns; c++) {
const collection = output instanceof Array ? ({} as Output) : output;
for (let r = startDataRIndex; r < maxRows; r++) {
addCellToCollection({
c,
r,
collection,
});
}
if (
output instanceof Array &&
Object.keys(collection as Output[]).length
) {
output.push(collection);
}
}
}
return output;
}
export default convertTableDataToJSON;