@loaders.gl/schema-utils 4.4.0-alpha.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.
Files changed (164) hide show
  1. package/LICENSE +41 -0
  2. package/README.md +9 -0
  3. package/dist/dist.dev.js +14269 -0
  4. package/dist/dist.min.js +18 -0
  5. package/dist/index.cjs +2436 -0
  6. package/dist/index.cjs.map +7 -0
  7. package/dist/index.d.ts +28 -0
  8. package/dist/index.d.ts.map +1 -0
  9. package/dist/index.js +73 -0
  10. package/dist/lib/arrow-utils/arrow-fixed-size-list-utils.d.ts +11 -0
  11. package/dist/lib/arrow-utils/arrow-fixed-size-list-utils.d.ts.map +1 -0
  12. package/dist/lib/arrow-utils/arrow-fixed-size-list-utils.js +40 -0
  13. package/dist/lib/arrow-utils/arrow-list-of-fixed-size-list-utils.d.ts +11 -0
  14. package/dist/lib/arrow-utils/arrow-list-of-fixed-size-list-utils.d.ts.map +1 -0
  15. package/dist/lib/arrow-utils/arrow-list-of-fixed-size-list-utils.js +32 -0
  16. package/dist/lib/mesh/convert-mesh-to-table.d.ts +18 -0
  17. package/dist/lib/mesh/convert-mesh-to-table.d.ts.map +1 -0
  18. package/dist/lib/mesh/convert-mesh-to-table.js +60 -0
  19. package/dist/lib/mesh/convert-table-to-mesh.d.ts +7 -0
  20. package/dist/lib/mesh/convert-table-to-mesh.d.ts.map +1 -0
  21. package/dist/lib/mesh/convert-table-to-mesh.js +40 -0
  22. package/dist/lib/mesh/deduce-mesh-schema.d.ts +23 -0
  23. package/dist/lib/mesh/deduce-mesh-schema.d.ts.map +1 -0
  24. package/dist/lib/mesh/deduce-mesh-schema.js +62 -0
  25. package/dist/lib/mesh/mesh-utils.d.ts +23 -0
  26. package/dist/lib/mesh/mesh-utils.d.ts.map +1 -0
  27. package/dist/lib/mesh/mesh-utils.js +46 -0
  28. package/dist/lib/schema/convert-arrow-schema.d.ts +23 -0
  29. package/dist/lib/schema/convert-arrow-schema.d.ts.map +1 -0
  30. package/dist/lib/schema/convert-arrow-schema.js +280 -0
  31. package/dist/lib/schema/data-type.d.ts +16 -0
  32. package/dist/lib/schema/data-type.d.ts.map +1 -0
  33. package/dist/lib/schema/data-type.js +88 -0
  34. package/dist/lib/schema/deduce-table-schema.d.ts +14 -0
  35. package/dist/lib/schema/deduce-table-schema.d.ts.map +1 -0
  36. package/dist/lib/schema/deduce-table-schema.js +100 -0
  37. package/dist/lib/table/arrow-api/arrow-like-field.d.ts +19 -0
  38. package/dist/lib/table/arrow-api/arrow-like-field.d.ts.map +1 -0
  39. package/dist/lib/table/arrow-api/arrow-like-field.js +36 -0
  40. package/dist/lib/table/arrow-api/arrow-like-interface.d.ts +43 -0
  41. package/dist/lib/table/arrow-api/arrow-like-interface.d.ts.map +1 -0
  42. package/dist/lib/table/arrow-api/arrow-like-interface.js +4 -0
  43. package/dist/lib/table/arrow-api/arrow-like-schema.d.ts +12 -0
  44. package/dist/lib/table/arrow-api/arrow-like-schema.d.ts.map +1 -0
  45. package/dist/lib/table/arrow-api/arrow-like-schema.js +81 -0
  46. package/dist/lib/table/arrow-api/arrow-like-table.d.ts +28 -0
  47. package/dist/lib/table/arrow-api/arrow-like-table.d.ts.map +1 -0
  48. package/dist/lib/table/arrow-api/arrow-like-table.js +63 -0
  49. package/dist/lib/table/arrow-api/arrow-like-type.d.ts +177 -0
  50. package/dist/lib/table/arrow-api/arrow-like-type.d.ts.map +1 -0
  51. package/dist/lib/table/arrow-api/arrow-like-type.js +442 -0
  52. package/dist/lib/table/arrow-api/enum.d.ts +92 -0
  53. package/dist/lib/table/arrow-api/enum.d.ts.map +1 -0
  54. package/dist/lib/table/arrow-api/enum.js +97 -0
  55. package/dist/lib/table/arrow-api/get-type-info.d.ts +13 -0
  56. package/dist/lib/table/arrow-api/get-type-info.d.ts.map +1 -0
  57. package/dist/lib/table/arrow-api/get-type-info.js +27 -0
  58. package/dist/lib/table/arrow-api/index.d.ts +5 -0
  59. package/dist/lib/table/arrow-api/index.d.ts.map +1 -0
  60. package/dist/lib/table/arrow-api/index.js +7 -0
  61. package/dist/lib/table/batch-builder/arrow-table-batch-aggregator.d.ts +10 -0
  62. package/dist/lib/table/batch-builder/arrow-table-batch-aggregator.d.ts.map +1 -0
  63. package/dist/lib/table/batch-builder/arrow-table-batch-aggregator.js +67 -0
  64. package/dist/lib/table/batch-builder/arrow-table-builder.d.ts +25 -0
  65. package/dist/lib/table/batch-builder/arrow-table-builder.d.ts.map +1 -0
  66. package/dist/lib/table/batch-builder/arrow-table-builder.js +99 -0
  67. package/dist/lib/table/batch-builder/base-table-batch-aggregator.d.ts +19 -0
  68. package/dist/lib/table/batch-builder/base-table-batch-aggregator.d.ts.map +1 -0
  69. package/dist/lib/table/batch-builder/base-table-batch-aggregator.js +63 -0
  70. package/dist/lib/table/batch-builder/columnar-table-batch-aggregator.d.ts +20 -0
  71. package/dist/lib/table/batch-builder/columnar-table-batch-aggregator.d.ts.map +1 -0
  72. package/dist/lib/table/batch-builder/columnar-table-batch-aggregator.js +83 -0
  73. package/dist/lib/table/batch-builder/row-table-batch-aggregator.d.ts +21 -0
  74. package/dist/lib/table/batch-builder/row-table-batch-aggregator.d.ts.map +1 -0
  75. package/dist/lib/table/batch-builder/row-table-batch-aggregator.js +85 -0
  76. package/dist/lib/table/batch-builder/table-batch-aggregator.d.ts +24 -0
  77. package/dist/lib/table/batch-builder/table-batch-aggregator.d.ts.map +1 -0
  78. package/dist/lib/table/batch-builder/table-batch-aggregator.js +4 -0
  79. package/dist/lib/table/batch-builder/table-batch-builder.d.ts +52 -0
  80. package/dist/lib/table/batch-builder/table-batch-builder.d.ts.map +1 -0
  81. package/dist/lib/table/batch-builder/table-batch-builder.js +148 -0
  82. package/dist/lib/table/batches/convert-batches.d.ts +10 -0
  83. package/dist/lib/table/batches/convert-batches.d.ts.map +1 -0
  84. package/dist/lib/table/batches/convert-batches.js +45 -0
  85. package/dist/lib/table/batches/make-arrow-batch-iterator.d.ts +17 -0
  86. package/dist/lib/table/batches/make-arrow-batch-iterator.d.ts.map +1 -0
  87. package/dist/lib/table/batches/make-arrow-batch-iterator.js +57 -0
  88. package/dist/lib/table/batches/make-table-batch-iterator.d.ts +15 -0
  89. package/dist/lib/table/batches/make-table-batch-iterator.d.ts.map +1 -0
  90. package/dist/lib/table/batches/make-table-batch-iterator.js +21 -0
  91. package/dist/lib/table/batches/make-table-from-batches.d.ts +9 -0
  92. package/dist/lib/table/batches/make-table-from-batches.d.ts.map +1 -0
  93. package/dist/lib/table/batches/make-table-from-batches.js +62 -0
  94. package/dist/lib/table/tables/convert-arrow-table.d.ts +24 -0
  95. package/dist/lib/table/tables/convert-arrow-table.d.ts.map +1 -0
  96. package/dist/lib/table/tables/convert-arrow-table.js +150 -0
  97. package/dist/lib/table/tables/convert-table.d.ts +43 -0
  98. package/dist/lib/table/tables/convert-table.d.ts.map +1 -0
  99. package/dist/lib/table/tables/convert-table.js +120 -0
  100. package/dist/lib/table/tables/make-table.d.ts +67 -0
  101. package/dist/lib/table/tables/make-table.d.ts.map +1 -0
  102. package/dist/lib/table/tables/make-table.js +101 -0
  103. package/dist/lib/table/tables/row-utils.d.ts +15 -0
  104. package/dist/lib/table/tables/row-utils.d.ts.map +1 -0
  105. package/dist/lib/table/tables/row-utils.js +49 -0
  106. package/dist/lib/table/tables/table-accessors.d.ts +68 -0
  107. package/dist/lib/table/tables/table-accessors.d.ts.map +1 -0
  108. package/dist/lib/table/tables/table-accessors.js +350 -0
  109. package/dist/lib/table/tables/table-column.d.ts +7 -0
  110. package/dist/lib/table/tables/table-column.d.ts.map +1 -0
  111. package/dist/lib/table/tables/table-column.js +39 -0
  112. package/dist/lib/table/tables/table-types.d.ts +12 -0
  113. package/dist/lib/table/tables/table-types.d.ts.map +1 -0
  114. package/dist/lib/table/tables/table-types.js +23 -0
  115. package/dist/lib/utils/assert.d.ts +2 -0
  116. package/dist/lib/utils/assert.d.ts.map +1 -0
  117. package/dist/lib/utils/assert.js +8 -0
  118. package/dist/lib/utils/async-queue.d.ts +18 -0
  119. package/dist/lib/utils/async-queue.d.ts.map +1 -0
  120. package/dist/lib/utils/async-queue.js +90 -0
  121. package/dist/types/types.d.ts +15 -0
  122. package/dist/types/types.d.ts.map +1 -0
  123. package/dist/types/types.js +4 -0
  124. package/package.json +52 -0
  125. package/src/index.ts +134 -0
  126. package/src/lib/arrow-utils/arrow-fixed-size-list-utils.ts +63 -0
  127. package/src/lib/arrow-utils/arrow-list-of-fixed-size-list-utils.ts +48 -0
  128. package/src/lib/mesh/convert-mesh-to-table.ts +77 -0
  129. package/src/lib/mesh/convert-table-to-mesh.ts +49 -0
  130. package/src/lib/mesh/deduce-mesh-schema.ts +75 -0
  131. package/src/lib/mesh/mesh-utils.ts +64 -0
  132. package/src/lib/schema/convert-arrow-schema.ts +301 -0
  133. package/src/lib/schema/data-type.ts +103 -0
  134. package/src/lib/schema/deduce-table-schema.ts +119 -0
  135. package/src/lib/table/arrow-api/arrow-like-field.ts +53 -0
  136. package/src/lib/table/arrow-api/arrow-like-interface.ts +84 -0
  137. package/src/lib/table/arrow-api/arrow-like-schema.ts +101 -0
  138. package/src/lib/table/arrow-api/arrow-like-table.ts +85 -0
  139. package/src/lib/table/arrow-api/arrow-like-type.ts +502 -0
  140. package/src/lib/table/arrow-api/enum.ts +99 -0
  141. package/src/lib/table/arrow-api/get-type-info.ts +38 -0
  142. package/src/lib/table/arrow-api/index.ts +8 -0
  143. package/src/lib/table/batch-builder/arrow-table-batch-aggregator-builders.ts.disabled +190 -0
  144. package/src/lib/table/batch-builder/arrow-table-batch-aggregator.ts +82 -0
  145. package/src/lib/table/batch-builder/arrow-table-builder.ts +112 -0
  146. package/src/lib/table/batch-builder/base-table-batch-aggregator.ts +82 -0
  147. package/src/lib/table/batch-builder/columnar-table-batch-aggregator.ts +99 -0
  148. package/src/lib/table/batch-builder/row-table-batch-aggregator.ts +108 -0
  149. package/src/lib/table/batch-builder/table-batch-aggregator.ts +28 -0
  150. package/src/lib/table/batch-builder/table-batch-builder.ts +185 -0
  151. package/src/lib/table/batches/convert-batches.ts +83 -0
  152. package/src/lib/table/batches/make-arrow-batch-iterator.ts +75 -0
  153. package/src/lib/table/batches/make-table-batch-iterator.ts +25 -0
  154. package/src/lib/table/batches/make-table-from-batches.ts +84 -0
  155. package/src/lib/table/tables/convert-arrow-table.ts +198 -0
  156. package/src/lib/table/tables/convert-table.ts +151 -0
  157. package/src/lib/table/tables/make-table.ts +127 -0
  158. package/src/lib/table/tables/row-utils.ts +59 -0
  159. package/src/lib/table/tables/table-accessors.ts +417 -0
  160. package/src/lib/table/tables/table-column.ts +49 -0
  161. package/src/lib/table/tables/table-types.ts +37 -0
  162. package/src/lib/utils/assert.ts +8 -0
  163. package/src/lib/utils/async-queue.ts +104 -0
  164. package/src/types/types.ts +45 -0
@@ -0,0 +1,417 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+
5
+ /* eslint-disable no-else-return */
6
+
7
+ import type {Table, ArrayRowTable, ObjectRowTable} from '@loaders.gl/schema';
8
+
9
+ export function isTable(table: any): table is Table {
10
+ const shape = typeof table === 'object' && table?.shape;
11
+ switch (shape) {
12
+ case 'array-row-table':
13
+ case 'object-row-table':
14
+ return Array.isArray(table.data);
15
+
16
+ case 'geojson-table':
17
+ return Array.isArray(table.features);
18
+
19
+ case 'columnar-table':
20
+ return table.data && typeof table.data === 'object';
21
+
22
+ case 'arrow-table':
23
+ return Boolean(table?.data?.numRows !== undefined);
24
+
25
+ default:
26
+ return false;
27
+ }
28
+ }
29
+
30
+ /**
31
+ * Returns the length of the table (i.e. the number of rows)
32
+ */
33
+ export function getTableLength(table: Table): number {
34
+ switch (table.shape) {
35
+ case 'array-row-table':
36
+ case 'object-row-table':
37
+ return table.data.length;
38
+
39
+ case 'geojson-table':
40
+ return table.features.length;
41
+
42
+ case 'arrow-table':
43
+ const arrowTable = table.data as any;
44
+ return arrowTable.numRows;
45
+
46
+ case 'columnar-table':
47
+ for (const column of Object.values(table.data)) {
48
+ return column.length || 0;
49
+ }
50
+ return 0;
51
+ default:
52
+ throw new Error('table');
53
+ }
54
+ }
55
+
56
+ /**
57
+ * Returns the number of columns in the table
58
+ * @throws Fails to deduce number of columns if the table has no schema and is empty
59
+ */
60
+ export function getTableNumCols(table: Table): number {
61
+ if (table.schema) {
62
+ return table.schema.fields.length;
63
+ }
64
+ if (getTableLength(table) === 0) {
65
+ throw new Error('empty table');
66
+ }
67
+ switch (table.shape) {
68
+ case 'array-row-table':
69
+ return table.data[0].length;
70
+ case 'object-row-table':
71
+ return Object.keys(table.data[0]).length;
72
+ case 'geojson-table':
73
+ return Object.keys(table.features[0]).length;
74
+ case 'columnar-table':
75
+ return Object.keys(table.data).length;
76
+ case 'arrow-table':
77
+ const arrowTable = table.data as any;
78
+ return arrowTable.numCols;
79
+ default:
80
+ throw new Error('table');
81
+ }
82
+ }
83
+
84
+ /** Get a table cell value at row index and column name */
85
+ export function getTableCell(table: Table, rowIndex: number, columnName: string): unknown {
86
+ switch (table.shape) {
87
+ case 'array-row-table':
88
+ const columnIndex = getTableColumnIndex(table, columnName);
89
+ return table.data[rowIndex][columnIndex];
90
+
91
+ case 'object-row-table':
92
+ return table.data[rowIndex][columnName];
93
+
94
+ case 'geojson-table':
95
+ return table.features[rowIndex][columnName];
96
+
97
+ case 'columnar-table':
98
+ const column = table.data[columnName];
99
+ return column[rowIndex];
100
+
101
+ case 'arrow-table':
102
+ const arrowTable = table.data as any;
103
+ const arrowColumnIndex = arrowTable.schema.fields.findIndex(
104
+ (field) => field.name === columnName
105
+ );
106
+ return arrowTable.getChildAt(arrowColumnIndex)?.get(rowIndex);
107
+
108
+ default:
109
+ throw new Error('todo');
110
+ }
111
+ }
112
+
113
+ /** Get a table cell value at row index and column name */
114
+ export function getTableCellAt(table: Table, rowIndex: number, columnIndex: number): unknown {
115
+ switch (table.shape) {
116
+ case 'array-row-table':
117
+ return table.data[rowIndex][columnIndex];
118
+
119
+ case 'object-row-table':
120
+ const columnName1 = getTableColumnName(table, columnIndex);
121
+ return table.data[rowIndex][columnName1];
122
+
123
+ case 'geojson-table':
124
+ const columnName2 = getTableColumnName(table, columnIndex);
125
+ return table.features[rowIndex][columnName2];
126
+
127
+ case 'columnar-table':
128
+ const columnName3 = getTableColumnName(table, columnIndex);
129
+ const column = table.data[columnName3];
130
+ return column[rowIndex];
131
+
132
+ case 'arrow-table':
133
+ const arrowTable = table.data as any;
134
+ return arrowTable.getChildAt(columnIndex)?.get(rowIndex);
135
+
136
+ default:
137
+ throw new Error('todo');
138
+ }
139
+ }
140
+
141
+ /** Deduce the table row shape */
142
+ export function getTableRowShape(table: Table): 'array-row-table' | 'object-row-table' {
143
+ switch (table.shape) {
144
+ case 'array-row-table':
145
+ case 'object-row-table':
146
+ return table.shape;
147
+
148
+ case 'geojson-table':
149
+ // TODO - this is not correct, geojson-table is not a row table
150
+ return 'object-row-table';
151
+
152
+ case 'columnar-table':
153
+ default:
154
+ throw new Error('Not a row table');
155
+ }
156
+ }
157
+
158
+ /** Get the index of a named table column. Requires the table to have a schema */
159
+ export function getTableColumnIndex(table: Table, columnName: string): number {
160
+ const columnIndex = table.schema?.fields.findIndex((field) => field.name === columnName);
161
+ if (columnIndex === undefined) {
162
+ throw new Error(columnName);
163
+ }
164
+ return columnIndex;
165
+ }
166
+
167
+ /** Get the name of a table column by index. Requires the table to have a schema */
168
+ export function getTableColumnName(table: Table, columnIndex: number): string {
169
+ const columnName = table.schema?.fields[columnIndex]?.name;
170
+ if (!columnName) {
171
+ throw new Error(`${columnIndex}`);
172
+ }
173
+ return columnName;
174
+ }
175
+
176
+ /**
177
+ * Returns one row of the table in object format.
178
+ * @param target Optional parameter will be used if needed to store the row. Can be reused between calls to improve performance
179
+ * @returns an array representing the row. May be the original array in the row, a new object, or the target parameter
180
+ */
181
+ // eslint-disable-next-line complexity
182
+ export function getTableRowAsObject(
183
+ table: Table,
184
+ rowIndex: number,
185
+ target?: {[columnName: string]: unknown},
186
+ copy?: 'copy'
187
+ ): {[columnName: string]: unknown} {
188
+ switch (table.shape) {
189
+ case 'object-row-table':
190
+ return copy ? Object.fromEntries(Object.entries(table.data[rowIndex])) : table.data[rowIndex];
191
+
192
+ case 'array-row-table':
193
+ if (table.schema) {
194
+ const objectRow: {[columnName: string]: unknown} = target || {};
195
+ for (let i = 0; i < table.schema.fields.length; i++) {
196
+ objectRow[table.schema.fields[i].name] = table.data[rowIndex][i];
197
+ }
198
+ return objectRow;
199
+ }
200
+ throw new Error('no schema');
201
+
202
+ case 'geojson-table':
203
+ if (table.schema) {
204
+ const objectRow: {[columnName: string]: unknown} = target || {};
205
+ // TODO - should lift properties to top level
206
+ for (let i = 0; i < table.schema.fields.length; i++) {
207
+ objectRow[table.schema.fields[i].name] = table.features[rowIndex][i];
208
+ }
209
+ return objectRow;
210
+ }
211
+ throw new Error('no schema');
212
+
213
+ case 'columnar-table':
214
+ if (table.schema) {
215
+ const objectRow: {[columnName: string]: unknown} = target || {};
216
+ for (let i = 0; i < table.schema.fields.length; i++) {
217
+ objectRow[table.schema.fields[i].name] =
218
+ table.data[table.schema.fields[i].name][rowIndex];
219
+ }
220
+ return objectRow;
221
+ } else {
222
+ // eslint-disable-line no-else-return
223
+ const objectRow: {[columnName: string]: unknown} = target || {};
224
+ for (const [name, column] of Object.entries(table.data)) {
225
+ objectRow[name] = column[rowIndex];
226
+ }
227
+ return objectRow;
228
+ }
229
+
230
+ case 'arrow-table':
231
+ const arrowTable = table.data as any;
232
+ const objectRow: {[columnName: string]: unknown} = target || {};
233
+ const row = arrowTable.get(rowIndex);
234
+ const schema = arrowTable.schema;
235
+ for (let i = 0; i < schema.fields.length; i++) {
236
+ objectRow[schema.fields[i].name] = row?.[schema.fields[i].name];
237
+ }
238
+ return objectRow;
239
+
240
+ default:
241
+ throw new Error('shape');
242
+ }
243
+ }
244
+
245
+ /**
246
+ * Returns one row of the table in array format.
247
+ * @param target Optional parameter will be used if needed to store the row. Can be reused between calls to improve performance.
248
+ * @returns an array representing the row. May be the original array in the row, a new object, or the target parameter
249
+ */
250
+ // eslint-disable-next-line complexity
251
+ export function getTableRowAsArray(
252
+ table: Table,
253
+ rowIndex: number,
254
+ target?: unknown[],
255
+ copy?: 'copy'
256
+ ): unknown[] {
257
+ switch (table.shape) {
258
+ case 'array-row-table':
259
+ return copy ? Array.from(table.data[rowIndex]) : table.data[rowIndex];
260
+
261
+ case 'object-row-table':
262
+ if (table.schema) {
263
+ const arrayRow: unknown[] = target || [];
264
+ for (let i = 0; i < table.schema.fields.length; i++) {
265
+ arrayRow[i] = table.data[rowIndex][table.schema.fields[i].name];
266
+ }
267
+ return arrayRow;
268
+ }
269
+ // Warning: just slap on the values, this risks mismatches between rows
270
+ return Object.values(table.data[rowIndex]);
271
+
272
+ case 'geojson-table':
273
+ if (table.schema) {
274
+ const arrayRow: unknown[] = target || [];
275
+ // TODO - should lift properties to top level
276
+ for (let i = 0; i < table.schema.fields.length; i++) {
277
+ arrayRow[i] = table.features[rowIndex][table.schema.fields[i].name];
278
+ }
279
+ return arrayRow;
280
+ }
281
+ // Warning: just slap on the values, this risks mismatches between rows
282
+ return Object.values(table.features[rowIndex]);
283
+
284
+ case 'columnar-table':
285
+ if (table.schema) {
286
+ const arrayRow: unknown[] = target || [];
287
+ for (let i = 0; i < table.schema.fields.length; i++) {
288
+ arrayRow[i] = table.data[table.schema.fields[i].name][rowIndex];
289
+ }
290
+ return arrayRow;
291
+ } else {
292
+ // eslint-disable-line no-else-return
293
+ const arrayRow: unknown[] = target || [];
294
+ let i = 0;
295
+ for (const column of Object.values(table.data)) {
296
+ arrayRow[i] = column[rowIndex];
297
+ i++;
298
+ }
299
+ return arrayRow;
300
+ }
301
+
302
+ case 'arrow-table':
303
+ const arrowTable = table.data as any;
304
+ const arrayRow: unknown[] = target || [];
305
+ const row = arrowTable.get(rowIndex);
306
+ const schema = arrowTable.schema;
307
+ for (let i = 0; i < schema.fields.length; i++) {
308
+ arrayRow[i] = row?.[schema.fields[i].name];
309
+ }
310
+ return arrayRow;
311
+
312
+ default:
313
+ throw new Error('shape');
314
+ }
315
+ }
316
+
317
+ /** Convert any table into array row format */
318
+ export function makeArrayRowTable(table: Table): ArrayRowTable {
319
+ if (table.shape === 'array-row-table') {
320
+ return table;
321
+ }
322
+ const length = getTableLength(table);
323
+ const data = new Array<unknown[]>(length);
324
+ for (let rowIndex = 0; rowIndex < length; rowIndex++) {
325
+ data[rowIndex] = getTableRowAsArray(table, rowIndex);
326
+ }
327
+ return {
328
+ shape: 'array-row-table',
329
+ schema: table.schema,
330
+ data
331
+ };
332
+ }
333
+
334
+ /** Convert any table into object row format */
335
+ export function makeObjectRowTable(table: Table): ObjectRowTable {
336
+ if (table.shape === 'object-row-table') {
337
+ return table;
338
+ }
339
+ const length = getTableLength(table);
340
+ const data = new Array<{[key: string]: unknown}>(length);
341
+ for (let rowIndex = 0; rowIndex < length; rowIndex++) {
342
+ data[rowIndex] = getTableRowAsObject(table, rowIndex);
343
+ }
344
+ return {
345
+ shape: 'object-row-table',
346
+ schema: table.schema,
347
+ data
348
+ };
349
+ }
350
+
351
+ /** Convert any table into object row format */
352
+ export function makeColumnarTable(table: Table): ObjectRowTable {
353
+ if (table.shape === 'object-row-table') {
354
+ return table;
355
+ }
356
+ const length = getTableLength(table);
357
+ const data = new Array<{[key: string]: unknown}>(length);
358
+ for (let rowIndex = 0; rowIndex < length; rowIndex++) {
359
+ data[rowIndex] = getTableRowAsObject(table, rowIndex);
360
+ }
361
+ return {
362
+ shape: 'object-row-table',
363
+ schema: table.schema,
364
+ data
365
+ };
366
+ }
367
+
368
+ // Row Iterators
369
+
370
+ /**
371
+ * Iterate over table rows
372
+ * @param table
373
+ * @param shape
374
+ */
375
+ export function* makeRowIterator(
376
+ table: Table,
377
+ shape: 'object-row-table' | 'array-row-table'
378
+ ): Iterable<unknown[] | {[key: string]: unknown}> {
379
+ switch (shape) {
380
+ case 'array-row-table':
381
+ yield* makeArrayRowIterator(table);
382
+ break;
383
+ case 'object-row-table':
384
+ yield* makeObjectRowIterator(table);
385
+ break;
386
+
387
+ default:
388
+ throw new Error(`Unknown row type ${shape}`);
389
+ }
390
+ }
391
+
392
+ /**
393
+ * Streaming processing: Iterate over table, yielding array rows
394
+ * @param table
395
+ * @param shape
396
+ */
397
+ export function* makeArrayRowIterator(table: Table, target: unknown[] = []): Iterable<unknown[]> {
398
+ const length = getTableLength(table);
399
+ for (let rowIndex = 0; rowIndex < length; rowIndex++) {
400
+ yield getTableRowAsArray(table, rowIndex, target);
401
+ }
402
+ }
403
+
404
+ /**
405
+ * Streaming processing: Iterate over table, yielding object rows
406
+ * @param table
407
+ * @param shape
408
+ */
409
+ export function* makeObjectRowIterator(
410
+ table: Table,
411
+ target: {[key: string]: unknown} = {}
412
+ ): Iterable<{[key: string]: unknown}> {
413
+ const length = getTableLength(table);
414
+ for (let rowIndex = 0; rowIndex < length; rowIndex++) {
415
+ yield getTableRowAsObject(table, rowIndex, target);
416
+ }
417
+ }
@@ -0,0 +1,49 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+
5
+ // import type {TypedArray,} from '../../../types/types';
6
+ import type {Field} from '@loaders.gl/schema';
7
+ import {getArrayTypeFromDataType} from '../../schema/data-type';
8
+
9
+ export interface ArrayType<T = unknown> {
10
+ readonly length: number;
11
+ [n: number]: T;
12
+ }
13
+
14
+ export function makeColumnFromField(field: Field, length: number): ArrayType {
15
+ const ArrayType = getArrayTypeFromDataType(field.type, field.nullable);
16
+ return new ArrayType(length);
17
+ }
18
+
19
+ /*
20
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
21
+ function deduceSchema(rows) {
22
+ const row = rows[0];
23
+
24
+ const schema = {};
25
+ let i = 0;
26
+ for (const columnName in row) {
27
+ const value = row[columnName];
28
+ switch (typeof value) {
29
+ case 'number':
30
+ case 'boolean':
31
+ // TODO - booleans could be handled differently...
32
+ schema[columnName] = {name: String(columnName), index: i, type: Float32Array};
33
+ break;
34
+
35
+ case 'object':
36
+ schema[columnName] = {name: String(columnName), index: i, type: Array};
37
+ break;
38
+
39
+ case 'string':
40
+ default:
41
+ schema[columnName] = {name: String(columnName), index: i, type: Array};
42
+ // We currently only handle numeric rows
43
+ // TODO we could offer a function to map strings to numbers?
44
+ }
45
+ i++;
46
+ }
47
+ return schema;
48
+ }
49
+ */
@@ -0,0 +1,37 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+
5
+ import type {
6
+ Table,
7
+ ObjectRowTable,
8
+ ArrayRowTable,
9
+ ColumnarTable,
10
+ GeoJSONTable,
11
+ ArrowTable
12
+ } from '@loaders.gl/schema';
13
+
14
+ /** Checks if a table is of array row layout */
15
+ export function isArrayRowTable(table: Table): table is ArrayRowTable {
16
+ return table.shape === 'array-row-table';
17
+ }
18
+
19
+ /** Checks if a table is of array row layout */
20
+ export function isObjectRowTable(table: Table): table is ObjectRowTable {
21
+ return table.shape === 'object-row-table';
22
+ }
23
+
24
+ /** Checks if a table is of columnar layout */
25
+ export function isColumnarTable(table: Table): table is ColumnarTable {
26
+ return table.shape === 'columnar-table';
27
+ }
28
+
29
+ /** Checks if a table is of GeoJSON format */
30
+ export function isGeoJSONTable(table: Table): table is GeoJSONTable {
31
+ return table.shape === 'geojson-table';
32
+ }
33
+
34
+ /** Checks if table wraps an Apache Arrow table */
35
+ export function isArrowTable(table: Table): table is ArrowTable {
36
+ return table.shape === 'arrow-table';
37
+ }
@@ -0,0 +1,8 @@
1
+ // Replacement for the external assert method to reduce bundle size
2
+ // Note: We don't use the second "message" argument in calling code,
3
+ // so no need to support it here
4
+ export function assert(condition: boolean, message?: string) {
5
+ if (!condition) {
6
+ throw new Error(message || 'loader assertion failed.');
7
+ }
8
+ }
@@ -0,0 +1,104 @@
1
+ // From https://github.com/rauschma/async-iter-demo/tree/master/src under MIT license
2
+ // http://2ality.com/2016/10/asynchronous-iteration.html
3
+
4
+ class ArrayQueue<T> extends Array<T> {
5
+ enqueue(value: T) {
6
+ // Add at the end
7
+ return this.push(value);
8
+ }
9
+ dequeue(): T {
10
+ // Remove first element
11
+ return this.shift() as T;
12
+ }
13
+ }
14
+
15
+ export default class AsyncQueue<T> {
16
+ private _values: ArrayQueue<T | Error>;
17
+ private _settlers: ArrayQueue<{resolve: (value: any) => void; reject: (reason?: any) => void}>;
18
+ private _closed: boolean;
19
+
20
+ constructor() {
21
+ // enqueues > dequeues
22
+ this._values = new ArrayQueue<T>();
23
+ // dequeues > enqueues
24
+ this._settlers = new ArrayQueue<{
25
+ resolve: (value: any) => void;
26
+ reject: (reason?: any) => void;
27
+ }>();
28
+ this._closed = false;
29
+ }
30
+
31
+ close(): void {
32
+ while (this._settlers.length > 0) {
33
+ this._settlers.dequeue().resolve({done: true});
34
+ }
35
+ this._closed = true;
36
+ }
37
+
38
+ [Symbol.asyncIterator](): AsyncIterator<T> {
39
+ return this;
40
+ }
41
+
42
+ enqueue(value: T | Error): void {
43
+ if (this._closed) {
44
+ throw new Error('Closed');
45
+ }
46
+
47
+ if (this._settlers.length > 0) {
48
+ if (this._values.length > 0) {
49
+ throw new Error('Illegal internal state');
50
+ }
51
+ const settler = this._settlers.dequeue();
52
+ if (value instanceof Error) {
53
+ settler.reject(value);
54
+ } else {
55
+ settler.resolve({value});
56
+ }
57
+ } else {
58
+ this._values.enqueue(value);
59
+ }
60
+ }
61
+
62
+ /**
63
+ * @returns a Promise for an IteratorResult
64
+ */
65
+ next(): Promise<any> {
66
+ if (this._values.length > 0) {
67
+ const value = this._values.dequeue();
68
+ if (value instanceof Error) {
69
+ return Promise.reject(value);
70
+ }
71
+ return Promise.resolve({value});
72
+ }
73
+
74
+ if (this._closed) {
75
+ if (this._settlers.length > 0) {
76
+ throw new Error('Illegal internal state');
77
+ }
78
+ return Promise.resolve({done: true});
79
+ }
80
+ // Wait for new values to be enqueued
81
+ return new Promise((resolve, reject) => {
82
+ this._settlers.enqueue({resolve, reject});
83
+ });
84
+ }
85
+ }
86
+
87
+ /**
88
+ * @returns a Promise for an Array with the elements in `asyncIterable`
89
+ */
90
+ export async function takeAsync(
91
+ asyncIterable: AsyncIterable<any>,
92
+ count = Infinity
93
+ ): Promise<any[]> {
94
+ const result: Array<any> = [];
95
+ const iterator = asyncIterable[Symbol.asyncIterator]();
96
+ while (result.length < count) {
97
+ const {value, done} = await iterator.next();
98
+ if (done) {
99
+ break;
100
+ }
101
+ result.push(value);
102
+ }
103
+ return result;
104
+ }
@@ -0,0 +1,45 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+
5
+ /** Any typed array */
6
+ export type TypedArray =
7
+ | Int8Array
8
+ | Uint8Array
9
+ | Int16Array
10
+ | Uint16Array
11
+ | Int32Array
12
+ | Uint32Array
13
+ | Uint8ClampedArray
14
+ | Float32Array
15
+ | Float64Array;
16
+
17
+ export type BigTypedArray = TypedArray | BigInt64Array | BigUint64Array;
18
+
19
+ export type TypedArrayConstructor =
20
+ | Int8ArrayConstructor
21
+ | Uint8ArrayConstructor
22
+ | Int16ArrayConstructor
23
+ | Uint16ArrayConstructor
24
+ | Int32ArrayConstructor
25
+ | Uint32ArrayConstructor
26
+ | Float32ArrayConstructor
27
+ | Float64ArrayConstructor;
28
+
29
+ export type BigTypedArrayConstructor =
30
+ | TypedArrayConstructor
31
+ | BigInt64ArrayConstructor
32
+ | BigUint64ArrayConstructor;
33
+
34
+ /** Any numeric array: typed array or `number[]` */
35
+ export type NumberArray = number[] | TypedArray;
36
+
37
+ export type NumericArray = number[] | TypedArray;
38
+
39
+ export interface ArrayType<T = unknown> {
40
+ readonly length: number;
41
+ [n: number]: T;
42
+ }
43
+
44
+ /** Any array: typed array or js array (`any[]`) */
45
+ export type AnyArray = any[] | TypedArray;