@loaders.gl/schema 4.0.0-alpha.23 → 4.0.0-alpha.24

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 (65) hide show
  1. package/dist/dist.min.js +517 -927
  2. package/dist/es5/index.js +7 -0
  3. package/dist/es5/index.js.map +1 -1
  4. package/dist/es5/lib/table/simple-table/convert-table.js +24 -0
  5. package/dist/es5/lib/table/simple-table/convert-table.js.map +1 -1
  6. package/dist/es5/types/binary-geometries.js.map +1 -1
  7. package/dist/es5/types/category-gis.js.map +1 -1
  8. package/dist/esm/index.js +1 -0
  9. package/dist/esm/index.js.map +1 -1
  10. package/dist/esm/lib/table/simple-table/convert-table.js +22 -0
  11. package/dist/esm/lib/table/simple-table/convert-table.js.map +1 -1
  12. package/dist/esm/types/binary-geometries.js.map +1 -1
  13. package/dist/esm/types/category-gis.js.map +1 -1
  14. package/dist/index.d.ts +3 -2
  15. package/dist/index.d.ts.map +1 -1
  16. package/dist/lib/table/simple-table/convert-table.d.ts +34 -1
  17. package/dist/lib/table/simple-table/convert-table.d.ts.map +1 -1
  18. package/dist/types/binary-geometries.d.ts +9 -7
  19. package/dist/types/binary-geometries.d.ts.map +1 -1
  20. package/dist/types/category-gis.d.ts +1 -1
  21. package/dist/types/category-gis.d.ts.map +1 -1
  22. package/package.json +2 -2
  23. package/src/index.ts +8 -4
  24. package/src/lib/table/simple-table/convert-table.ts +73 -1
  25. package/src/types/binary-geometries.ts +10 -7
  26. package/src/types/category-gis.ts +5 -4
  27. package/dist/bundle.js +0 -5
  28. package/dist/index.js +0 -92
  29. package/dist/lib/mesh/convert-mesh.js +0 -40
  30. package/dist/lib/mesh/deduce-mesh-schema.js +0 -66
  31. package/dist/lib/mesh/mesh-to-arrow-table.js +0 -44
  32. package/dist/lib/mesh/mesh-utils.js +0 -51
  33. package/dist/lib/table/arrow/arrow-type-utils.js +0 -53
  34. package/dist/lib/table/arrow/convert-schema-arrow.js +0 -187
  35. package/dist/lib/table/arrow/convert-table-to-arrow.js +0 -57
  36. package/dist/lib/table/arrow-api/arrow-like-field.js +0 -34
  37. package/dist/lib/table/arrow-api/arrow-like-schema.js +0 -81
  38. package/dist/lib/table/arrow-api/arrow-like-table.js +0 -53
  39. package/dist/lib/table/arrow-api/arrow-like-type.js +0 -463
  40. package/dist/lib/table/arrow-api/enum.js +0 -97
  41. package/dist/lib/table/arrow-api/get-type-info.js +0 -29
  42. package/dist/lib/table/arrow-api/index.js +0 -25
  43. package/dist/lib/table/batches/base-table-batch-aggregator.js +0 -60
  44. package/dist/lib/table/batches/columnar-table-batch-aggregator.js +0 -92
  45. package/dist/lib/table/batches/row-table-batch-aggregator.js +0 -81
  46. package/dist/lib/table/batches/table-batch-aggregator.js +0 -3
  47. package/dist/lib/table/batches/table-batch-builder.js +0 -152
  48. package/dist/lib/table/simple-table/convert-table.js +0 -65
  49. package/dist/lib/table/simple-table/data-type.js +0 -93
  50. package/dist/lib/table/simple-table/make-table.js +0 -95
  51. package/dist/lib/table/simple-table/row-utils.js +0 -33
  52. package/dist/lib/table/simple-table/table-accessors.js +0 -319
  53. package/dist/lib/table/simple-table/table-column.js +0 -41
  54. package/dist/lib/table/simple-table/table-schema.js +0 -85
  55. package/dist/lib/utils/assert.js +0 -12
  56. package/dist/lib/utils/async-queue.js +0 -92
  57. package/dist/types/binary-geometries.js +0 -2
  58. package/dist/types/category-gis.js +0 -3
  59. package/dist/types/category-image.js +0 -2
  60. package/dist/types/category-mesh.js +0 -3
  61. package/dist/types/category-table.js +0 -3
  62. package/dist/types/category-texture.js +0 -3
  63. package/dist/types/flat-geometries.js +0 -3
  64. package/dist/types/schema.js +0 -3
  65. package/dist/types/types.js +0 -2
@@ -1,93 +0,0 @@
1
- "use strict";
2
- // loaders.gl, MIT license
3
- Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.getArrayTypeFromDataType = exports.getDataTypeFromTypedArray = exports.getDataTypeFromArray = exports.getDataTypeFromValue = void 0;
5
- /** Deduce column types from values */
6
- function getDataTypeFromValue(value, defaultNumberType = 'float32') {
7
- if (value instanceof Date) {
8
- return 'date-millisecond';
9
- }
10
- if (value instanceof Number) {
11
- return defaultNumberType;
12
- }
13
- if (typeof value === 'string') {
14
- return 'utf8';
15
- }
16
- if (value === null || value === 'undefined') {
17
- return 'null';
18
- }
19
- return 'null';
20
- }
21
- exports.getDataTypeFromValue = getDataTypeFromValue;
22
- /**
23
- * Deduces a simple data type "descriptor from a typed array instance
24
- */
25
- function getDataTypeFromArray(array) {
26
- let type = getDataTypeFromTypedArray(array);
27
- if (type !== 'null') {
28
- return { type, nullable: false };
29
- }
30
- if (array.length > 0) {
31
- type = getDataTypeFromValue(array[0]);
32
- return { type, nullable: true };
33
- }
34
- return { type: 'null', nullable: true };
35
- }
36
- exports.getDataTypeFromArray = getDataTypeFromArray;
37
- /**
38
- * Deduces a simple data type "descriptor from a typed array instance
39
- */
40
- function getDataTypeFromTypedArray(array) {
41
- switch (array.constructor) {
42
- case Int8Array:
43
- return 'int8';
44
- case Uint8Array:
45
- case Uint8ClampedArray:
46
- return 'uint8';
47
- case Int16Array:
48
- return 'int16';
49
- case Uint16Array:
50
- return 'uint16';
51
- case Int32Array:
52
- return 'int32';
53
- case Uint32Array:
54
- return 'uint32';
55
- case Float32Array:
56
- return 'float32';
57
- case Float64Array:
58
- return 'float64';
59
- default:
60
- return 'null';
61
- }
62
- }
63
- exports.getDataTypeFromTypedArray = getDataTypeFromTypedArray;
64
- function getArrayTypeFromDataType(type, nullable) {
65
- if (!nullable) {
66
- switch (type) {
67
- case 'int8':
68
- return Int8Array;
69
- case 'uint8':
70
- return Uint8Array;
71
- case 'int16':
72
- return Int16Array;
73
- case 'uint16':
74
- return Uint16Array;
75
- case 'int32':
76
- return Int32Array;
77
- case 'uint32':
78
- return Uint32Array;
79
- case 'float32':
80
- return Float32Array;
81
- case 'float64':
82
- return Float64Array;
83
- default:
84
- break;
85
- }
86
- }
87
- // if (typeof BigInt64Array !== 'undefined') {
88
- // TYPED_ARRAY_TO_TYPE.BigInt64Array = new Int64();
89
- // TYPED_ARRAY_TO_TYPE.BigUint64Array = new Uint64();
90
- // }
91
- return Array;
92
- }
93
- exports.getArrayTypeFromDataType = getArrayTypeFromDataType;
@@ -1,95 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.makeTableFromData = void 0;
4
- const table_schema_1 = require("./table-schema");
5
- function makeTableFromData(data) {
6
- let table;
7
- switch (getTableShapeFromData(data)) {
8
- case 'array-row-table':
9
- table = { shape: 'array-row-table', data: data };
10
- break;
11
- case 'object-row-table':
12
- table = { shape: 'object-row-table', data: data };
13
- break;
14
- case 'columnar-table':
15
- table = { shape: 'columnar-table', data: data };
16
- break;
17
- default:
18
- throw new Error('table');
19
- }
20
- const schema = (0, table_schema_1.deduceTableSchema)(table);
21
- return { ...table, schema };
22
- }
23
- exports.makeTableFromData = makeTableFromData;
24
- /** Helper function to get shape of data */
25
- function getTableShapeFromData(data) {
26
- if (Array.isArray(data)) {
27
- if (data.length === 0) {
28
- throw new Error('cannot deduce type of empty table');
29
- }
30
- // Deduce the table shape from the first row
31
- const firstRow = data[0];
32
- if (Array.isArray(firstRow)) {
33
- return 'array-row-table';
34
- }
35
- if (firstRow && typeof firstRow === 'object') {
36
- return 'object-row-table';
37
- }
38
- }
39
- if (data && typeof data === 'object') {
40
- return 'columnar-table';
41
- }
42
- throw new Error('invalid table');
43
- }
44
- /** Convert any table into object row format *
45
- export function makeColumnarTable(table: Table): ColumnarTable {
46
- if (table.shape === 'columnar-table') {
47
- return table;
48
- }
49
- const length = getTableLength(table);
50
- const data = new Array<{[key: string]: unknown}>(length);
51
- for (let rowIndex = 0; rowIndex < length; rowIndex++) {
52
- data[rowIndex] = getTableRowAsObject(table, rowIndex);
53
- }
54
- return {
55
- shape: 'columnar-table',
56
- schema: table.schema,
57
- data
58
- };
59
- }
60
-
61
-
62
- /** Convert any table into array row format *
63
- export function makeArrayRowTable(table: TableLike): ArrayRowTable {
64
- if (table.shape === 'array-row-table') {
65
- return table;
66
- }
67
- const length = getTableLength(table);
68
- const data = new Array<unknown[]>(length);
69
- for (let rowIndex = 0; rowIndex < length; rowIndex++) {
70
- data[rowIndex] = getTableRowAsArray(table, rowIndex);
71
- }
72
- return {
73
- shape: 'array-row-table',
74
- schema: table.schema,
75
- data
76
- };
77
- }
78
-
79
- /** Convert any table into object row format *
80
- export function makeObjectRowTable(table: Table): ObjectRowTable {
81
- if (table.shape === 'object-row-table') {
82
- return table;
83
- }
84
- const length = getTableLength(table);
85
- const data = new Array<{[key: string]: unknown}>(length);
86
- for (let rowIndex = 0; rowIndex < length; rowIndex++) {
87
- data[rowIndex] = getTableRowAsObject(table, rowIndex);
88
- }
89
- return {
90
- shape: 'object-row-table',
91
- schema: table.schema,
92
- data
93
- };
94
- }
95
- */
@@ -1,33 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.convertToArrayRow = exports.convertToObjectRow = void 0;
4
- /** Convert an object row to an array row */
5
- function convertToObjectRow(arrayRow, headers) {
6
- if (!arrayRow) {
7
- throw new Error('null row');
8
- }
9
- if (!headers) {
10
- throw new Error('no headers');
11
- }
12
- const objectRow = {};
13
- for (let i = 0; i < headers.length; i++) {
14
- objectRow[headers[i]] = arrayRow[i];
15
- }
16
- return objectRow;
17
- }
18
- exports.convertToObjectRow = convertToObjectRow;
19
- /** Convert an object row to an array row */
20
- function convertToArrayRow(objectRow, headers) {
21
- if (!objectRow) {
22
- throw new Error('null row');
23
- }
24
- if (!headers) {
25
- throw new Error('no headers');
26
- }
27
- const arrayRow = new Array(headers.length);
28
- for (let i = 0; i < headers.length; i++) {
29
- arrayRow[i] = objectRow[headers[i]];
30
- }
31
- return arrayRow;
32
- }
33
- exports.convertToArrayRow = convertToArrayRow;
@@ -1,319 +0,0 @@
1
- "use strict";
2
- // loaders.gl, MIT license
3
- Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.makeObjectRowIterator = exports.makeArrayRowIterator = exports.makeRowIterator = exports.makeColumnarTable = exports.makeObjectRowTable = exports.makeArrayRowTable = exports.getTableRowAsArray = exports.getTableRowAsObject = exports.getTableColumnName = exports.getTableColumnIndex = exports.getTableRowShape = exports.getTableCellAt = exports.getTableCell = exports.getTableNumCols = exports.getTableLength = void 0;
5
- /**
6
- * Returns the length of the table (i.e. the number of rows)
7
- */
8
- function getTableLength(table) {
9
- switch (table.shape) {
10
- case 'array-row-table':
11
- case 'object-row-table':
12
- case 'geojson-row-table':
13
- return table.data.length;
14
- case 'arrow-table':
15
- return table.data.numRows;
16
- case 'columnar-table':
17
- for (const column of Object.values(table.data)) {
18
- return column.length || 0;
19
- }
20
- return 0;
21
- default:
22
- throw new Error('table');
23
- }
24
- }
25
- exports.getTableLength = getTableLength;
26
- /**
27
- * Returns the number of columns in the table
28
- * @throws Fails to deduce number of columns if the table has no schema and is empty
29
- */
30
- function getTableNumCols(table) {
31
- if (table.schema) {
32
- return table.schema.fields.length;
33
- }
34
- if (getTableLength(table) === 0) {
35
- throw new Error('empty table');
36
- }
37
- switch (table.shape) {
38
- case 'array-row-table':
39
- return table.data[0].length;
40
- case 'object-row-table':
41
- case 'geojson-row-table':
42
- return Object.keys(table.data[0]).length;
43
- case 'columnar-table':
44
- return Object.keys(table.data).length;
45
- case 'arrow-table':
46
- return table.data.numCols;
47
- default:
48
- throw new Error('table');
49
- }
50
- }
51
- exports.getTableNumCols = getTableNumCols;
52
- /** Get a table cell value at row index and column name */
53
- function getTableCell(table, rowIndex, columnName) {
54
- switch (table.shape) {
55
- case 'array-row-table':
56
- const columnIndex = getTableColumnIndex(table, columnName);
57
- return table.data[rowIndex][columnIndex];
58
- case 'object-row-table':
59
- case 'geojson-row-table':
60
- return table.data[rowIndex][columnName];
61
- case 'columnar-table':
62
- const column = table.data[columnName];
63
- return column[rowIndex];
64
- case 'arrow-table':
65
- const arrowColumnIndex = table.data.schema.fields.findIndex((field) => field.name === columnName);
66
- return table.data.getChildAt(arrowColumnIndex)?.get(rowIndex);
67
- default:
68
- throw new Error('todo');
69
- }
70
- }
71
- exports.getTableCell = getTableCell;
72
- /** Get a table cell value at row index and column name */
73
- function getTableCellAt(table, rowIndex, columnIndex) {
74
- switch (table.shape) {
75
- case 'array-row-table':
76
- return table.data[rowIndex][columnIndex];
77
- case 'object-row-table':
78
- case 'geojson-row-table':
79
- let columnName = getTableColumnName(table, columnIndex);
80
- return table.data[rowIndex][columnName];
81
- case 'columnar-table':
82
- columnName = getTableColumnName(table, columnIndex);
83
- const column = table.data[columnName];
84
- return column[rowIndex];
85
- case 'arrow-table':
86
- return table.data.getChildAt(columnIndex)?.get(rowIndex);
87
- default:
88
- throw new Error('todo');
89
- }
90
- }
91
- exports.getTableCellAt = getTableCellAt;
92
- /** Deduce the table row shape */
93
- function getTableRowShape(table) {
94
- switch (table.shape) {
95
- case 'array-row-table':
96
- case 'object-row-table':
97
- return table.shape;
98
- case 'geojson-row-table':
99
- return 'object-row-table';
100
- case 'columnar-table':
101
- default:
102
- throw new Error('Not a row table');
103
- }
104
- }
105
- exports.getTableRowShape = getTableRowShape;
106
- /** Get the index of a named table column. Requires the table to have a schema */
107
- function getTableColumnIndex(table, columnName) {
108
- const columnIndex = table.schema?.fields.findIndex((field) => field.name === columnName);
109
- if (columnIndex === undefined) {
110
- throw new Error(columnName);
111
- }
112
- return columnIndex;
113
- }
114
- exports.getTableColumnIndex = getTableColumnIndex;
115
- /** Get the name of a table column by index. Requires the table to have a schema */
116
- function getTableColumnName(table, columnIndex) {
117
- const columnName = table.schema?.fields[columnIndex]?.name;
118
- if (!columnName) {
119
- throw new Error(`${columnIndex}`);
120
- }
121
- return columnName;
122
- }
123
- exports.getTableColumnName = getTableColumnName;
124
- /**
125
- * Returns one row of the table in object format.
126
- * @param target Optional parameter will be used if needed to store the row. Can be reused between calls to improve performance
127
- * @returns an array representing the row. May be the original array in the row, a new object, or the target parameter
128
- */
129
- // eslint-disable-next-line complexity
130
- function getTableRowAsObject(table, rowIndex, target, copy) {
131
- switch (table.shape) {
132
- case 'object-row-table':
133
- return copy ? Object.fromEntries(Object.entries(table.data[rowIndex])) : table.data[rowIndex];
134
- case 'array-row-table':
135
- case 'geojson-row-table':
136
- if (table.schema) {
137
- const objectRow = target || {};
138
- for (let i = 0; i < table.schema.fields.length; i++) {
139
- objectRow[table.schema.fields[i].name] = table.data[rowIndex][i];
140
- }
141
- return objectRow;
142
- }
143
- throw new Error('no schema');
144
- case 'columnar-table':
145
- if (table.schema) {
146
- const objectRow = target || {};
147
- for (let i = 0; i < table.schema.fields.length; i++) {
148
- objectRow[table.schema.fields[i].name] =
149
- table.data[table.schema.fields[i].name][rowIndex];
150
- }
151
- return objectRow;
152
- }
153
- else {
154
- // eslint-disable-line no-else-return
155
- const objectRow = target || {};
156
- for (const [name, column] of Object.entries(table.data)) {
157
- objectRow[name] = column[rowIndex];
158
- }
159
- return objectRow;
160
- }
161
- case 'arrow-table':
162
- const objectRow = target || {};
163
- const row = table.data.get(rowIndex);
164
- const schema = table.data.schema;
165
- for (let i = 0; i < schema.fields.length; i++) {
166
- objectRow[schema.fields[i].name] = row?.[schema.fields[i].name];
167
- }
168
- return objectRow;
169
- default:
170
- throw new Error('shape');
171
- }
172
- }
173
- exports.getTableRowAsObject = getTableRowAsObject;
174
- /**
175
- * Returns one row of the table in array format.
176
- * @param target Optional parameter will be used if needed to store the row. Can be reused between calls to improve performance.
177
- * @returns an array representing the row. May be the original array in the row, a new object, or the target parameter
178
- */
179
- // eslint-disable-next-line complexity
180
- function getTableRowAsArray(table, rowIndex, target, copy) {
181
- switch (table.shape) {
182
- case 'array-row-table':
183
- return copy ? Array.from(table.data[rowIndex]) : table.data[rowIndex];
184
- case 'object-row-table':
185
- case 'geojson-row-table':
186
- if (table.schema) {
187
- const arrayRow = target || [];
188
- for (let i = 0; i < table.schema.fields.length; i++) {
189
- arrayRow[i] = table.data[rowIndex][table.schema.fields[i].name];
190
- }
191
- return arrayRow;
192
- }
193
- // Warning: just slap on the values, this risks mismatches between rows
194
- return Object.values(table.data[rowIndex]);
195
- case 'columnar-table':
196
- if (table.schema) {
197
- const arrayRow = target || [];
198
- for (let i = 0; i < table.schema.fields.length; i++) {
199
- arrayRow[i] = table.data[table.schema.fields[i].name][rowIndex];
200
- }
201
- return arrayRow;
202
- }
203
- else {
204
- // eslint-disable-line no-else-return
205
- const arrayRow = target || [];
206
- let i = 0;
207
- for (const column of Object.values(table.data)) {
208
- arrayRow[i] = column[rowIndex];
209
- i++;
210
- }
211
- return arrayRow;
212
- }
213
- case 'arrow-table':
214
- const arrayRow = target || [];
215
- const row = table.data.get(rowIndex);
216
- const schema = table.data.schema;
217
- for (let i = 0; i < schema.fields.length; i++) {
218
- arrayRow[i] = row?.[schema.fields[i].name];
219
- }
220
- return arrayRow;
221
- default:
222
- throw new Error('shape');
223
- }
224
- }
225
- exports.getTableRowAsArray = getTableRowAsArray;
226
- /** Convert any table into array row format */
227
- function makeArrayRowTable(table) {
228
- if (table.shape === 'array-row-table') {
229
- return table;
230
- }
231
- const length = getTableLength(table);
232
- const data = new Array(length);
233
- for (let rowIndex = 0; rowIndex < length; rowIndex++) {
234
- data[rowIndex] = getTableRowAsArray(table, rowIndex);
235
- }
236
- return {
237
- shape: 'array-row-table',
238
- schema: table.schema,
239
- data
240
- };
241
- }
242
- exports.makeArrayRowTable = makeArrayRowTable;
243
- /** Convert any table into object row format */
244
- function makeObjectRowTable(table) {
245
- if (table.shape === 'object-row-table') {
246
- return table;
247
- }
248
- const length = getTableLength(table);
249
- const data = new Array(length);
250
- for (let rowIndex = 0; rowIndex < length; rowIndex++) {
251
- data[rowIndex] = getTableRowAsObject(table, rowIndex);
252
- }
253
- return {
254
- shape: 'object-row-table',
255
- schema: table.schema,
256
- data
257
- };
258
- }
259
- exports.makeObjectRowTable = makeObjectRowTable;
260
- /** Convert any table into object row format */
261
- function makeColumnarTable(table) {
262
- if (table.shape === 'object-row-table') {
263
- return table;
264
- }
265
- const length = getTableLength(table);
266
- const data = new Array(length);
267
- for (let rowIndex = 0; rowIndex < length; rowIndex++) {
268
- data[rowIndex] = getTableRowAsObject(table, rowIndex);
269
- }
270
- return {
271
- shape: 'object-row-table',
272
- schema: table.schema,
273
- data
274
- };
275
- }
276
- exports.makeColumnarTable = makeColumnarTable;
277
- // Row Iterators
278
- /**
279
- * Iterate over table rows
280
- * @param table
281
- * @param shape
282
- */
283
- function* makeRowIterator(table, shape) {
284
- switch (shape) {
285
- case 'array-row-table':
286
- yield* makeArrayRowIterator(table);
287
- break;
288
- case 'object-row-table':
289
- yield* makeObjectRowIterator(table);
290
- break;
291
- default:
292
- throw new Error(`Unknown row type ${shape}`);
293
- }
294
- }
295
- exports.makeRowIterator = makeRowIterator;
296
- /**
297
- * Streaming processing: Iterate over table, yielding array rows
298
- * @param table
299
- * @param shape
300
- */
301
- function* makeArrayRowIterator(table, target = []) {
302
- const length = getTableLength(table);
303
- for (let rowIndex = 0; rowIndex < length; rowIndex++) {
304
- yield getTableRowAsArray(table, rowIndex, target);
305
- }
306
- }
307
- exports.makeArrayRowIterator = makeArrayRowIterator;
308
- /**
309
- * Streaming processing: Iterate over table, yielding object rows
310
- * @param table
311
- * @param shape
312
- */
313
- function* makeObjectRowIterator(table, target = {}) {
314
- const length = getTableLength(table);
315
- for (let rowIndex = 0; rowIndex < length; rowIndex++) {
316
- yield getTableRowAsObject(table, rowIndex, target);
317
- }
318
- }
319
- exports.makeObjectRowIterator = makeObjectRowIterator;
@@ -1,41 +0,0 @@
1
- "use strict";
2
- // loaders.gl, MIT license
3
- Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.makeColumnFromField = void 0;
5
- const data_type_1 = require("./data-type");
6
- function makeColumnFromField(field, length) {
7
- const ArrayType = (0, data_type_1.getArrayTypeFromDataType)(field.type, field.nullable);
8
- return new ArrayType(length);
9
- }
10
- exports.makeColumnFromField = makeColumnFromField;
11
- /*
12
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
13
- function deduceSchema(rows) {
14
- const row = rows[0];
15
-
16
- const schema = {};
17
- let i = 0;
18
- for (const columnName in row) {
19
- const value = row[columnName];
20
- switch (typeof value) {
21
- case 'number':
22
- case 'boolean':
23
- // TODO - booleans could be handled differently...
24
- schema[columnName] = {name: String(columnName), index: i, type: Float32Array};
25
- break;
26
-
27
- case 'object':
28
- schema[columnName] = {name: String(columnName), index: i, type: Array};
29
- break;
30
-
31
- case 'string':
32
- default:
33
- schema[columnName] = {name: String(columnName), index: i, type: Array};
34
- // We currently only handle numeric rows
35
- // TODO we could offer a function to map strings to numbers?
36
- }
37
- i++;
38
- }
39
- return schema;
40
- }
41
- */
@@ -1,85 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.deduceSchema = exports.deduceTableSchema = void 0;
4
- const data_type_1 = require("./data-type");
5
- /**
6
- * SCHEMA SUPPORT - AUTODEDUCTION
7
- * @param {*} table
8
- * @param {*} schema
9
- * @returns
10
- */
11
- function deduceTableSchema(table) {
12
- switch (table.shape) {
13
- case 'array-row-table':
14
- case 'object-row-table':
15
- return deduceSchemaFromRows(table.data);
16
- case 'columnar-table':
17
- return deduceSchemaFromColumns(table.data);
18
- case 'arrow-table':
19
- default:
20
- throw new Error('Deduce schema');
21
- }
22
- }
23
- exports.deduceTableSchema = deduceTableSchema;
24
- function deduceSchema(data) {
25
- return Array.isArray(data) ? deduceSchemaFromRows(data) : deduceSchemaFromColumns(data);
26
- }
27
- exports.deduceSchema = deduceSchema;
28
- /** Given an object with columnar arrays, try to deduce a schema */
29
- function deduceSchemaFromColumns(columnarTable) {
30
- const fields = [];
31
- for (const [columnName, column] of Object.entries(columnarTable)) {
32
- const field = deduceFieldFromColumn(column, columnName);
33
- fields.push(field);
34
- }
35
- return { fields, metadata: {} };
36
- }
37
- /** Given an array of rows, try to deduce a schema */
38
- function deduceSchemaFromRows(rowTable) {
39
- if (!rowTable.length) {
40
- throw new Error('deduce from empty table');
41
- }
42
- const fields = [];
43
- const row0 = rowTable[0];
44
- // TODO - fields can be nullable, false detection...
45
- // Could look at additional rows if nulls in first row
46
- // TODO - if array, column names will be numbers
47
- for (const [columnName, value] of Object.entries(row0)) {
48
- fields.push(deduceFieldFromValue(value, columnName));
49
- }
50
- return { fields, metadata: {} };
51
- }
52
- /** Given a column (i.e. array), attempt to deduce an appropriate `Field` */
53
- function deduceFieldFromColumn(column, name) {
54
- if (ArrayBuffer.isView(column)) {
55
- const type = (0, data_type_1.getDataTypeFromArray)(column);
56
- return {
57
- name,
58
- type: type.type || 'null',
59
- nullable: type.nullable
60
- // metadata: {}
61
- };
62
- }
63
- if (Array.isArray(column) && column.length > 0) {
64
- const value = column[0];
65
- const type = (0, data_type_1.getDataTypeFromValue)(value);
66
- // TODO - support nested schemas?
67
- return {
68
- name,
69
- type,
70
- nullable: true
71
- // metadata: {},
72
- };
73
- }
74
- throw new Error('empty table');
75
- }
76
- /** Given a value, attempt to deduce an appropriate `Field` */
77
- function deduceFieldFromValue(value, name) {
78
- const type = (0, data_type_1.getDataTypeFromValue)(value);
79
- return {
80
- name,
81
- type,
82
- nullable: true
83
- // metadata: {}
84
- };
85
- }
@@ -1,12 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.assert = void 0;
4
- // Replacement for the external assert method to reduce bundle size
5
- // Note: We don't use the second "message" argument in calling code,
6
- // so no need to support it here
7
- function assert(condition, message) {
8
- if (!condition) {
9
- throw new Error(message || 'loader assertion failed.');
10
- }
11
- }
12
- exports.assert = assert;