@loaders.gl/csv 3.1.0-alpha.4 → 3.1.0-beta.3
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/dist/bundle.d.ts +2 -0
- package/dist/bundle.d.ts.map +1 -0
- package/dist/bundle.js +1503 -0
- package/dist/csv-loader.d.ts +34 -0
- package/dist/csv-loader.d.ts.map +1 -0
- package/dist/csv-loader.js +267 -0
- package/dist/csv-writer.d.ts +1 -0
- package/dist/csv-writer.d.ts.map +1 -0
- package/dist/csv-writer.js +6 -0
- package/dist/es5/bundle.js +1 -1
- package/dist/es5/bundle.js.map +1 -1
- package/dist/es5/csv-loader.js +87 -137
- package/dist/es5/csv-loader.js.map +1 -1
- package/dist/es5/index.js +1 -1
- package/dist/es5/papaparse/async-iterator-streamer.js +16 -109
- package/dist/es5/papaparse/async-iterator-streamer.js.map +1 -1
- package/dist/{esm/libs → es5/papaparse}/papaparse.js +170 -404
- package/dist/es5/papaparse/papaparse.js.map +1 -0
- package/dist/esm/csv-loader.js +5 -5
- package/dist/esm/csv-loader.js.map +1 -1
- package/dist/esm/papaparse/async-iterator-streamer.js +1 -1
- package/dist/esm/papaparse/async-iterator-streamer.js.map +1 -1
- package/{src/libs → dist/esm/papaparse}/papaparse.js +162 -404
- package/dist/esm/papaparse/papaparse.js.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +5 -0
- package/dist/papaparse/async-iterator-streamer.d.ts +6 -0
- package/dist/papaparse/async-iterator-streamer.d.ts.map +1 -0
- package/dist/papaparse/async-iterator-streamer.js +63 -0
- package/dist/papaparse/papaparse.d.ts +30 -0
- package/dist/papaparse/papaparse.d.ts.map +1 -0
- package/dist/papaparse/papaparse.js +935 -0
- package/package.json +6 -6
- package/src/csv-loader.ts +2 -2
- package/src/{lib → papaparse}/async-iterator-streamer.ts +2 -2
- package/{dist/es5/libs/papaparse.js → src/papaparse/papaparse.ts} +48 -73
- package/dist/dist.min.js +0 -9
- package/dist/dist.min.js.map +0 -1
- package/dist/es5/lib/async-iterator-streamer.js +0 -140
- package/dist/es5/lib/async-iterator-streamer.js.map +0 -1
- package/dist/esm/lib/async-iterator-streamer.js +0 -35
- package/dist/esm/lib/async-iterator-streamer.js.map +0 -1
- package/src/papaparse/async-iterator-streamer.js +0 -71
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { LoaderWithParser, LoaderOptions } from '@loaders.gl/loader-utils';
|
|
2
|
+
import type { Batch } from '@loaders.gl/schema';
|
|
3
|
+
export declare type CSVLoaderOptions = LoaderOptions & {
|
|
4
|
+
csv?: {
|
|
5
|
+
shape?: 'array-row-table' | 'object-row-table' | 'columnar-table';
|
|
6
|
+
/** optimizes memory usage but increases parsing time. */
|
|
7
|
+
optimizeMemoryUsage?: boolean;
|
|
8
|
+
columnPrefix?: string;
|
|
9
|
+
header?: 'auto';
|
|
10
|
+
quoteChar?: string;
|
|
11
|
+
escapeChar?: string;
|
|
12
|
+
dynamicTyping?: boolean;
|
|
13
|
+
comments?: boolean;
|
|
14
|
+
skipEmptyLines?: boolean | 'greedy';
|
|
15
|
+
delimitersToGuess?: string[];
|
|
16
|
+
};
|
|
17
|
+
};
|
|
18
|
+
export declare const CSVLoader: {
|
|
19
|
+
id: string;
|
|
20
|
+
module: string;
|
|
21
|
+
name: string;
|
|
22
|
+
version: any;
|
|
23
|
+
extensions: string[];
|
|
24
|
+
mimeTypes: string[];
|
|
25
|
+
category: string;
|
|
26
|
+
parse: (arrayBuffer: ArrayBuffer, options?: CSVLoaderOptions | undefined) => Promise<any>;
|
|
27
|
+
parseText: (text: string, options?: CSVLoaderOptions | undefined) => Promise<any>;
|
|
28
|
+
parseInBatches: typeof parseCSVInBatches;
|
|
29
|
+
options: CSVLoaderOptions;
|
|
30
|
+
};
|
|
31
|
+
declare function parseCSVInBatches(asyncIterator: AsyncIterable<ArrayBuffer> | Iterable<ArrayBuffer>, options?: CSVLoaderOptions): AsyncIterable<Batch>;
|
|
32
|
+
export declare const _typecheckCSVLoader: LoaderWithParser;
|
|
33
|
+
export {};
|
|
34
|
+
//# sourceMappingURL=csv-loader.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"csv-loader.d.ts","sourceRoot":"","sources":["../src/csv-loader.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,gBAAgB,EAAE,aAAa,EAAC,MAAM,0BAA0B,CAAC;AAC9E,OAAO,KAAK,EAAC,KAAK,EAAC,MAAM,oBAAoB,CAAC;AAgB9C,oBAAY,gBAAgB,GAAG,aAAa,GAAG;IAC7C,GAAG,CAAC,EAAE;QAEJ,KAAK,CAAC,EAAE,iBAAiB,GAAG,kBAAkB,GAAG,gBAAgB,CAAC;QAClE,yDAAyD;QACzD,mBAAmB,CAAC,EAAE,OAAO,CAAC;QAC9B,YAAY,CAAC,EAAE,MAAM,CAAC;QACtB,MAAM,CAAC,EAAE,MAAM,CAAC;QAKhB,SAAS,CAAC,EAAE,MAAM,CAAC;QACnB,UAAU,CAAC,EAAE,MAAM,CAAC;QAEpB,aAAa,CAAC,EAAE,OAAO,CAAC;QACxB,QAAQ,CAAC,EAAE,OAAO,CAAC;QACnB,cAAc,CAAC,EAAE,OAAO,GAAG,QAAQ,CAAC;QAEpC,iBAAiB,CAAC,EAAE,MAAM,EAAE,CAAC;KAE9B,CAAC;CACH,CAAC;AAsBF,eAAO,MAAM,SAAS;;;;;;;;yBAQO,WAAW;sBAEpB,MAAM;;;CAKzB,CAAC;AAsDF,iBAAS,iBAAiB,CACxB,aAAa,EAAE,aAAa,CAAC,WAAW,CAAC,GAAG,QAAQ,CAAC,WAAW,CAAC,EACjE,OAAO,CAAC,EAAE,gBAAgB,GACzB,aAAa,CAAC,KAAK,CAAC,CAoHtB;AAgFD,eAAO,MAAM,mBAAmB,EAAE,gBAA4B,CAAC"}
|
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports._typecheckCSVLoader = exports.CSVLoader = void 0;
|
|
7
|
+
const schema_1 = require("@loaders.gl/schema");
|
|
8
|
+
const papaparse_1 = __importDefault(require("./papaparse/papaparse"));
|
|
9
|
+
const async_iterator_streamer_1 = __importDefault(require("./papaparse/async-iterator-streamer"));
|
|
10
|
+
// __VERSION__ is injected by babel-plugin-version-inline
|
|
11
|
+
// @ts-ignore TS2304: Cannot find name '__VERSION__'.
|
|
12
|
+
const VERSION = typeof __VERSION__ !== 'undefined' ? __VERSION__ : 'latest';
|
|
13
|
+
const DEFAULT_CSV_LOADER_OPTIONS = {
|
|
14
|
+
csv: {
|
|
15
|
+
shape: 'object-row-table',
|
|
16
|
+
optimizeMemoryUsage: false,
|
|
17
|
+
// CSV options
|
|
18
|
+
header: 'auto',
|
|
19
|
+
columnPrefix: 'column',
|
|
20
|
+
// delimiter: auto
|
|
21
|
+
// newline: auto
|
|
22
|
+
quoteChar: '"',
|
|
23
|
+
escapeChar: '"',
|
|
24
|
+
dynamicTyping: true,
|
|
25
|
+
comments: false,
|
|
26
|
+
skipEmptyLines: true,
|
|
27
|
+
// transform: null?
|
|
28
|
+
delimitersToGuess: [',', '\t', '|', ';']
|
|
29
|
+
// fastMode: auto
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
exports.CSVLoader = {
|
|
33
|
+
id: 'csv',
|
|
34
|
+
module: 'csv',
|
|
35
|
+
name: 'CSV',
|
|
36
|
+
version: VERSION,
|
|
37
|
+
extensions: ['csv', 'tsv', 'dsv'],
|
|
38
|
+
mimeTypes: ['text/csv', 'text/tab-separated-values', 'text/dsv'],
|
|
39
|
+
category: 'table',
|
|
40
|
+
parse: async (arrayBuffer, options) => parseCSV(new TextDecoder().decode(arrayBuffer), options),
|
|
41
|
+
parseText: (text, options) => parseCSV(text, options),
|
|
42
|
+
parseInBatches: parseCSVInBatches,
|
|
43
|
+
// @ts-ignore
|
|
44
|
+
// testText: null,
|
|
45
|
+
options: DEFAULT_CSV_LOADER_OPTIONS
|
|
46
|
+
};
|
|
47
|
+
async function parseCSV(csvText, options) {
|
|
48
|
+
// Apps can call the parse method directly, we so apply default options here
|
|
49
|
+
const csvOptions = { ...DEFAULT_CSV_LOADER_OPTIONS.csv, ...options?.csv };
|
|
50
|
+
const firstRow = readFirstRow(csvText);
|
|
51
|
+
const header = csvOptions.header === 'auto' ? isHeaderRow(firstRow) : Boolean(csvOptions.header);
|
|
52
|
+
const parseWithHeader = header;
|
|
53
|
+
const papaparseConfig = {
|
|
54
|
+
// dynamicTyping: true,
|
|
55
|
+
...csvOptions,
|
|
56
|
+
header: parseWithHeader,
|
|
57
|
+
download: false,
|
|
58
|
+
transformHeader: parseWithHeader ? duplicateColumnTransformer() : undefined,
|
|
59
|
+
error: (e) => {
|
|
60
|
+
throw new Error(e);
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
const result = papaparse_1.default.parse(csvText, papaparseConfig);
|
|
64
|
+
let { data: rows } = result;
|
|
65
|
+
const headerRow = result.meta.fields || generateHeader(csvOptions.columnPrefix, firstRow.length);
|
|
66
|
+
switch (csvOptions.shape) {
|
|
67
|
+
case 'object-row-table':
|
|
68
|
+
rows = rows.map((row) => (Array.isArray(row) ? (0, schema_1.convertToObjectRow)(row, headerRow) : row));
|
|
69
|
+
break;
|
|
70
|
+
case 'array-row-table':
|
|
71
|
+
rows = rows.map((row) => (Array.isArray(row) ? row : (0, schema_1.convertToArrayRow)(row, headerRow)));
|
|
72
|
+
break;
|
|
73
|
+
default:
|
|
74
|
+
}
|
|
75
|
+
/*
|
|
76
|
+
if (!header && shape === 'object-row-table') {
|
|
77
|
+
// If the dataset has no header, transform the array result into an object shape with an
|
|
78
|
+
// autogenerated header
|
|
79
|
+
return result.data.map((row) =>
|
|
80
|
+
row.reduce((acc, value, i) => {
|
|
81
|
+
acc[headerRow[i]] = value;
|
|
82
|
+
return acc;
|
|
83
|
+
}, {})
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
*/
|
|
87
|
+
return rows;
|
|
88
|
+
}
|
|
89
|
+
// TODO - support batch size 0 = no batching/single batch?
|
|
90
|
+
function parseCSVInBatches(asyncIterator, options) {
|
|
91
|
+
// Papaparse does not support standard batch size handling
|
|
92
|
+
// TODO - investigate papaparse chunks mode
|
|
93
|
+
options = { ...options };
|
|
94
|
+
if (options.batchSize === 'auto') {
|
|
95
|
+
options.batchSize = 4000;
|
|
96
|
+
}
|
|
97
|
+
// Apps can call the parse method directly, we so apply default options here
|
|
98
|
+
const csvOptions = { ...DEFAULT_CSV_LOADER_OPTIONS.csv, ...options?.csv };
|
|
99
|
+
const asyncQueue = new schema_1.AsyncQueue();
|
|
100
|
+
let isFirstRow = true;
|
|
101
|
+
let headerRow = null;
|
|
102
|
+
let tableBatchBuilder = null;
|
|
103
|
+
let schema = null;
|
|
104
|
+
const config = {
|
|
105
|
+
// dynamicTyping: true, // Convert numbers and boolean values in rows from strings,
|
|
106
|
+
...csvOptions,
|
|
107
|
+
header: false,
|
|
108
|
+
download: false,
|
|
109
|
+
// chunkSize is set to 5MB explicitly (same as Papaparse default) due to a bug where the
|
|
110
|
+
// streaming parser gets stuck if skipEmptyLines and a step callback are both supplied.
|
|
111
|
+
// See https://github.com/mholt/PapaParse/issues/465
|
|
112
|
+
chunkSize: 1024 * 1024 * 5,
|
|
113
|
+
// skipEmptyLines is set to a boolean value if supplied. Greedy is set to true
|
|
114
|
+
// skipEmptyLines is handled manually given two bugs where the streaming parser gets stuck if
|
|
115
|
+
// both of the skipEmptyLines and step callback options are provided:
|
|
116
|
+
// - true doesn't work unless chunkSize is set: https://github.com/mholt/PapaParse/issues/465
|
|
117
|
+
// - greedy doesn't work: https://github.com/mholt/PapaParse/issues/825
|
|
118
|
+
skipEmptyLines: false,
|
|
119
|
+
// step is called on every row
|
|
120
|
+
// eslint-disable-next-line complexity
|
|
121
|
+
step(results) {
|
|
122
|
+
let row = results.data;
|
|
123
|
+
if (csvOptions.skipEmptyLines) {
|
|
124
|
+
// Manually reject lines that are empty
|
|
125
|
+
const collapsedRow = row.flat().join('').trim();
|
|
126
|
+
if (collapsedRow === '') {
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
const bytesUsed = results.meta.cursor;
|
|
131
|
+
// Check if we need to save a header row
|
|
132
|
+
if (isFirstRow && !headerRow) {
|
|
133
|
+
// Auto detects or can be forced with csvOptions.header
|
|
134
|
+
const header = csvOptions.header === 'auto' ? isHeaderRow(row) : Boolean(csvOptions.header);
|
|
135
|
+
if (header) {
|
|
136
|
+
headerRow = row.map(duplicateColumnTransformer());
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
// If first data row, we can deduce the schema
|
|
141
|
+
if (isFirstRow) {
|
|
142
|
+
isFirstRow = false;
|
|
143
|
+
if (!headerRow) {
|
|
144
|
+
headerRow = generateHeader(csvOptions.columnPrefix, row.length);
|
|
145
|
+
}
|
|
146
|
+
schema = deduceSchema(row, headerRow);
|
|
147
|
+
}
|
|
148
|
+
if (csvOptions.optimizeMemoryUsage) {
|
|
149
|
+
// A workaround to allocate new strings and don't retain pointers to original strings.
|
|
150
|
+
// https://bugs.chromium.org/p/v8/issues/detail?id=2869
|
|
151
|
+
row = JSON.parse(JSON.stringify(row));
|
|
152
|
+
}
|
|
153
|
+
// Add the row
|
|
154
|
+
tableBatchBuilder =
|
|
155
|
+
tableBatchBuilder ||
|
|
156
|
+
new schema_1.TableBatchBuilder(schema, {
|
|
157
|
+
// @ts-expect-error
|
|
158
|
+
shape: csvOptions.shape || 'array-row-table',
|
|
159
|
+
...options
|
|
160
|
+
});
|
|
161
|
+
try {
|
|
162
|
+
tableBatchBuilder.addRow(row);
|
|
163
|
+
// If a batch has been completed, emit it
|
|
164
|
+
const batch = tableBatchBuilder && tableBatchBuilder.getFullBatch({ bytesUsed });
|
|
165
|
+
if (batch) {
|
|
166
|
+
asyncQueue.enqueue(batch);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
catch (error) {
|
|
170
|
+
asyncQueue.enqueue(error);
|
|
171
|
+
}
|
|
172
|
+
},
|
|
173
|
+
// complete is called when all rows have been read
|
|
174
|
+
complete(results) {
|
|
175
|
+
try {
|
|
176
|
+
const bytesUsed = results.meta.cursor;
|
|
177
|
+
// Ensure any final (partial) batch gets emitted
|
|
178
|
+
const batch = tableBatchBuilder && tableBatchBuilder.getFinalBatch({ bytesUsed });
|
|
179
|
+
if (batch) {
|
|
180
|
+
asyncQueue.enqueue(batch);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
catch (error) {
|
|
184
|
+
asyncQueue.enqueue(error);
|
|
185
|
+
}
|
|
186
|
+
asyncQueue.close();
|
|
187
|
+
}
|
|
188
|
+
};
|
|
189
|
+
papaparse_1.default.parse(asyncIterator, config, async_iterator_streamer_1.default);
|
|
190
|
+
// TODO - Does it matter if we return asyncIterable or asyncIterator
|
|
191
|
+
// return asyncQueue[Symbol.asyncIterator]();
|
|
192
|
+
return asyncQueue;
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Checks if a certain row is a header row
|
|
196
|
+
* @param row the row to check
|
|
197
|
+
* @returns true if the row looks like a header
|
|
198
|
+
*/
|
|
199
|
+
function isHeaderRow(row) {
|
|
200
|
+
return row && row.every((value) => typeof value === 'string');
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* Reads, parses, and returns the first row of a CSV text
|
|
204
|
+
* @param csvText the csv text to parse
|
|
205
|
+
* @returns the first row
|
|
206
|
+
*/
|
|
207
|
+
function readFirstRow(csvText) {
|
|
208
|
+
const result = papaparse_1.default.parse(csvText, {
|
|
209
|
+
download: false,
|
|
210
|
+
dynamicTyping: true,
|
|
211
|
+
preview: 1
|
|
212
|
+
});
|
|
213
|
+
return result.data[0];
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* Creates a transformer that renames duplicate columns. This is needed as Papaparse doesn't handle
|
|
217
|
+
* duplicate header columns and would use the latest occurrence by default.
|
|
218
|
+
* See the header option in https://www.papaparse.com/docs#config
|
|
219
|
+
* @returns a transform function that returns sanitized names for duplicate fields
|
|
220
|
+
*/
|
|
221
|
+
function duplicateColumnTransformer() {
|
|
222
|
+
const observedColumns = new Set();
|
|
223
|
+
return (col) => {
|
|
224
|
+
let colName = col;
|
|
225
|
+
let counter = 1;
|
|
226
|
+
while (observedColumns.has(colName)) {
|
|
227
|
+
colName = `${col}.${counter}`;
|
|
228
|
+
counter++;
|
|
229
|
+
}
|
|
230
|
+
observedColumns.add(colName);
|
|
231
|
+
return colName;
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* Generates the header of a CSV given a prefix and a column count
|
|
236
|
+
* @param columnPrefix the columnPrefix to use
|
|
237
|
+
* @param count the count of column names to generate
|
|
238
|
+
* @returns an array of column names
|
|
239
|
+
*/
|
|
240
|
+
function generateHeader(columnPrefix, count = 0) {
|
|
241
|
+
const headers = [];
|
|
242
|
+
for (let i = 0; i < count; i++) {
|
|
243
|
+
headers.push(`${columnPrefix}${i + 1}`);
|
|
244
|
+
}
|
|
245
|
+
return headers;
|
|
246
|
+
}
|
|
247
|
+
function deduceSchema(row, headerRow) {
|
|
248
|
+
const schema = headerRow ? {} : [];
|
|
249
|
+
for (let i = 0; i < row.length; i++) {
|
|
250
|
+
const columnName = (headerRow && headerRow[i]) || i;
|
|
251
|
+
const value = row[i];
|
|
252
|
+
switch (typeof value) {
|
|
253
|
+
case 'number':
|
|
254
|
+
case 'boolean':
|
|
255
|
+
// TODO - booleans could be handled differently...
|
|
256
|
+
schema[columnName] = { name: String(columnName), index: i, type: Float32Array };
|
|
257
|
+
break;
|
|
258
|
+
case 'string':
|
|
259
|
+
default:
|
|
260
|
+
schema[columnName] = { name: String(columnName), index: i, type: Array };
|
|
261
|
+
// We currently only handle numeric rows
|
|
262
|
+
// TODO we could offer a function to map strings to numbers?
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
return schema;
|
|
266
|
+
}
|
|
267
|
+
exports._typecheckCSVLoader = exports.CSVLoader;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
//# sourceMappingURL=csv-writer.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"csv-writer.d.ts","sourceRoot":"","sources":["../src/csv-writer.ts"],"names":[],"mappings":""}
|
package/dist/es5/bundle.js
CHANGED
package/dist/es5/bundle.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/bundle.ts"],"names":["moduleExports","require","globalThis","loaders","module","exports","Object","assign"],"mappings":";;AACA,
|
|
1
|
+
{"version":3,"sources":["../../src/bundle.ts"],"names":["moduleExports","require","globalThis","loaders","module","exports","Object","assign"],"mappings":";;AACA,MAAMA,aAAa,GAAGC,OAAO,CAAC,SAAD,CAA7B;;AACAC,UAAU,CAACC,OAAX,GAAqBD,UAAU,CAACC,OAAX,IAAsB,EAA3C;AACAC,MAAM,CAACC,OAAP,GAAiBC,MAAM,CAACC,MAAP,CAAcL,UAAU,CAACC,OAAzB,EAAkCH,aAAlC,CAAjB","sourcesContent":["// @ts-nocheck\nconst moduleExports = require('./index');\nglobalThis.loaders = globalThis.loaders || {};\nmodule.exports = Object.assign(globalThis.loaders, moduleExports);\n"],"file":"bundle.js"}
|
package/dist/es5/csv-loader.js
CHANGED
|
@@ -7,26 +7,14 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
7
7
|
});
|
|
8
8
|
exports._typecheckCSVLoader = exports.CSVLoader = void 0;
|
|
9
9
|
|
|
10
|
-
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
|
|
11
|
-
|
|
12
|
-
var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
|
|
13
|
-
|
|
14
|
-
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
15
|
-
|
|
16
|
-
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
|
|
17
|
-
|
|
18
10
|
var _schema = require("@loaders.gl/schema");
|
|
19
11
|
|
|
20
|
-
var _papaparse = _interopRequireDefault(require("./
|
|
21
|
-
|
|
22
|
-
var _asyncIteratorStreamer = _interopRequireDefault(require("./lib/async-iterator-streamer"));
|
|
12
|
+
var _papaparse = _interopRequireDefault(require("./papaparse/papaparse"));
|
|
23
13
|
|
|
24
|
-
|
|
14
|
+
var _asyncIteratorStreamer = _interopRequireDefault(require("./papaparse/async-iterator-streamer"));
|
|
25
15
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
var VERSION = typeof "3.1.0-alpha.4" !== 'undefined' ? "3.1.0-alpha.4" : 'latest';
|
|
29
|
-
var DEFAULT_CSV_LOADER_OPTIONS = {
|
|
16
|
+
const VERSION = typeof "3.1.0-beta.3" !== 'undefined' ? "3.1.0-beta.3" : 'latest';
|
|
17
|
+
const DEFAULT_CSV_LOADER_OPTIONS = {
|
|
30
18
|
csv: {
|
|
31
19
|
shape: 'object-row-table',
|
|
32
20
|
optimizeMemoryUsage: false,
|
|
@@ -40,7 +28,7 @@ var DEFAULT_CSV_LOADER_OPTIONS = {
|
|
|
40
28
|
delimitersToGuess: [',', '\t', '|', ';']
|
|
41
29
|
}
|
|
42
30
|
};
|
|
43
|
-
|
|
31
|
+
const CSVLoader = {
|
|
44
32
|
id: 'csv',
|
|
45
33
|
module: 'csv',
|
|
46
34
|
name: 'CSV',
|
|
@@ -48,128 +36,90 @@ var CSVLoader = {
|
|
|
48
36
|
extensions: ['csv', 'tsv', 'dsv'],
|
|
49
37
|
mimeTypes: ['text/csv', 'text/tab-separated-values', 'text/dsv'],
|
|
50
38
|
category: 'table',
|
|
51
|
-
parse:
|
|
52
|
-
|
|
53
|
-
return _regenerator.default.wrap(function _callee$(_context) {
|
|
54
|
-
while (1) {
|
|
55
|
-
switch (_context.prev = _context.next) {
|
|
56
|
-
case 0:
|
|
57
|
-
return _context.abrupt("return", parseCSV(new TextDecoder().decode(arrayBuffer), options));
|
|
58
|
-
|
|
59
|
-
case 1:
|
|
60
|
-
case "end":
|
|
61
|
-
return _context.stop();
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
}, _callee);
|
|
65
|
-
}));
|
|
66
|
-
|
|
67
|
-
function parse(_x, _x2) {
|
|
68
|
-
return _parse.apply(this, arguments);
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
return parse;
|
|
72
|
-
}(),
|
|
73
|
-
parseText: function parseText(text, options) {
|
|
74
|
-
return parseCSV(text, options);
|
|
75
|
-
},
|
|
39
|
+
parse: async (arrayBuffer, options) => parseCSV(new TextDecoder().decode(arrayBuffer), options),
|
|
40
|
+
parseText: (text, options) => parseCSV(text, options),
|
|
76
41
|
parseInBatches: parseCSVInBatches,
|
|
77
42
|
options: DEFAULT_CSV_LOADER_OPTIONS
|
|
78
43
|
};
|
|
79
44
|
exports.CSVLoader = CSVLoader;
|
|
80
45
|
|
|
81
|
-
function parseCSV(
|
|
82
|
-
|
|
83
|
-
|
|
46
|
+
async function parseCSV(csvText, options) {
|
|
47
|
+
const csvOptions = { ...DEFAULT_CSV_LOADER_OPTIONS.csv,
|
|
48
|
+
...(options === null || options === void 0 ? void 0 : options.csv)
|
|
49
|
+
};
|
|
50
|
+
const firstRow = readFirstRow(csvText);
|
|
51
|
+
const header = csvOptions.header === 'auto' ? isHeaderRow(firstRow) : Boolean(csvOptions.header);
|
|
52
|
+
const parseWithHeader = header;
|
|
53
|
+
const papaparseConfig = { ...csvOptions,
|
|
54
|
+
header: parseWithHeader,
|
|
55
|
+
download: false,
|
|
56
|
+
transformHeader: parseWithHeader ? duplicateColumnTransformer() : undefined,
|
|
57
|
+
error: e => {
|
|
58
|
+
throw new Error(e);
|
|
59
|
+
}
|
|
60
|
+
};
|
|
84
61
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
rows = result.data;
|
|
106
|
-
headerRow = result.meta.fields || generateHeader(csvOptions.columnPrefix, firstRow.length);
|
|
107
|
-
_context2.t0 = csvOptions.shape;
|
|
108
|
-
_context2.next = _context2.t0 === 'object-row-table' ? 11 : _context2.t0 === 'array-row-table' ? 13 : 15;
|
|
109
|
-
break;
|
|
110
|
-
|
|
111
|
-
case 11:
|
|
112
|
-
rows = rows.map(function (row) {
|
|
113
|
-
return Array.isArray(row) ? (0, _schema.convertToObjectRow)(row, headerRow) : row;
|
|
114
|
-
});
|
|
115
|
-
return _context2.abrupt("break", 15);
|
|
116
|
-
|
|
117
|
-
case 13:
|
|
118
|
-
rows = rows.map(function (row) {
|
|
119
|
-
return Array.isArray(row) ? row : (0, _schema.convertToArrayRow)(row, headerRow);
|
|
120
|
-
});
|
|
121
|
-
return _context2.abrupt("break", 15);
|
|
122
|
-
|
|
123
|
-
case 15:
|
|
124
|
-
return _context2.abrupt("return", rows);
|
|
125
|
-
|
|
126
|
-
case 16:
|
|
127
|
-
case "end":
|
|
128
|
-
return _context2.stop();
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
}, _callee2);
|
|
132
|
-
}));
|
|
133
|
-
return _parseCSV.apply(this, arguments);
|
|
62
|
+
const result = _papaparse.default.parse(csvText, papaparseConfig);
|
|
63
|
+
|
|
64
|
+
let {
|
|
65
|
+
data: rows
|
|
66
|
+
} = result;
|
|
67
|
+
const headerRow = result.meta.fields || generateHeader(csvOptions.columnPrefix, firstRow.length);
|
|
68
|
+
|
|
69
|
+
switch (csvOptions.shape) {
|
|
70
|
+
case 'object-row-table':
|
|
71
|
+
rows = rows.map(row => Array.isArray(row) ? (0, _schema.convertToObjectRow)(row, headerRow) : row);
|
|
72
|
+
break;
|
|
73
|
+
|
|
74
|
+
case 'array-row-table':
|
|
75
|
+
rows = rows.map(row => Array.isArray(row) ? row : (0, _schema.convertToArrayRow)(row, headerRow));
|
|
76
|
+
break;
|
|
77
|
+
|
|
78
|
+
default:
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return rows;
|
|
134
82
|
}
|
|
135
83
|
|
|
136
84
|
function parseCSVInBatches(asyncIterator, options) {
|
|
137
85
|
var _options;
|
|
138
86
|
|
|
139
|
-
options =
|
|
87
|
+
options = { ...options
|
|
88
|
+
};
|
|
140
89
|
|
|
141
90
|
if (options.batchSize === 'auto') {
|
|
142
91
|
options.batchSize = 4000;
|
|
143
92
|
}
|
|
144
93
|
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
94
|
+
const csvOptions = { ...DEFAULT_CSV_LOADER_OPTIONS.csv,
|
|
95
|
+
...((_options = options) === null || _options === void 0 ? void 0 : _options.csv)
|
|
96
|
+
};
|
|
97
|
+
const asyncQueue = new _schema.AsyncQueue();
|
|
98
|
+
let isFirstRow = true;
|
|
99
|
+
let headerRow = null;
|
|
100
|
+
let tableBatchBuilder = null;
|
|
101
|
+
let schema = null;
|
|
102
|
+
const config = { ...csvOptions,
|
|
154
103
|
header: false,
|
|
155
104
|
download: false,
|
|
156
105
|
chunkSize: 1024 * 1024 * 5,
|
|
157
106
|
skipEmptyLines: false,
|
|
158
|
-
|
|
159
|
-
|
|
107
|
+
|
|
108
|
+
step(results) {
|
|
109
|
+
let row = results.data;
|
|
160
110
|
|
|
161
111
|
if (csvOptions.skipEmptyLines) {
|
|
162
|
-
|
|
112
|
+
const collapsedRow = row.flat().join('').trim();
|
|
163
113
|
|
|
164
114
|
if (collapsedRow === '') {
|
|
165
115
|
return;
|
|
166
116
|
}
|
|
167
117
|
}
|
|
168
118
|
|
|
169
|
-
|
|
119
|
+
const bytesUsed = results.meta.cursor;
|
|
170
120
|
|
|
171
121
|
if (isFirstRow && !headerRow) {
|
|
172
|
-
|
|
122
|
+
const header = csvOptions.header === 'auto' ? isHeaderRow(row) : Boolean(csvOptions.header);
|
|
173
123
|
|
|
174
124
|
if (header) {
|
|
175
125
|
headerRow = row.map(duplicateColumnTransformer());
|
|
@@ -191,14 +141,15 @@ function parseCSVInBatches(asyncIterator, options) {
|
|
|
191
141
|
row = JSON.parse(JSON.stringify(row));
|
|
192
142
|
}
|
|
193
143
|
|
|
194
|
-
tableBatchBuilder = tableBatchBuilder || new _schema.TableBatchBuilder(schema,
|
|
195
|
-
shape: csvOptions.shape || 'array-row-table'
|
|
196
|
-
|
|
144
|
+
tableBatchBuilder = tableBatchBuilder || new _schema.TableBatchBuilder(schema, {
|
|
145
|
+
shape: csvOptions.shape || 'array-row-table',
|
|
146
|
+
...options
|
|
147
|
+
});
|
|
197
148
|
|
|
198
149
|
try {
|
|
199
150
|
tableBatchBuilder.addRow(row);
|
|
200
|
-
|
|
201
|
-
bytesUsed
|
|
151
|
+
const batch = tableBatchBuilder && tableBatchBuilder.getFullBatch({
|
|
152
|
+
bytesUsed
|
|
202
153
|
});
|
|
203
154
|
|
|
204
155
|
if (batch) {
|
|
@@ -208,11 +159,12 @@ function parseCSVInBatches(asyncIterator, options) {
|
|
|
208
159
|
asyncQueue.enqueue(error);
|
|
209
160
|
}
|
|
210
161
|
},
|
|
211
|
-
|
|
162
|
+
|
|
163
|
+
complete(results) {
|
|
212
164
|
try {
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
bytesUsed
|
|
165
|
+
const bytesUsed = results.meta.cursor;
|
|
166
|
+
const batch = tableBatchBuilder && tableBatchBuilder.getFinalBatch({
|
|
167
|
+
bytesUsed
|
|
216
168
|
});
|
|
217
169
|
|
|
218
170
|
if (batch) {
|
|
@@ -224,7 +176,8 @@ function parseCSVInBatches(asyncIterator, options) {
|
|
|
224
176
|
|
|
225
177
|
asyncQueue.close();
|
|
226
178
|
}
|
|
227
|
-
|
|
179
|
+
|
|
180
|
+
};
|
|
228
181
|
|
|
229
182
|
_papaparse.default.parse(asyncIterator, config, _asyncIteratorStreamer.default);
|
|
230
183
|
|
|
@@ -232,13 +185,11 @@ function parseCSVInBatches(asyncIterator, options) {
|
|
|
232
185
|
}
|
|
233
186
|
|
|
234
187
|
function isHeaderRow(row) {
|
|
235
|
-
return row && row.every(
|
|
236
|
-
return typeof value === 'string';
|
|
237
|
-
});
|
|
188
|
+
return row && row.every(value => typeof value === 'string');
|
|
238
189
|
}
|
|
239
190
|
|
|
240
191
|
function readFirstRow(csvText) {
|
|
241
|
-
|
|
192
|
+
const result = _papaparse.default.parse(csvText, {
|
|
242
193
|
download: false,
|
|
243
194
|
dynamicTyping: true,
|
|
244
195
|
preview: 1
|
|
@@ -248,10 +199,10 @@ function readFirstRow(csvText) {
|
|
|
248
199
|
}
|
|
249
200
|
|
|
250
201
|
function duplicateColumnTransformer() {
|
|
251
|
-
|
|
252
|
-
return
|
|
253
|
-
|
|
254
|
-
|
|
202
|
+
const observedColumns = new Set();
|
|
203
|
+
return col => {
|
|
204
|
+
let colName = col;
|
|
205
|
+
let counter = 1;
|
|
255
206
|
|
|
256
207
|
while (observedColumns.has(colName)) {
|
|
257
208
|
colName = "".concat(col, ".").concat(counter);
|
|
@@ -263,11 +214,10 @@ function duplicateColumnTransformer() {
|
|
|
263
214
|
};
|
|
264
215
|
}
|
|
265
216
|
|
|
266
|
-
function generateHeader(columnPrefix) {
|
|
267
|
-
|
|
268
|
-
var headers = [];
|
|
217
|
+
function generateHeader(columnPrefix, count = 0) {
|
|
218
|
+
const headers = [];
|
|
269
219
|
|
|
270
|
-
for (
|
|
220
|
+
for (let i = 0; i < count; i++) {
|
|
271
221
|
headers.push("".concat(columnPrefix).concat(i + 1));
|
|
272
222
|
}
|
|
273
223
|
|
|
@@ -275,13 +225,13 @@ function generateHeader(columnPrefix) {
|
|
|
275
225
|
}
|
|
276
226
|
|
|
277
227
|
function deduceSchema(row, headerRow) {
|
|
278
|
-
|
|
228
|
+
const schema = headerRow ? {} : [];
|
|
279
229
|
|
|
280
|
-
for (
|
|
281
|
-
|
|
282
|
-
|
|
230
|
+
for (let i = 0; i < row.length; i++) {
|
|
231
|
+
const columnName = headerRow && headerRow[i] || i;
|
|
232
|
+
const value = row[i];
|
|
283
233
|
|
|
284
|
-
switch (
|
|
234
|
+
switch (typeof value) {
|
|
285
235
|
case 'number':
|
|
286
236
|
case 'boolean':
|
|
287
237
|
schema[columnName] = {
|
|
@@ -304,6 +254,6 @@ function deduceSchema(row, headerRow) {
|
|
|
304
254
|
return schema;
|
|
305
255
|
}
|
|
306
256
|
|
|
307
|
-
|
|
257
|
+
const _typecheckCSVLoader = CSVLoader;
|
|
308
258
|
exports._typecheckCSVLoader = _typecheckCSVLoader;
|
|
309
259
|
//# sourceMappingURL=csv-loader.js.map
|