@loaders.gl/csv 3.3.0-alpha.5 → 3.3.0-alpha.6
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/csv-loader.d.ts +2 -2
- package/dist/csv-loader.d.ts.map +1 -1
- package/dist/es5/bundle.js +0 -1
- package/dist/es5/bundle.js.map +1 -1
- package/dist/es5/csv-loader.js +4 -50
- package/dist/es5/csv-loader.js.map +1 -1
- package/dist/es5/csv-writer.js +2 -0
- package/dist/es5/csv-writer.js.map +1 -1
- package/dist/es5/index.js +0 -1
- package/dist/es5/index.js.map +1 -1
- package/dist/es5/papaparse/async-iterator-streamer.js +34 -68
- package/dist/es5/papaparse/async-iterator-streamer.js.map +1 -1
- package/dist/es5/papaparse/papaparse.js +40 -146
- package/dist/es5/papaparse/papaparse.js.map +1 -1
- package/dist/esm/bundle.js +1 -1
- package/dist/esm/bundle.js.map +1 -1
- package/dist/esm/csv-loader.js +16 -36
- package/dist/esm/csv-loader.js.map +1 -1
- package/dist/esm/csv-writer.js.map +1 -1
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/papaparse/async-iterator-streamer.js +4 -3
- package/dist/esm/papaparse/async-iterator-streamer.js.map +1 -1
- package/dist/esm/papaparse/papaparse.js +41 -143
- package/dist/esm/papaparse/papaparse.js.map +1 -1
- package/package.json +4 -4
|
@@ -1,9 +1,11 @@
|
|
|
1
|
+
|
|
1
2
|
/* @license
|
|
2
3
|
Papa Parse
|
|
3
4
|
v5.0.0-beta.0
|
|
4
5
|
https://github.com/mholt/PapaParse
|
|
5
6
|
License: MIT
|
|
6
7
|
*/
|
|
8
|
+
|
|
7
9
|
const BYTE_ORDER_MARK = '\ufeff';
|
|
8
10
|
const Papa = {
|
|
9
11
|
parse: CsvToJson,
|
|
@@ -17,6 +19,7 @@ const Papa = {
|
|
|
17
19
|
LocalChunkSize: 1024 * 1024 * 10,
|
|
18
20
|
RemoteChunkSize: 1024 * 1024 * 5,
|
|
19
21
|
DefaultDelimiter: ',',
|
|
22
|
+
|
|
20
23
|
Parser: Parser,
|
|
21
24
|
ParserHandle: ParserHandle,
|
|
22
25
|
ChunkStreamer: ChunkStreamer,
|
|
@@ -24,18 +27,17 @@ const Papa = {
|
|
|
24
27
|
};
|
|
25
28
|
export default Papa;
|
|
26
29
|
|
|
27
|
-
function
|
|
30
|
+
function
|
|
31
|
+
|
|
32
|
+
CsvToJson(_input, _config, UserDefinedStreamer) {
|
|
28
33
|
_config = _config || {};
|
|
29
34
|
var dynamicTyping = _config.dynamicTyping || false;
|
|
30
|
-
|
|
31
35
|
if (isFunction(dynamicTyping)) {
|
|
32
36
|
_config.dynamicTypingFunction = dynamicTyping;
|
|
33
37
|
dynamicTyping = {};
|
|
34
38
|
}
|
|
35
|
-
|
|
36
39
|
_config.dynamicTyping = dynamicTyping;
|
|
37
40
|
_config.transform = isFunction(_config.transform) ? _config.transform : false;
|
|
38
|
-
|
|
39
41
|
if (_config.worker && Papa.WORKERS_SUPPORTED) {
|
|
40
42
|
var w = newWorker();
|
|
41
43
|
w.userStep = _config.step;
|
|
@@ -47,6 +49,7 @@ function CsvToJson(_input, _config, UserDefinedStreamer) {
|
|
|
47
49
|
_config.complete = isFunction(_config.complete);
|
|
48
50
|
_config.error = isFunction(_config.error);
|
|
49
51
|
delete _config.worker;
|
|
52
|
+
|
|
50
53
|
w.postMessage({
|
|
51
54
|
input: _input,
|
|
52
55
|
config: _config,
|
|
@@ -54,9 +57,7 @@ function CsvToJson(_input, _config, UserDefinedStreamer) {
|
|
|
54
57
|
});
|
|
55
58
|
return;
|
|
56
59
|
}
|
|
57
|
-
|
|
58
60
|
var streamer = null;
|
|
59
|
-
|
|
60
61
|
if (typeof _input === 'string') {
|
|
61
62
|
streamer = new StringStreamer(_config);
|
|
62
63
|
}
|
|
@@ -67,27 +68,30 @@ function CsvToJson(_input, _config, UserDefinedStreamer) {
|
|
|
67
68
|
|
|
68
69
|
return streamer.stream(_input);
|
|
69
70
|
}
|
|
70
|
-
|
|
71
71
|
function JsonToCsv(_input, _config) {
|
|
72
|
+
|
|
72
73
|
var _quotes = false;
|
|
74
|
+
|
|
73
75
|
var _writeHeader = true;
|
|
76
|
+
|
|
74
77
|
var _delimiter = ',';
|
|
78
|
+
|
|
75
79
|
var _newline = '\r\n';
|
|
80
|
+
|
|
76
81
|
var _quoteChar = '"';
|
|
77
82
|
|
|
78
83
|
var _escapedQuote = _quoteChar + _quoteChar;
|
|
79
84
|
|
|
80
85
|
var _skipEmptyLines = false;
|
|
86
|
+
|
|
81
87
|
var _columns = null;
|
|
82
88
|
unpackConfig();
|
|
83
89
|
var quoteCharRegex = new RegExp(escapeRegExp(_quoteChar), 'g');
|
|
84
90
|
if (typeof _input === 'string') _input = JSON.parse(_input);
|
|
85
|
-
|
|
86
91
|
if (Array.isArray(_input)) {
|
|
87
92
|
if (!_input.length || Array.isArray(_input[0])) return serialize(null, _input, _skipEmptyLines);else if (typeof _input[0] === 'object') return serialize(_columns || objectKeys(_input[0]), _input, _skipEmptyLines);
|
|
88
93
|
} else if (typeof _input === 'object') {
|
|
89
94
|
if (typeof _input.data === 'string') _input.data = JSON.parse(_input.data);
|
|
90
|
-
|
|
91
95
|
if (Array.isArray(_input.data)) {
|
|
92
96
|
if (!_input.fields) _input.fields = _input.meta && _input.meta.fields;
|
|
93
97
|
if (!_input.fields) _input.fields = Array.isArray(_input.data[0]) ? _input.fields : objectKeys(_input.data[0]);
|
|
@@ -98,27 +102,22 @@ function JsonToCsv(_input, _config) {
|
|
|
98
102
|
}
|
|
99
103
|
|
|
100
104
|
throw new Error('Unable to serialize unrecognized input');
|
|
101
|
-
|
|
102
105
|
function unpackConfig() {
|
|
103
106
|
if (typeof _config !== 'object') return;
|
|
104
|
-
|
|
105
107
|
if (typeof _config.delimiter === 'string' && !Papa.BAD_DELIMITERS.filter(function (value) {
|
|
106
108
|
return _config.delimiter.indexOf(value) !== -1;
|
|
107
109
|
}).length) {
|
|
108
110
|
_delimiter = _config.delimiter;
|
|
109
111
|
}
|
|
110
|
-
|
|
111
112
|
if (typeof _config.quotes === 'boolean' || Array.isArray(_config.quotes)) _quotes = _config.quotes;
|
|
112
113
|
if (typeof _config.skipEmptyLines === 'boolean' || typeof _config.skipEmptyLines === 'string') _skipEmptyLines = _config.skipEmptyLines;
|
|
113
114
|
if (typeof _config.newline === 'string') _newline = _config.newline;
|
|
114
115
|
if (typeof _config.quoteChar === 'string') _quoteChar = _config.quoteChar;
|
|
115
116
|
if (typeof _config.header === 'boolean') _writeHeader = _config.header;
|
|
116
|
-
|
|
117
117
|
if (Array.isArray(_config.columns)) {
|
|
118
118
|
if (_config.columns.length === 0) throw new Error('Option columns is empty');
|
|
119
119
|
_columns = _config.columns;
|
|
120
120
|
}
|
|
121
|
-
|
|
122
121
|
if (_config.escapeChar !== undefined) {
|
|
123
122
|
_escapedQuote = _config.escapeChar + _quoteChar;
|
|
124
123
|
}
|
|
@@ -127,9 +126,7 @@ function JsonToCsv(_input, _config) {
|
|
|
127
126
|
function objectKeys(obj) {
|
|
128
127
|
if (typeof obj !== 'object') return [];
|
|
129
128
|
var keys = [];
|
|
130
|
-
|
|
131
129
|
for (var key in obj) keys.push(key);
|
|
132
|
-
|
|
133
130
|
return keys;
|
|
134
131
|
}
|
|
135
132
|
|
|
@@ -145,7 +142,6 @@ function JsonToCsv(_input, _config) {
|
|
|
145
142
|
if (i > 0) csv += _delimiter;
|
|
146
143
|
csv += safe(fields[i], i);
|
|
147
144
|
}
|
|
148
|
-
|
|
149
145
|
if (data.length > 0) csv += _newline;
|
|
150
146
|
}
|
|
151
147
|
|
|
@@ -153,35 +149,28 @@ function JsonToCsv(_input, _config) {
|
|
|
153
149
|
var maxCol = hasHeader ? fields.length : data[row].length;
|
|
154
150
|
var emptyLine = false;
|
|
155
151
|
var nullLine = hasHeader ? Object.keys(data[row]).length === 0 : data[row].length === 0;
|
|
156
|
-
|
|
157
152
|
if (skipEmptyLines && !hasHeader) {
|
|
158
153
|
emptyLine = skipEmptyLines === 'greedy' ? data[row].join('').trim() === '' : data[row].length === 1 && data[row][0].length === 0;
|
|
159
154
|
}
|
|
160
|
-
|
|
161
155
|
if (skipEmptyLines === 'greedy' && hasHeader) {
|
|
162
156
|
var line = [];
|
|
163
|
-
|
|
164
157
|
for (var c = 0; c < maxCol; c++) {
|
|
165
158
|
var cx = dataKeyedByField ? fields[c] : c;
|
|
166
159
|
line.push(data[row][cx]);
|
|
167
160
|
}
|
|
168
|
-
|
|
169
161
|
emptyLine = line.join('').trim() === '';
|
|
170
162
|
}
|
|
171
|
-
|
|
172
163
|
if (!emptyLine) {
|
|
173
164
|
for (var col = 0; col < maxCol; col++) {
|
|
174
165
|
if (col > 0 && !nullLine) csv += _delimiter;
|
|
175
166
|
var colIdx = hasHeader && dataKeyedByField ? fields[col] : col;
|
|
176
167
|
csv += safe(data[row][colIdx], col);
|
|
177
168
|
}
|
|
178
|
-
|
|
179
169
|
if (row < data.length - 1 && (!skipEmptyLines || maxCol > 0 && !nullLine)) {
|
|
180
170
|
csv += _newline;
|
|
181
171
|
}
|
|
182
172
|
}
|
|
183
173
|
}
|
|
184
|
-
|
|
185
174
|
return csv;
|
|
186
175
|
}
|
|
187
176
|
|
|
@@ -192,10 +181,8 @@ function JsonToCsv(_input, _config) {
|
|
|
192
181
|
var needsQuotes = typeof _quotes === 'boolean' && _quotes || Array.isArray(_quotes) && _quotes[col] || hasAny(str, Papa.BAD_DELIMITERS) || str.indexOf(_delimiter) > -1 || str.charAt(0) === ' ' || str.charAt(str.length - 1) === ' ';
|
|
193
182
|
return needsQuotes ? _quoteChar + str + _quoteChar : str;
|
|
194
183
|
}
|
|
195
|
-
|
|
196
184
|
function hasAny(str, substrings) {
|
|
197
185
|
for (var i = 0; i < substrings.length; i++) if (str.indexOf(substrings[i]) > -1) return true;
|
|
198
|
-
|
|
199
186
|
return false;
|
|
200
187
|
}
|
|
201
188
|
}
|
|
@@ -217,59 +204,45 @@ function ChunkStreamer(config) {
|
|
|
217
204
|
meta: {}
|
|
218
205
|
};
|
|
219
206
|
replaceConfig.call(this, config);
|
|
220
|
-
|
|
221
207
|
this.parseChunk = function (chunk, isFakeChunk) {
|
|
222
208
|
if (this.isFirstChunk && isFunction(this._config.beforeFirstChunk)) {
|
|
223
209
|
var modifiedChunk = this._config.beforeFirstChunk(chunk);
|
|
224
|
-
|
|
225
210
|
if (modifiedChunk !== undefined) chunk = modifiedChunk;
|
|
226
211
|
}
|
|
227
|
-
|
|
228
212
|
this.isFirstChunk = false;
|
|
213
|
+
|
|
229
214
|
var aggregate = this._partialLine + chunk;
|
|
230
215
|
this._partialLine = '';
|
|
231
|
-
|
|
232
216
|
var results = this._handle.parse(aggregate, this._baseIndex, !this._finished);
|
|
233
|
-
|
|
234
217
|
if (this._handle.paused() || this._handle.aborted()) return;
|
|
235
218
|
var lastIndex = results.meta.cursor;
|
|
236
|
-
|
|
237
219
|
if (!this._finished) {
|
|
238
220
|
this._partialLine = aggregate.substring(lastIndex - this._baseIndex);
|
|
239
221
|
this._baseIndex = lastIndex;
|
|
240
222
|
}
|
|
241
|
-
|
|
242
223
|
if (results && results.data) this._rowCount += results.data.length;
|
|
243
224
|
var finishedIncludingPreview = this._finished || this._config.preview && this._rowCount >= this._config.preview;
|
|
244
|
-
|
|
245
225
|
if (isFunction(this._config.chunk) && !isFakeChunk) {
|
|
246
226
|
this._config.chunk(results, this._handle);
|
|
247
|
-
|
|
248
227
|
if (this._handle.paused() || this._handle.aborted()) return;
|
|
249
228
|
results = undefined;
|
|
250
229
|
this._completeResults = undefined;
|
|
251
230
|
}
|
|
252
|
-
|
|
253
231
|
if (!this._config.step && !this._config.chunk) {
|
|
254
232
|
this._completeResults.data = this._completeResults.data.concat(results.data);
|
|
255
233
|
this._completeResults.errors = this._completeResults.errors.concat(results.errors);
|
|
256
234
|
this._completeResults.meta = results.meta;
|
|
257
235
|
}
|
|
258
|
-
|
|
259
236
|
if (!this._completed && finishedIncludingPreview && isFunction(this._config.complete) && (!results || !results.meta.aborted)) {
|
|
260
237
|
this._config.complete(this._completeResults, this._input);
|
|
261
|
-
|
|
262
238
|
this._completed = true;
|
|
263
239
|
}
|
|
264
|
-
|
|
265
240
|
if (!finishedIncludingPreview && (!results || !results.meta.paused)) this._nextChunk();
|
|
266
241
|
return results;
|
|
267
242
|
};
|
|
268
|
-
|
|
269
243
|
this._sendError = function (error) {
|
|
270
244
|
if (isFunction(this._config.error)) this._config.error(error);
|
|
271
245
|
};
|
|
272
|
-
|
|
273
246
|
function replaceConfig(config) {
|
|
274
247
|
var configCopy = copy(config);
|
|
275
248
|
configCopy.chunkSize = parseInt(configCopy.chunkSize);
|
|
@@ -284,12 +257,10 @@ function StringStreamer(config) {
|
|
|
284
257
|
config = config || {};
|
|
285
258
|
ChunkStreamer.call(this, config);
|
|
286
259
|
var remaining;
|
|
287
|
-
|
|
288
260
|
this.stream = function (s) {
|
|
289
261
|
remaining = s;
|
|
290
262
|
return this._nextChunk();
|
|
291
263
|
};
|
|
292
|
-
|
|
293
264
|
this._nextChunk = function () {
|
|
294
265
|
if (this._finished) return;
|
|
295
266
|
var size = this._config.chunkSize;
|
|
@@ -299,7 +270,6 @@ function StringStreamer(config) {
|
|
|
299
270
|
return this.parseChunk(chunk);
|
|
300
271
|
};
|
|
301
272
|
}
|
|
302
|
-
|
|
303
273
|
StringStreamer.prototype = Object.create(StringStreamer.prototype);
|
|
304
274
|
StringStreamer.prototype.constructor = StringStreamer;
|
|
305
275
|
|
|
@@ -309,34 +279,29 @@ function ParserHandle(_config) {
|
|
|
309
279
|
var self = this;
|
|
310
280
|
var _stepCounter = 0;
|
|
311
281
|
var _rowCounter = 0;
|
|
312
|
-
|
|
313
282
|
var _input;
|
|
314
|
-
|
|
315
283
|
var _parser;
|
|
316
|
-
|
|
317
284
|
var _paused = false;
|
|
318
285
|
var _aborted = false;
|
|
319
|
-
|
|
320
286
|
var _delimiterError;
|
|
321
|
-
|
|
322
287
|
var _fields = [];
|
|
323
288
|
var _results = {
|
|
324
289
|
data: [],
|
|
325
290
|
errors: [],
|
|
326
291
|
meta: {}
|
|
327
292
|
};
|
|
328
|
-
|
|
329
293
|
if (isFunction(_config.step)) {
|
|
330
294
|
var userStep = _config.step;
|
|
331
|
-
|
|
332
295
|
_config.step = function (results) {
|
|
333
296
|
_results = results;
|
|
334
|
-
if (needsHeaderRow()) processResults();
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
297
|
+
if (needsHeaderRow()) processResults();
|
|
298
|
+
else {
|
|
299
|
+
processResults();
|
|
300
|
+
|
|
301
|
+
if (!_results.data || _results.data.length === 0) return;
|
|
302
|
+
_stepCounter += results.data.length;
|
|
303
|
+
if (_config.preview && _stepCounter > _config.preview) _parser.abort();else userStep(_results, self);
|
|
304
|
+
}
|
|
340
305
|
};
|
|
341
306
|
}
|
|
342
307
|
|
|
@@ -344,7 +309,6 @@ function ParserHandle(_config) {
|
|
|
344
309
|
var quoteChar = _config.quoteChar || '"';
|
|
345
310
|
if (!_config.newline) _config.newline = guessLineEndings(input, quoteChar);
|
|
346
311
|
_delimiterError = false;
|
|
347
|
-
|
|
348
312
|
if (!_config.delimiter) {
|
|
349
313
|
var delimGuess = guessDelimiter(input, _config.newline, _config.skipEmptyLines, _config.comments, _config.delimitersToGuess);
|
|
350
314
|
if (delimGuess.successful) _config.delimiter = delimGuess.bestDelimiter;else {
|
|
@@ -356,9 +320,9 @@ function ParserHandle(_config) {
|
|
|
356
320
|
_config.delimiter = _config.delimiter(input);
|
|
357
321
|
_results.meta.delimiter = _config.delimiter;
|
|
358
322
|
}
|
|
359
|
-
|
|
360
323
|
var parserConfig = copy(_config);
|
|
361
324
|
if (_config.preview && _config.header) parserConfig.preview++;
|
|
325
|
+
|
|
362
326
|
_input = input;
|
|
363
327
|
_parser = new Parser(parserConfig);
|
|
364
328
|
_results = _parser.parse(_input, baseIndex, ignoreLastRow);
|
|
@@ -373,140 +337,107 @@ function ParserHandle(_config) {
|
|
|
373
337
|
}
|
|
374
338
|
};
|
|
375
339
|
};
|
|
376
|
-
|
|
377
340
|
this.paused = function () {
|
|
378
341
|
return _paused;
|
|
379
342
|
};
|
|
380
|
-
|
|
381
343
|
this.pause = function () {
|
|
382
344
|
_paused = true;
|
|
383
|
-
|
|
384
345
|
_parser.abort();
|
|
385
|
-
|
|
386
346
|
_input = _input.substr(_parser.getCharIndex());
|
|
387
347
|
};
|
|
388
|
-
|
|
389
348
|
this.resume = function () {
|
|
390
349
|
_paused = false;
|
|
391
350
|
self.streamer.parseChunk(_input, true);
|
|
392
351
|
};
|
|
393
|
-
|
|
394
352
|
this.aborted = function () {
|
|
395
353
|
return _aborted;
|
|
396
354
|
};
|
|
397
|
-
|
|
398
355
|
this.abort = function () {
|
|
399
356
|
_aborted = true;
|
|
400
|
-
|
|
401
357
|
_parser.abort();
|
|
402
|
-
|
|
403
358
|
_results.meta.aborted = true;
|
|
404
359
|
if (isFunction(_config.complete)) _config.complete(_results);
|
|
405
360
|
_input = '';
|
|
406
361
|
};
|
|
407
|
-
|
|
408
362
|
function testEmptyLine(s) {
|
|
409
363
|
return _config.skipEmptyLines === 'greedy' ? s.join('').trim() === '' : s.length === 1 && s[0].length === 0;
|
|
410
364
|
}
|
|
411
|
-
|
|
412
365
|
function processResults() {
|
|
413
366
|
if (_results && _delimiterError) {
|
|
414
367
|
addError('Delimiter', 'UndetectableDelimiter', "Unable to auto-detect delimiting character; defaulted to '" + Papa.DefaultDelimiter + "'");
|
|
415
368
|
_delimiterError = false;
|
|
416
369
|
}
|
|
417
|
-
|
|
418
370
|
if (_config.skipEmptyLines) {
|
|
419
371
|
for (var i = 0; i < _results.data.length; i++) if (testEmptyLine(_results.data[i])) _results.data.splice(i--, 1);
|
|
420
372
|
}
|
|
421
|
-
|
|
422
373
|
if (needsHeaderRow()) fillHeaderFields();
|
|
423
374
|
return applyHeaderAndDynamicTypingAndTransformation();
|
|
424
375
|
}
|
|
425
|
-
|
|
426
376
|
function needsHeaderRow() {
|
|
427
377
|
return _config.header && _fields.length === 0;
|
|
428
378
|
}
|
|
429
|
-
|
|
430
379
|
function fillHeaderFields() {
|
|
431
380
|
if (!_results) return;
|
|
432
|
-
|
|
433
381
|
function addHeder(header) {
|
|
434
382
|
if (isFunction(_config.transformHeader)) header = _config.transformHeader(header);
|
|
435
|
-
|
|
436
383
|
_fields.push(header);
|
|
437
384
|
}
|
|
438
|
-
|
|
439
385
|
if (Array.isArray(_results.data[0])) {
|
|
440
386
|
for (var i = 0; needsHeaderRow() && i < _results.data.length; i++) _results.data[i].forEach(addHeder);
|
|
441
|
-
|
|
442
387
|
_results.data.splice(0, 1);
|
|
443
|
-
}
|
|
388
|
+
}
|
|
389
|
+
else _results.data.forEach(addHeder);
|
|
444
390
|
}
|
|
445
|
-
|
|
446
391
|
function shouldApplyDynamicTyping(field) {
|
|
447
392
|
if (_config.dynamicTypingFunction && _config.dynamicTyping[field] === undefined) {
|
|
448
393
|
_config.dynamicTyping[field] = _config.dynamicTypingFunction(field);
|
|
449
394
|
}
|
|
450
|
-
|
|
451
395
|
return (_config.dynamicTyping[field] || _config.dynamicTyping) === true;
|
|
452
396
|
}
|
|
453
|
-
|
|
454
397
|
function parseDynamic(field, value) {
|
|
455
398
|
if (shouldApplyDynamicTyping(field)) {
|
|
456
399
|
if (value === 'true' || value === 'TRUE') return true;else if (value === 'false' || value === 'FALSE') return false;else if (FLOAT.test(value)) return parseFloat(value);else if (ISO_DATE.test(value)) return new Date(value);else return value === '' ? null : value;
|
|
457
400
|
}
|
|
458
|
-
|
|
459
401
|
return value;
|
|
460
402
|
}
|
|
461
|
-
|
|
462
403
|
function applyHeaderAndDynamicTypingAndTransformation() {
|
|
463
404
|
if (!_results || !_results.data || !_config.header && !_config.dynamicTyping && !_config.transform) return _results;
|
|
464
|
-
|
|
465
405
|
function processRow(rowSource, i) {
|
|
466
406
|
var row = _config.header ? {} : [];
|
|
467
407
|
var j;
|
|
468
|
-
|
|
469
408
|
for (j = 0; j < rowSource.length; j++) {
|
|
470
409
|
var field = j;
|
|
471
410
|
var value = rowSource[j];
|
|
472
411
|
if (_config.header) field = j >= _fields.length ? '__parsed_extra' : _fields[j];
|
|
473
412
|
if (_config.transform) value = _config.transform(value, field);
|
|
474
413
|
value = parseDynamic(field, value);
|
|
475
|
-
|
|
476
414
|
if (field === '__parsed_extra') {
|
|
477
415
|
row[field] = row[field] || [];
|
|
478
416
|
row[field].push(value);
|
|
479
417
|
} else row[field] = value;
|
|
480
418
|
}
|
|
481
|
-
|
|
482
419
|
if (_config.header) {
|
|
483
420
|
if (j > _fields.length) addError('FieldMismatch', 'TooManyFields', 'Too many fields: expected ' + _fields.length + ' fields but parsed ' + j, _rowCounter + i);else if (j < _fields.length) addError('FieldMismatch', 'TooFewFields', 'Too few fields: expected ' + _fields.length + ' fields but parsed ' + j, _rowCounter + i);
|
|
484
421
|
}
|
|
485
|
-
|
|
486
422
|
return row;
|
|
487
423
|
}
|
|
488
|
-
|
|
489
424
|
var incrementBy = 1;
|
|
490
|
-
|
|
491
425
|
if (!_results.data[0] || Array.isArray(_results.data[0])) {
|
|
492
426
|
_results.data = _results.data.map(processRow);
|
|
493
427
|
incrementBy = _results.data.length;
|
|
494
428
|
} else _results.data = processRow(_results.data, 0);
|
|
495
|
-
|
|
496
429
|
if (_config.header && _results.meta) _results.meta.fields = _fields;
|
|
497
430
|
_rowCounter += incrementBy;
|
|
498
431
|
return _results;
|
|
499
432
|
}
|
|
500
|
-
|
|
501
433
|
function guessDelimiter(input, newline, skipEmptyLines, comments, delimitersToGuess) {
|
|
502
434
|
var bestDelim, bestDelta, fieldCountPrevRow;
|
|
503
435
|
delimitersToGuess = delimitersToGuess || [',', '\t', '|', ';', Papa.RECORD_SEP, Papa.UNIT_SEP];
|
|
504
|
-
|
|
505
436
|
for (var i = 0; i < delimitersToGuess.length; i++) {
|
|
506
437
|
var delim = delimitersToGuess[i];
|
|
507
438
|
var delta = 0,
|
|
508
|
-
|
|
509
|
-
|
|
439
|
+
avgFieldCount = 0,
|
|
440
|
+
emptyLinesCount = 0;
|
|
510
441
|
fieldCountPrevRow = undefined;
|
|
511
442
|
var preview = new Parser({
|
|
512
443
|
comments: comments,
|
|
@@ -514,16 +445,13 @@ function ParserHandle(_config) {
|
|
|
514
445
|
newline: newline,
|
|
515
446
|
preview: 10
|
|
516
447
|
}).parse(input);
|
|
517
|
-
|
|
518
448
|
for (var j = 0; j < preview.data.length; j++) {
|
|
519
449
|
if (skipEmptyLines && testEmptyLine(preview.data[j])) {
|
|
520
450
|
emptyLinesCount++;
|
|
521
451
|
continue;
|
|
522
452
|
}
|
|
523
|
-
|
|
524
453
|
var fieldCount = preview.data[j].length;
|
|
525
454
|
avgFieldCount += fieldCount;
|
|
526
|
-
|
|
527
455
|
if (typeof fieldCountPrevRow === 'undefined') {
|
|
528
456
|
fieldCountPrevRow = 0;
|
|
529
457
|
continue;
|
|
@@ -532,22 +460,18 @@ function ParserHandle(_config) {
|
|
|
532
460
|
fieldCountPrevRow = fieldCount;
|
|
533
461
|
}
|
|
534
462
|
}
|
|
535
|
-
|
|
536
463
|
if (preview.data.length > 0) avgFieldCount /= preview.data.length - emptyLinesCount;
|
|
537
|
-
|
|
538
464
|
if ((typeof bestDelta === 'undefined' || delta > bestDelta) && avgFieldCount > 1.99) {
|
|
539
465
|
bestDelta = delta;
|
|
540
466
|
bestDelim = delim;
|
|
541
467
|
}
|
|
542
468
|
}
|
|
543
|
-
|
|
544
469
|
_config.delimiter = bestDelim;
|
|
545
470
|
return {
|
|
546
471
|
successful: !!bestDelim,
|
|
547
472
|
bestDelimiter: bestDelim
|
|
548
473
|
};
|
|
549
474
|
}
|
|
550
|
-
|
|
551
475
|
function guessLineEndings(input, quoteChar) {
|
|
552
476
|
input = input.substr(0, 1024 * 1024);
|
|
553
477
|
var re = new RegExp(escapeRegExp(quoteChar) + '([^]*?)' + escapeRegExp(quoteChar), 'gm');
|
|
@@ -557,14 +481,11 @@ function ParserHandle(_config) {
|
|
|
557
481
|
var nAppearsFirst = n.length > 1 && n[0].length < r[0].length;
|
|
558
482
|
if (r.length === 1 || nAppearsFirst) return '\n';
|
|
559
483
|
var numWithN = 0;
|
|
560
|
-
|
|
561
484
|
for (var i = 0; i < r.length; i++) {
|
|
562
485
|
if (r[i][0] === '\n') numWithN++;
|
|
563
486
|
}
|
|
564
|
-
|
|
565
487
|
return numWithN >= r.length / 2 ? '\r\n' : '\r';
|
|
566
488
|
}
|
|
567
|
-
|
|
568
489
|
function addError(type, code, msg, row) {
|
|
569
490
|
_results.errors.push({
|
|
570
491
|
type: type,
|
|
@@ -588,64 +509,59 @@ function Parser(config) {
|
|
|
588
509
|
var preview = config.preview;
|
|
589
510
|
var fastMode = config.fastMode;
|
|
590
511
|
var quoteChar;
|
|
591
|
-
|
|
592
512
|
if (config.quoteChar === undefined) {
|
|
593
513
|
quoteChar = '"';
|
|
594
514
|
} else {
|
|
595
515
|
quoteChar = config.quoteChar;
|
|
596
516
|
}
|
|
597
|
-
|
|
598
517
|
var escapeChar = quoteChar;
|
|
599
|
-
|
|
600
518
|
if (config.escapeChar !== undefined) {
|
|
601
519
|
escapeChar = config.escapeChar;
|
|
602
520
|
}
|
|
603
521
|
|
|
604
522
|
if (typeof delim !== 'string' || Papa.BAD_DELIMITERS.indexOf(delim) > -1) delim = ',';
|
|
523
|
+
|
|
605
524
|
if (comments === delim) throw new Error('Comment character same as delimiter');else if (comments === true) comments = '#';else if (typeof comments !== 'string' || Papa.BAD_DELIMITERS.indexOf(comments) > -1) comments = false;
|
|
525
|
+
|
|
606
526
|
if (newline !== '\n' && newline !== '\r' && newline !== '\r\n') newline = '\n';
|
|
527
|
+
|
|
607
528
|
var cursor = 0;
|
|
608
529
|
var aborted = false;
|
|
609
|
-
|
|
610
530
|
this.parse = function (input, baseIndex, ignoreLastRow) {
|
|
611
531
|
if (typeof input !== 'string') throw new Error('Input must be a string');
|
|
532
|
+
|
|
612
533
|
var inputLen = input.length,
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
534
|
+
delimLen = delim.length,
|
|
535
|
+
newlineLen = newline.length,
|
|
536
|
+
commentsLen = comments.length;
|
|
616
537
|
var stepIsFunction = isFunction(step);
|
|
538
|
+
|
|
617
539
|
cursor = 0;
|
|
618
540
|
var data = [],
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
541
|
+
errors = [],
|
|
542
|
+
row = [],
|
|
543
|
+
lastCursor = 0;
|
|
622
544
|
if (!input) return returnable();
|
|
623
|
-
|
|
624
545
|
if (fastMode || fastMode !== false && input.indexOf(quoteChar) === -1) {
|
|
625
546
|
var rows = input.split(newline);
|
|
626
|
-
|
|
627
547
|
for (var i = 0; i < rows.length; i++) {
|
|
628
548
|
row = rows[i];
|
|
629
549
|
cursor += row.length;
|
|
630
550
|
if (i !== rows.length - 1) cursor += newline.length;else if (ignoreLastRow) return returnable();
|
|
631
551
|
if (comments && row.substr(0, commentsLen) === comments) continue;
|
|
632
|
-
|
|
633
552
|
if (stepIsFunction) {
|
|
634
553
|
data = [];
|
|
635
554
|
pushRow(row.split(delim));
|
|
636
555
|
doStep();
|
|
637
556
|
if (aborted) return returnable();
|
|
638
557
|
} else pushRow(row.split(delim));
|
|
639
|
-
|
|
640
558
|
if (preview && i >= preview) {
|
|
641
559
|
data = data.slice(0, preview);
|
|
642
560
|
return returnable(true);
|
|
643
561
|
}
|
|
644
562
|
}
|
|
645
|
-
|
|
646
563
|
return returnable();
|
|
647
564
|
}
|
|
648
|
-
|
|
649
565
|
var nextDelim = input.indexOf(delim, cursor);
|
|
650
566
|
var nextNewline = input.indexOf(newline, cursor);
|
|
651
567
|
var quoteCharRegex = new RegExp(escapeRegExp(escapeChar) + escapeRegExp(quoteChar), 'g');
|
|
@@ -654,8 +570,8 @@ function Parser(config) {
|
|
|
654
570
|
for (;;) {
|
|
655
571
|
if (input[cursor] === quoteChar) {
|
|
656
572
|
quoteSearch = cursor;
|
|
657
|
-
cursor++;
|
|
658
573
|
|
|
574
|
+
cursor++;
|
|
659
575
|
for (;;) {
|
|
660
576
|
quoteSearch = input.indexOf(quoteChar, quoteSearch + 1);
|
|
661
577
|
|
|
@@ -669,7 +585,6 @@ function Parser(config) {
|
|
|
669
585
|
index: cursor
|
|
670
586
|
});
|
|
671
587
|
}
|
|
672
|
-
|
|
673
588
|
return finish();
|
|
674
589
|
}
|
|
675
590
|
|
|
@@ -695,16 +610,13 @@ function Parser(config) {
|
|
|
695
610
|
cursor = quoteSearch + 1 + spacesBetweenQuoteAndDelimiter + delimLen;
|
|
696
611
|
nextDelim = input.indexOf(delim, cursor);
|
|
697
612
|
nextNewline = input.indexOf(newline, cursor);
|
|
698
|
-
|
|
699
613
|
if (stepIsFunction) {
|
|
700
614
|
doStep();
|
|
701
615
|
if (aborted) return returnable();
|
|
702
616
|
}
|
|
703
|
-
|
|
704
617
|
if (preview && data.length >= preview) return returnable(true);
|
|
705
618
|
break;
|
|
706
619
|
}
|
|
707
|
-
|
|
708
620
|
var spacesBetweenQuoteAndNewLine = extraSpaces(nextNewline);
|
|
709
621
|
|
|
710
622
|
if (input.substr(quoteSearch + 1 + spacesBetweenQuoteAndNewLine, newlineLen) === newline) {
|
|
@@ -716,7 +628,6 @@ function Parser(config) {
|
|
|
716
628
|
doStep();
|
|
717
629
|
if (aborted) return returnable();
|
|
718
630
|
}
|
|
719
|
-
|
|
720
631
|
if (preview && data.length >= preview) return returnable(true);
|
|
721
632
|
break;
|
|
722
633
|
}
|
|
@@ -731,18 +642,17 @@ function Parser(config) {
|
|
|
731
642
|
quoteSearch++;
|
|
732
643
|
continue;
|
|
733
644
|
}
|
|
734
|
-
|
|
735
645
|
if (stepIsFunction) {
|
|
736
646
|
doStep();
|
|
737
647
|
if (aborted) return returnable();
|
|
738
648
|
}
|
|
739
|
-
|
|
740
649
|
if (preview && data.length >= preview) return returnable(true);
|
|
741
650
|
continue;
|
|
742
651
|
}
|
|
743
652
|
|
|
744
653
|
if (comments && row.length === 0 && input.substr(cursor, commentsLen) === comments) {
|
|
745
|
-
if (nextNewline === -1)
|
|
654
|
+
if (nextNewline === -1)
|
|
655
|
+
return returnable();
|
|
746
656
|
cursor = nextNewline + newlineLen;
|
|
747
657
|
nextNewline = input.indexOf(newline, cursor);
|
|
748
658
|
nextDelim = input.indexOf(delim, cursor);
|
|
@@ -759,21 +669,16 @@ function Parser(config) {
|
|
|
759
669
|
if (nextNewline !== -1) {
|
|
760
670
|
row.push(input.substring(cursor, nextNewline));
|
|
761
671
|
saveRow(nextNewline + newlineLen);
|
|
762
|
-
|
|
763
672
|
if (stepIsFunction) {
|
|
764
673
|
doStep();
|
|
765
674
|
if (aborted) return returnable();
|
|
766
675
|
}
|
|
767
|
-
|
|
768
676
|
if (preview && data.length >= preview) return returnable(true);
|
|
769
677
|
continue;
|
|
770
678
|
}
|
|
771
|
-
|
|
772
679
|
break;
|
|
773
680
|
}
|
|
774
|
-
|
|
775
681
|
return finish();
|
|
776
|
-
|
|
777
682
|
function pushRow(row) {
|
|
778
683
|
data.push(row);
|
|
779
684
|
lastCursor = cursor;
|
|
@@ -781,15 +686,12 @@ function Parser(config) {
|
|
|
781
686
|
|
|
782
687
|
function extraSpaces(index) {
|
|
783
688
|
var spaceLength = 0;
|
|
784
|
-
|
|
785
689
|
if (index !== -1) {
|
|
786
690
|
var textBetweenClosingQuoteAndIndex = input.substring(quoteSearch + 1, index);
|
|
787
|
-
|
|
788
691
|
if (textBetweenClosingQuoteAndIndex && textBetweenClosingQuoteAndIndex.trim() === '') {
|
|
789
692
|
spaceLength = textBetweenClosingQuoteAndIndex.length;
|
|
790
693
|
}
|
|
791
694
|
}
|
|
792
|
-
|
|
793
695
|
return spaceLength;
|
|
794
696
|
}
|
|
795
697
|
|
|
@@ -840,7 +742,6 @@ function Parser(config) {
|
|
|
840
742
|
return cursor;
|
|
841
743
|
};
|
|
842
744
|
}
|
|
843
|
-
|
|
844
745
|
function notImplemented() {
|
|
845
746
|
throw new Error('Not implemented.');
|
|
846
747
|
}
|
|
@@ -848,12 +749,9 @@ function notImplemented() {
|
|
|
848
749
|
function copy(obj) {
|
|
849
750
|
if (typeof obj !== 'object' || obj === null) return obj;
|
|
850
751
|
var cpy = Array.isArray(obj) ? [] : {};
|
|
851
|
-
|
|
852
752
|
for (var key in obj) cpy[key] = copy(obj[key]);
|
|
853
|
-
|
|
854
753
|
return cpy;
|
|
855
754
|
}
|
|
856
|
-
|
|
857
755
|
function isFunction(func) {
|
|
858
756
|
return typeof func === 'function';
|
|
859
757
|
}
|