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