@fto-consult/expo-ui 8.72.0 → 8.73.0

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.
@@ -0,0 +1,1824 @@
1
+ /* @license
2
+ Papa Parse
3
+ v5.4.1
4
+ https://github.com/mholt/PapaParse
5
+ @see : https://github.com/mholt/PapaParse/tree/master
6
+ License: MIT
7
+ */
8
+ const global = (function() {
9
+ // alternative method, similar to `Function('return this')()`
10
+ // but without using `eval` (which is disabled when
11
+ // using Content Security Policy).
12
+ if (typeof self !== 'undefined') { return self; }
13
+ if (typeof window !== 'undefined') { return window; }
14
+ if (typeof global !== 'undefined') { return global; }
15
+ // When running tests none of the above have been defined
16
+ return {};
17
+ })();
18
+
19
+ function getWorkerBlob() {
20
+ var URL = global.URL || global.webkitURL || null;
21
+ var code = moduleFactory.toString();
22
+ return Papa.BLOB_URL || (Papa.BLOB_URL = URL.createObjectURL(new Blob(["var global = (function() { if (typeof self !== 'undefined') { return self; } if (typeof window !== 'undefined') { return window; } if (typeof global !== 'undefined') { return global; } return {}; })(); global.IS_PAPA_WORKER=true; ", '(', code, ')();'], {type: 'text/javascript'})));
23
+ }
24
+
25
+ var IS_WORKER = !global.document && !!global.postMessage,
26
+ IS_PAPA_WORKER = global.IS_PAPA_WORKER || false;
27
+
28
+ var workers = {}, workerIdCounter = 0;
29
+
30
+ const Papa = {};
31
+
32
+ Papa.parse = CsvToJson;
33
+ Papa.unparse = JsonToCsv;
34
+
35
+ Papa.RECORD_SEP = String.fromCharCode(30);
36
+ Papa.UNIT_SEP = String.fromCharCode(31);
37
+ Papa.BYTE_ORDER_MARK = '\ufeff';
38
+ Papa.BAD_DELIMITERS = ['\r', '\n', '"', Papa.BYTE_ORDER_MARK];
39
+ Papa.WORKERS_SUPPORTED = !IS_WORKER && !!global.Worker;
40
+ Papa.NODE_STREAM_INPUT = 1;
41
+
42
+ // Configurable chunk sizes for local and remote files, respectively
43
+ Papa.LocalChunkSize = 1024 * 1024 * 10; // 10 MB
44
+ Papa.RemoteChunkSize = 1024 * 1024 * 5; // 5 MB
45
+ Papa.DefaultDelimiter = ','; // Used if not specified and detection fails
46
+
47
+ // Exposed for testing and development only
48
+ Papa.Parser = Parser;
49
+ Papa.ParserHandle = ParserHandle;
50
+ Papa.NetworkStreamer = NetworkStreamer;
51
+ Papa.FileStreamer = FileStreamer;
52
+ Papa.StringStreamer = StringStreamer;
53
+ Papa.ReadableStreamStreamer = ReadableStreamStreamer;
54
+ if (typeof PAPA_BROWSER_CONTEXT === 'undefined') {
55
+ Papa.DuplexStreamStreamer = DuplexStreamStreamer;
56
+ }
57
+
58
+ if (IS_PAPA_WORKER)
59
+ {
60
+ global.onmessage = workerThreadReceivedMessage;
61
+ }
62
+
63
+ function CsvToJson(_input, _config)
64
+ {
65
+ _config = _config || {};
66
+ var dynamicTyping = _config.dynamicTyping || false;
67
+ if (isFunction(dynamicTyping)) {
68
+ _config.dynamicTypingFunction = dynamicTyping;
69
+ // Will be filled on first row call
70
+ dynamicTyping = {};
71
+ }
72
+ _config.dynamicTyping = dynamicTyping;
73
+
74
+ _config.transform = isFunction(_config.transform) ? _config.transform : false;
75
+
76
+ if (_config.worker && Papa.WORKERS_SUPPORTED)
77
+ {
78
+ var w = newWorker();
79
+
80
+ w.userStep = _config.step;
81
+ w.userChunk = _config.chunk;
82
+ w.userComplete = _config.complete;
83
+ w.userError = _config.error;
84
+
85
+ _config.step = isFunction(_config.step);
86
+ _config.chunk = isFunction(_config.chunk);
87
+ _config.complete = isFunction(_config.complete);
88
+ _config.error = isFunction(_config.error);
89
+ delete _config.worker; // prevent infinite loop
90
+
91
+ w.postMessage({
92
+ input: _input,
93
+ config: _config,
94
+ workerId: w.id
95
+ });
96
+
97
+ return;
98
+ }
99
+
100
+ var streamer = null;
101
+ if (_input === Papa.NODE_STREAM_INPUT && typeof PAPA_BROWSER_CONTEXT === 'undefined')
102
+ {
103
+ // create a node Duplex stream for use
104
+ // with .pipe
105
+ streamer = new DuplexStreamStreamer(_config);
106
+ return streamer.getStream();
107
+ }
108
+ else if (typeof _input === 'string')
109
+ {
110
+ _input = stripBom(_input);
111
+ if (_config.download)
112
+ streamer = new NetworkStreamer(_config);
113
+ else
114
+ streamer = new StringStreamer(_config);
115
+ }
116
+ else if (_input.readable === true && isFunction(_input.read) && isFunction(_input.on))
117
+ {
118
+ streamer = new ReadableStreamStreamer(_config);
119
+ }
120
+ else if ((global.File && _input instanceof File) || _input instanceof Object) // ...Safari. (see issue #106)
121
+ streamer = new FileStreamer(_config);
122
+
123
+ return streamer.stream(_input);
124
+
125
+ // Strip character from UTF-8 BOM encoded files that cause issue parsing the file
126
+ function stripBom(string) {
127
+ if (string.charCodeAt(0) === 0xfeff) {
128
+ return string.slice(1);
129
+ }
130
+ return string;
131
+ }
132
+ }
133
+
134
+ function JsonToCsv(_input, _config)
135
+ {
136
+ // Default configuration
137
+
138
+ /** whether to surround every datum with quotes */
139
+ var _quotes = false;
140
+
141
+ /** whether to write headers */
142
+ var _writeHeader = true;
143
+
144
+ /** delimiting character(s) */
145
+ var _delimiter = ',';
146
+
147
+ /** newline character(s) */
148
+ var _newline = '\r\n';
149
+
150
+ /** quote character */
151
+ var _quoteChar = '"';
152
+
153
+ /** escaped quote character, either "" or <config.escapeChar>" */
154
+ var _escapedQuote = _quoteChar + _quoteChar;
155
+
156
+ /** whether to skip empty lines */
157
+ var _skipEmptyLines = false;
158
+
159
+ /** the columns (keys) we expect when we unparse objects */
160
+ var _columns = null;
161
+
162
+ /** whether to prevent outputting cells that can be parsed as formulae by spreadsheet software (Excel and LibreOffice) */
163
+ var _escapeFormulae = false;
164
+
165
+ unpackConfig();
166
+
167
+ var quoteCharRegex = new RegExp(escapeRegExp(_quoteChar), 'g');
168
+
169
+ if (typeof _input === 'string')
170
+ _input = JSON.parse(_input);
171
+
172
+ if (Array.isArray(_input))
173
+ {
174
+ if (!_input.length || Array.isArray(_input[0]))
175
+ return serialize(null, _input, _skipEmptyLines);
176
+ else if (typeof _input[0] === 'object')
177
+ return serialize(_columns || Object.keys(_input[0]), _input, _skipEmptyLines);
178
+ }
179
+ else if (typeof _input === 'object')
180
+ {
181
+ if (typeof _input.data === 'string')
182
+ _input.data = JSON.parse(_input.data);
183
+
184
+ if (Array.isArray(_input.data))
185
+ {
186
+ if (!_input.fields)
187
+ _input.fields = _input.meta && _input.meta.fields || _columns;
188
+
189
+ if (!_input.fields)
190
+ _input.fields = Array.isArray(_input.data[0])
191
+ ? _input.fields
192
+ : typeof _input.data[0] === 'object'
193
+ ? Object.keys(_input.data[0])
194
+ : [];
195
+
196
+ if (!(Array.isArray(_input.data[0])) && typeof _input.data[0] !== 'object')
197
+ _input.data = [_input.data]; // handles input like [1,2,3] or ['asdf']
198
+ }
199
+
200
+ return serialize(_input.fields || [], _input.data || [], _skipEmptyLines);
201
+ }
202
+
203
+ // Default (any valid paths should return before this)
204
+ throw new Error('Unable to serialize unrecognized input');
205
+
206
+
207
+ function unpackConfig()
208
+ {
209
+ if (typeof _config !== 'object')
210
+ return;
211
+
212
+ if (typeof _config.delimiter === 'string'
213
+ && !Papa.BAD_DELIMITERS.filter(function(value) { return _config.delimiter.indexOf(value) !== -1; }).length)
214
+ {
215
+ _delimiter = _config.delimiter;
216
+ }
217
+
218
+ if (typeof _config.quotes === 'boolean'
219
+ || typeof _config.quotes === 'function'
220
+ || Array.isArray(_config.quotes))
221
+ _quotes = _config.quotes;
222
+
223
+ if (typeof _config.skipEmptyLines === 'boolean'
224
+ || typeof _config.skipEmptyLines === 'string')
225
+ _skipEmptyLines = _config.skipEmptyLines;
226
+
227
+ if (typeof _config.newline === 'string')
228
+ _newline = _config.newline;
229
+
230
+ if (typeof _config.quoteChar === 'string')
231
+ _quoteChar = _config.quoteChar;
232
+
233
+ if (typeof _config.header === 'boolean')
234
+ _writeHeader = _config.header;
235
+
236
+ if (Array.isArray(_config.columns)) {
237
+
238
+ if (_config.columns.length === 0) throw new Error('Option columns is empty');
239
+
240
+ _columns = _config.columns;
241
+ }
242
+
243
+ if (_config.escapeChar !== undefined) {
244
+ _escapedQuote = _config.escapeChar + _quoteChar;
245
+ }
246
+
247
+ if (_config.escapeFormulae instanceof RegExp) {
248
+ _escapeFormulae = _config.escapeFormulae;
249
+ } else if (typeof _config.escapeFormulae === 'boolean' && _config.escapeFormulae) {
250
+ _escapeFormulae = /^[=+\-@\t\r].*$/;
251
+ }
252
+ }
253
+
254
+ /** The double for loop that iterates the data and writes out a CSV string including header row */
255
+ function serialize(fields, data, skipEmptyLines)
256
+ {
257
+ var csv = '';
258
+
259
+ if (typeof fields === 'string')
260
+ fields = JSON.parse(fields);
261
+ if (typeof data === 'string')
262
+ data = JSON.parse(data);
263
+
264
+ var hasHeader = Array.isArray(fields) && fields.length > 0;
265
+ var dataKeyedByField = !(Array.isArray(data[0]));
266
+
267
+ // If there a header row, write it first
268
+ if (hasHeader && _writeHeader)
269
+ {
270
+ for (var i = 0; i < fields.length; i++)
271
+ {
272
+ if (i > 0)
273
+ csv += _delimiter;
274
+ csv += safe(fields[i], i);
275
+ }
276
+ if (data.length > 0)
277
+ csv += _newline;
278
+ }
279
+
280
+ // Then write out the data
281
+ for (var row = 0; row < data.length; row++)
282
+ {
283
+ var maxCol = hasHeader ? fields.length : data[row].length;
284
+
285
+ var emptyLine = false;
286
+ var nullLine = hasHeader ? Object.keys(data[row]).length === 0 : data[row].length === 0;
287
+ if (skipEmptyLines && !hasHeader)
288
+ {
289
+ emptyLine = skipEmptyLines === 'greedy' ? data[row].join('').trim() === '' : data[row].length === 1 && data[row][0].length === 0;
290
+ }
291
+ if (skipEmptyLines === 'greedy' && hasHeader) {
292
+ var line = [];
293
+ for (var c = 0; c < maxCol; c++) {
294
+ var cx = dataKeyedByField ? fields[c] : c;
295
+ line.push(data[row][cx]);
296
+ }
297
+ emptyLine = line.join('').trim() === '';
298
+ }
299
+ if (!emptyLine)
300
+ {
301
+ for (var col = 0; col < maxCol; col++)
302
+ {
303
+ if (col > 0 && !nullLine)
304
+ csv += _delimiter;
305
+ var colIdx = hasHeader && dataKeyedByField ? fields[col] : col;
306
+ csv += safe(data[row][colIdx], col);
307
+ }
308
+ if (row < data.length - 1 && (!skipEmptyLines || (maxCol > 0 && !nullLine)))
309
+ {
310
+ csv += _newline;
311
+ }
312
+ }
313
+ }
314
+ return csv;
315
+ }
316
+
317
+ /** Encloses a value around quotes if needed (makes a value safe for CSV insertion) */
318
+ function safe(str, col)
319
+ {
320
+ if (typeof str === 'undefined' || str === null)
321
+ return '';
322
+
323
+ if (str.constructor === Date)
324
+ return JSON.stringify(str).slice(1, 25);
325
+
326
+ var needsQuotes = false;
327
+
328
+ if (_escapeFormulae && typeof str === "string" && _escapeFormulae.test(str)) {
329
+ str = "'" + str;
330
+ needsQuotes = true;
331
+ }
332
+
333
+ var escapedQuoteStr = str.toString().replace(quoteCharRegex, _escapedQuote);
334
+
335
+ needsQuotes = needsQuotes
336
+ || _quotes === true
337
+ || (typeof _quotes === 'function' && _quotes(str, col))
338
+ || (Array.isArray(_quotes) && _quotes[col])
339
+ || hasAny(escapedQuoteStr, Papa.BAD_DELIMITERS)
340
+ || escapedQuoteStr.indexOf(_delimiter) > -1
341
+ || escapedQuoteStr.charAt(0) === ' '
342
+ || escapedQuoteStr.charAt(escapedQuoteStr.length - 1) === ' ';
343
+
344
+ return needsQuotes ? _quoteChar + escapedQuoteStr + _quoteChar : escapedQuoteStr;
345
+ }
346
+
347
+ function hasAny(str, substrings)
348
+ {
349
+ for (var i = 0; i < substrings.length; i++)
350
+ if (str.indexOf(substrings[i]) > -1)
351
+ return true;
352
+ return false;
353
+ }
354
+ }
355
+
356
+
357
+ /** ChunkStreamer is the base prototype for various streamer implementations. */
358
+ function ChunkStreamer(config)
359
+ {
360
+ this._handle = null;
361
+ this._finished = false;
362
+ this._completed = false;
363
+ this._halted = false;
364
+ this._input = null;
365
+ this._baseIndex = 0;
366
+ this._partialLine = '';
367
+ this._rowCount = 0;
368
+ this._start = 0;
369
+ this._nextChunk = null;
370
+ this.isFirstChunk = true;
371
+ this._completeResults = {
372
+ data: [],
373
+ errors: [],
374
+ meta: {}
375
+ };
376
+ replaceConfig.call(this, config);
377
+
378
+ this.parseChunk = function(chunk, isFakeChunk)
379
+ {
380
+ // First chunk pre-processing
381
+ if (this.isFirstChunk && isFunction(this._config.beforeFirstChunk))
382
+ {
383
+ var modifiedChunk = this._config.beforeFirstChunk(chunk);
384
+ if (modifiedChunk !== undefined)
385
+ chunk = modifiedChunk;
386
+ }
387
+ this.isFirstChunk = false;
388
+ this._halted = false;
389
+
390
+ // Rejoin the line we likely just split in two by chunking the file
391
+ var aggregate = this._partialLine + chunk;
392
+ this._pendingSkip = parseInt(this._config.skipFirstNLines) || 0;
393
+ this._skipHeader = 0;
394
+ if (this._config.header) {
395
+ this._skipHeader++;
396
+ }
397
+ if (this._pendingSkip > 0) {
398
+ var splitChunk = aggregate.split('\n');
399
+ var currentChunkLength = splitChunk.length;
400
+ if (currentChunkLength <= this._pendingSkip) {
401
+ aggregate = this._partialLine;
402
+ }
403
+ else{
404
+ aggregate = this._partialLine + [...splitChunk.slice(0, this._skipHeader), ...splitChunk.slice(this._skipHeader + this._pendingSkip)].join('\n');
405
+ }
406
+ this._pendingSkip -= currentChunkLength;
407
+ }
408
+ this._partialLine = '';
409
+ var results = this._handle.parse(aggregate, this._baseIndex, !this._finished);
410
+
411
+ if (this._handle.paused() || this._handle.aborted()) {
412
+ this._halted = true;
413
+ return;
414
+ }
415
+
416
+ var lastIndex = results.meta.cursor;
417
+
418
+ if (!this._finished)
419
+ {
420
+ this._partialLine = aggregate.substring(lastIndex - this._baseIndex);
421
+ this._baseIndex = lastIndex;
422
+ }
423
+
424
+ if (results && results.data)
425
+ this._rowCount += results.data.length;
426
+
427
+ var finishedIncludingPreview = this._finished || (this._config.preview && this._rowCount >= this._config.preview);
428
+
429
+ if (IS_PAPA_WORKER)
430
+ {
431
+ global.postMessage({
432
+ results: results,
433
+ workerId: Papa.WORKER_ID,
434
+ finished: finishedIncludingPreview
435
+ });
436
+ }
437
+ else if (isFunction(this._config.chunk) && !isFakeChunk)
438
+ {
439
+ this._config.chunk(results, this._handle);
440
+ if (this._handle.paused() || this._handle.aborted()) {
441
+ this._halted = true;
442
+ return;
443
+ }
444
+ results = undefined;
445
+ this._completeResults = undefined;
446
+ }
447
+
448
+ if (!this._config.step && !this._config.chunk) {
449
+ this._completeResults.data = this._completeResults.data.concat(results.data);
450
+ this._completeResults.errors = this._completeResults.errors.concat(results.errors);
451
+ this._completeResults.meta = results.meta;
452
+ }
453
+
454
+ if (!this._completed && finishedIncludingPreview && isFunction(this._config.complete) && (!results || !results.meta.aborted)) {
455
+ this._config.complete(this._completeResults, this._input);
456
+ this._completed = true;
457
+ }
458
+
459
+ if (!finishedIncludingPreview && (!results || !results.meta.paused))
460
+ this._nextChunk();
461
+
462
+ return results;
463
+ };
464
+
465
+ this._sendError = function(error)
466
+ {
467
+ if (isFunction(this._config.error))
468
+ this._config.error(error);
469
+ else if (IS_PAPA_WORKER && this._config.error)
470
+ {
471
+ global.postMessage({
472
+ workerId: Papa.WORKER_ID,
473
+ error: error,
474
+ finished: false
475
+ });
476
+ }
477
+ };
478
+
479
+ function replaceConfig(config)
480
+ {
481
+ // Deep-copy the config so we can edit it
482
+ var configCopy = copy(config);
483
+ configCopy.chunkSize = parseInt(configCopy.chunkSize); // parseInt VERY important so we don't concatenate strings!
484
+ if (!config.step && !config.chunk)
485
+ configCopy.chunkSize = null; // disable Range header if not streaming; bad values break IIS - see issue #196
486
+ this._handle = new ParserHandle(configCopy);
487
+ this._handle.streamer = this;
488
+ this._config = configCopy; // persist the copy to the caller
489
+ }
490
+ }
491
+
492
+
493
+ function NetworkStreamer(config)
494
+ {
495
+ config = config || {};
496
+ if (!config.chunkSize)
497
+ config.chunkSize = Papa.RemoteChunkSize;
498
+ ChunkStreamer.call(this, config);
499
+
500
+ var xhr;
501
+
502
+ if (IS_WORKER)
503
+ {
504
+ this._nextChunk = function()
505
+ {
506
+ this._readChunk();
507
+ this._chunkLoaded();
508
+ };
509
+ }
510
+ else
511
+ {
512
+ this._nextChunk = function()
513
+ {
514
+ this._readChunk();
515
+ };
516
+ }
517
+
518
+ this.stream = function(url)
519
+ {
520
+ this._input = url;
521
+ this._nextChunk(); // Starts streaming
522
+ };
523
+
524
+ this._readChunk = function()
525
+ {
526
+ if (this._finished)
527
+ {
528
+ this._chunkLoaded();
529
+ return;
530
+ }
531
+
532
+ xhr = new XMLHttpRequest();
533
+
534
+ if (this._config.withCredentials)
535
+ {
536
+ xhr.withCredentials = this._config.withCredentials;
537
+ }
538
+
539
+ if (!IS_WORKER)
540
+ {
541
+ xhr.onload = bindFunction(this._chunkLoaded, this);
542
+ xhr.onerror = bindFunction(this._chunkError, this);
543
+ }
544
+
545
+ xhr.open(this._config.downloadRequestBody ? 'POST' : 'GET', this._input, !IS_WORKER);
546
+ // Headers can only be set when once the request state is OPENED
547
+ if (this._config.downloadRequestHeaders)
548
+ {
549
+ var headers = this._config.downloadRequestHeaders;
550
+
551
+ for (var headerName in headers)
552
+ {
553
+ xhr.setRequestHeader(headerName, headers[headerName]);
554
+ }
555
+ }
556
+
557
+ if (this._config.chunkSize)
558
+ {
559
+ var end = this._start + this._config.chunkSize - 1; // minus one because byte range is inclusive
560
+ xhr.setRequestHeader('Range', 'bytes=' + this._start + '-' + end);
561
+ }
562
+
563
+ try {
564
+ xhr.send(this._config.downloadRequestBody);
565
+ }
566
+ catch (err) {
567
+ this._chunkError(err.message);
568
+ }
569
+
570
+ if (IS_WORKER && xhr.status === 0)
571
+ this._chunkError();
572
+ };
573
+
574
+ this._chunkLoaded = function()
575
+ {
576
+ if (xhr.readyState !== 4)
577
+ return;
578
+
579
+ if (xhr.status < 200 || xhr.status >= 400)
580
+ {
581
+ this._chunkError();
582
+ return;
583
+ }
584
+
585
+ // Use chunckSize as it may be a diference on reponse lentgh due to characters with more than 1 byte
586
+ this._start += this._config.chunkSize ? this._config.chunkSize : xhr.responseText.length;
587
+ this._finished = !this._config.chunkSize || this._start >= getFileSize(xhr);
588
+ this.parseChunk(xhr.responseText);
589
+ };
590
+
591
+ this._chunkError = function(errorMessage)
592
+ {
593
+ var errorText = xhr.statusText || errorMessage;
594
+ this._sendError(new Error(errorText));
595
+ };
596
+
597
+ function getFileSize(xhr)
598
+ {
599
+ var contentRange = xhr.getResponseHeader('Content-Range');
600
+ if (contentRange === null) { // no content range, then finish!
601
+ return -1;
602
+ }
603
+ return parseInt(contentRange.substring(contentRange.lastIndexOf('/') + 1));
604
+ }
605
+ }
606
+ NetworkStreamer.prototype = Object.create(ChunkStreamer.prototype);
607
+ NetworkStreamer.prototype.constructor = NetworkStreamer;
608
+
609
+
610
+ function FileStreamer(config)
611
+ {
612
+ config = config || {};
613
+ if (!config.chunkSize)
614
+ config.chunkSize = Papa.LocalChunkSize;
615
+ ChunkStreamer.call(this, config);
616
+
617
+ var reader, slice;
618
+
619
+ // FileReader is better than FileReaderSync (even in worker) - see http://stackoverflow.com/q/24708649/1048862
620
+ // But Firefox is a pill, too - see issue #76: https://github.com/mholt/PapaParse/issues/76
621
+ var usingAsyncReader = typeof FileReader !== 'undefined'; // Safari doesn't consider it a function - see issue #105
622
+
623
+ this.stream = function(file)
624
+ {
625
+ this._input = file;
626
+ slice = file.slice || file.webkitSlice || file.mozSlice;
627
+
628
+ if (usingAsyncReader)
629
+ {
630
+ reader = new FileReader(); // Preferred method of reading files, even in workers
631
+ reader.onload = bindFunction(this._chunkLoaded, this);
632
+ reader.onerror = bindFunction(this._chunkError, this);
633
+ }
634
+ else
635
+ reader = new FileReaderSync(); // Hack for running in a web worker in Firefox
636
+
637
+ this._nextChunk(); // Starts streaming
638
+ };
639
+
640
+ this._nextChunk = function()
641
+ {
642
+ if (!this._finished && (!this._config.preview || this._rowCount < this._config.preview))
643
+ this._readChunk();
644
+ };
645
+
646
+ this._readChunk = function()
647
+ {
648
+ var input = this._input;
649
+ if (this._config.chunkSize)
650
+ {
651
+ var end = Math.min(this._start + this._config.chunkSize, this._input.size);
652
+ input = slice.call(input, this._start, end);
653
+ }
654
+ var txt = reader.readAsText(input, this._config.encoding);
655
+ if (!usingAsyncReader)
656
+ this._chunkLoaded({ target: { result: txt } }); // mimic the async signature
657
+ };
658
+
659
+ this._chunkLoaded = function(event)
660
+ {
661
+ // Very important to increment start each time before handling results
662
+ this._start += this._config.chunkSize;
663
+ this._finished = !this._config.chunkSize || this._start >= this._input.size;
664
+ this.parseChunk(event.target.result);
665
+ };
666
+
667
+ this._chunkError = function()
668
+ {
669
+ this._sendError(reader.error);
670
+ };
671
+
672
+ }
673
+ FileStreamer.prototype = Object.create(ChunkStreamer.prototype);
674
+ FileStreamer.prototype.constructor = FileStreamer;
675
+
676
+
677
+ function StringStreamer(config)
678
+ {
679
+ config = config || {};
680
+ ChunkStreamer.call(this, config);
681
+
682
+ var remaining;
683
+ this.stream = function(s)
684
+ {
685
+ remaining = s;
686
+ return this._nextChunk();
687
+ };
688
+ this._nextChunk = function()
689
+ {
690
+ if (this._finished) return;
691
+ var size = this._config.chunkSize;
692
+ var chunk;
693
+ if(size) {
694
+ chunk = remaining.substring(0, size);
695
+ remaining = remaining.substring(size);
696
+ } else {
697
+ chunk = remaining;
698
+ remaining = '';
699
+ }
700
+ this._finished = !remaining;
701
+ return this.parseChunk(chunk);
702
+ };
703
+ }
704
+ StringStreamer.prototype = Object.create(StringStreamer.prototype);
705
+ StringStreamer.prototype.constructor = StringStreamer;
706
+
707
+
708
+ function ReadableStreamStreamer(config)
709
+ {
710
+ config = config || {};
711
+
712
+ ChunkStreamer.call(this, config);
713
+
714
+ var queue = [];
715
+ var parseOnData = true;
716
+ var streamHasEnded = false;
717
+
718
+ this.pause = function()
719
+ {
720
+ ChunkStreamer.prototype.pause.apply(this, arguments);
721
+ this._input.pause();
722
+ };
723
+
724
+ this.resume = function()
725
+ {
726
+ ChunkStreamer.prototype.resume.apply(this, arguments);
727
+ this._input.resume();
728
+ };
729
+
730
+ this.stream = function(stream)
731
+ {
732
+ this._input = stream;
733
+
734
+ this._input.on('data', this._streamData);
735
+ this._input.on('end', this._streamEnd);
736
+ this._input.on('error', this._streamError);
737
+ };
738
+
739
+ this._checkIsFinished = function()
740
+ {
741
+ if (streamHasEnded && queue.length === 1) {
742
+ this._finished = true;
743
+ }
744
+ };
745
+
746
+ this._nextChunk = function()
747
+ {
748
+ this._checkIsFinished();
749
+ if (queue.length)
750
+ {
751
+ this.parseChunk(queue.shift());
752
+ }
753
+ else
754
+ {
755
+ parseOnData = true;
756
+ }
757
+ };
758
+
759
+ this._streamData = bindFunction(function(chunk)
760
+ {
761
+ try
762
+ {
763
+ queue.push(typeof chunk === 'string' ? chunk : chunk.toString(this._config.encoding));
764
+
765
+ if (parseOnData)
766
+ {
767
+ parseOnData = false;
768
+ this._checkIsFinished();
769
+ this.parseChunk(queue.shift());
770
+ }
771
+ }
772
+ catch (error)
773
+ {
774
+ this._streamError(error);
775
+ }
776
+ }, this);
777
+
778
+ this._streamError = bindFunction(function(error)
779
+ {
780
+ this._streamCleanUp();
781
+ this._sendError(error);
782
+ }, this);
783
+
784
+ this._streamEnd = bindFunction(function()
785
+ {
786
+ this._streamCleanUp();
787
+ streamHasEnded = true;
788
+ this._streamData('');
789
+ }, this);
790
+
791
+ this._streamCleanUp = bindFunction(function()
792
+ {
793
+ this._input.removeListener('data', this._streamData);
794
+ this._input.removeListener('end', this._streamEnd);
795
+ this._input.removeListener('error', this._streamError);
796
+ }, this);
797
+ }
798
+ ReadableStreamStreamer.prototype = Object.create(ChunkStreamer.prototype);
799
+ ReadableStreamStreamer.prototype.constructor = ReadableStreamStreamer;
800
+
801
+
802
+ function DuplexStreamStreamer(_config) {
803
+ var Duplex = require('stream').Duplex;
804
+ var config = copy(_config);
805
+ var parseOnWrite = true;
806
+ var writeStreamHasFinished = false;
807
+ var parseCallbackQueue = [];
808
+ var stream = null;
809
+
810
+ this._onCsvData = function(results)
811
+ {
812
+ var data = results.data;
813
+ if (!stream.push(data) && !this._handle.paused()) {
814
+ // the writeable consumer buffer has filled up
815
+ // so we need to pause until more items
816
+ // can be processed
817
+ this._handle.pause();
818
+ }
819
+ };
820
+
821
+ this._onCsvComplete = function()
822
+ {
823
+ // node will finish the read stream when
824
+ // null is pushed
825
+ stream.push(null);
826
+ };
827
+
828
+ config.step = bindFunction(this._onCsvData, this);
829
+ config.complete = bindFunction(this._onCsvComplete, this);
830
+ ChunkStreamer.call(this, config);
831
+
832
+ this._nextChunk = function()
833
+ {
834
+ if (writeStreamHasFinished && parseCallbackQueue.length === 1) {
835
+ this._finished = true;
836
+ }
837
+ if (parseCallbackQueue.length) {
838
+ parseCallbackQueue.shift()();
839
+ } else {
840
+ parseOnWrite = true;
841
+ }
842
+ };
843
+
844
+ this._addToParseQueue = function(chunk, callback)
845
+ {
846
+ // add to queue so that we can indicate
847
+ // completion via callback
848
+ // node will automatically pause the incoming stream
849
+ // when too many items have been added without their
850
+ // callback being invoked
851
+ parseCallbackQueue.push(bindFunction(function() {
852
+ this.parseChunk(typeof chunk === 'string' ? chunk : chunk.toString(config.encoding));
853
+ if (isFunction(callback)) {
854
+ return callback();
855
+ }
856
+ }, this));
857
+ if (parseOnWrite) {
858
+ parseOnWrite = false;
859
+ this._nextChunk();
860
+ }
861
+ };
862
+
863
+ this._onRead = function()
864
+ {
865
+ if (this._handle.paused()) {
866
+ // the writeable consumer can handle more data
867
+ // so resume the chunk parsing
868
+ this._handle.resume();
869
+ }
870
+ };
871
+
872
+ this._onWrite = function(chunk, encoding, callback)
873
+ {
874
+ this._addToParseQueue(chunk, callback);
875
+ };
876
+
877
+ this._onWriteComplete = function()
878
+ {
879
+ writeStreamHasFinished = true;
880
+ // have to write empty string
881
+ // so parser knows its done
882
+ this._addToParseQueue('');
883
+ };
884
+
885
+ this.getStream = function()
886
+ {
887
+ return stream;
888
+ };
889
+ stream = new Duplex({
890
+ readableObjectMode: true,
891
+ decodeStrings: false,
892
+ read: bindFunction(this._onRead, this),
893
+ write: bindFunction(this._onWrite, this)
894
+ });
895
+ stream.once('finish', bindFunction(this._onWriteComplete, this));
896
+ }
897
+ if (typeof PAPA_BROWSER_CONTEXT === 'undefined') {
898
+ DuplexStreamStreamer.prototype = Object.create(ChunkStreamer.prototype);
899
+ DuplexStreamStreamer.prototype.constructor = DuplexStreamStreamer;
900
+ }
901
+
902
+
903
+ // Use one ParserHandle per entire CSV file or string
904
+ function ParserHandle(_config)
905
+ {
906
+ // One goal is to minimize the use of regular expressions...
907
+ var MAX_FLOAT = Math.pow(2, 53);
908
+ var MIN_FLOAT = -MAX_FLOAT;
909
+ var FLOAT = /^\s*-?(\d+\.?|\.\d+|\d+\.\d+)([eE][-+]?\d+)?\s*$/;
910
+ var ISO_DATE = /^((\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d\.\d+([+-][0-2]\d:[0-5]\d|Z))|(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d([+-][0-2]\d:[0-5]\d|Z))|(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d([+-][0-2]\d:[0-5]\d|Z)))$/;
911
+ var self = this;
912
+ var _stepCounter = 0; // Number of times step was called (number of rows parsed)
913
+ var _rowCounter = 0; // Number of rows that have been parsed so far
914
+ var _input; // The input being parsed
915
+ var _parser; // The core parser being used
916
+ var _paused = false; // Whether we are paused or not
917
+ var _aborted = false; // Whether the parser has aborted or not
918
+ var _delimiterError; // Temporary state between delimiter detection and processing results
919
+ var _fields = []; // Fields are from the header row of the input, if there is one
920
+ var _results = { // The last results returned from the parser
921
+ data: [],
922
+ errors: [],
923
+ meta: {}
924
+ };
925
+
926
+ if (isFunction(_config.step))
927
+ {
928
+ var userStep = _config.step;
929
+ _config.step = function(results)
930
+ {
931
+ _results = results;
932
+
933
+ if (needsHeaderRow())
934
+ processResults();
935
+ else // only call user's step function after header row
936
+ {
937
+ processResults();
938
+
939
+ // It's possbile that this line was empty and there's no row here after all
940
+ if (_results.data.length === 0)
941
+ return;
942
+
943
+ _stepCounter += results.data.length;
944
+ if (_config.preview && _stepCounter > _config.preview)
945
+ _parser.abort();
946
+ else {
947
+ _results.data = _results.data[0];
948
+ userStep(_results, self);
949
+ }
950
+ }
951
+ };
952
+ }
953
+
954
+ /**
955
+ * Parses input. Most users won't need, and shouldn't mess with, the baseIndex
956
+ * and ignoreLastRow parameters. They are used by streamers (wrapper functions)
957
+ * when an input comes in multiple chunks, like from a file.
958
+ */
959
+ this.parse = function(input, baseIndex, ignoreLastRow)
960
+ {
961
+ var quoteChar = _config.quoteChar || '"';
962
+ if (!_config.newline)
963
+ _config.newline = guessLineEndings(input, quoteChar);
964
+
965
+ _delimiterError = false;
966
+ if (!_config.delimiter)
967
+ {
968
+ var delimGuess = guessDelimiter(input, _config.newline, _config.skipEmptyLines, _config.comments, _config.delimitersToGuess);
969
+ if (delimGuess.successful)
970
+ _config.delimiter = delimGuess.bestDelimiter;
971
+ else
972
+ {
973
+ _delimiterError = true; // add error after parsing (otherwise it would be overwritten)
974
+ _config.delimiter = Papa.DefaultDelimiter;
975
+ }
976
+ _results.meta.delimiter = _config.delimiter;
977
+ }
978
+ else if(isFunction(_config.delimiter))
979
+ {
980
+ _config.delimiter = _config.delimiter(input);
981
+ _results.meta.delimiter = _config.delimiter;
982
+ }
983
+
984
+ var parserConfig = copy(_config);
985
+ if (_config.preview && _config.header)
986
+ parserConfig.preview++; // to compensate for header row
987
+
988
+ _input = input;
989
+ _parser = new Parser(parserConfig);
990
+ _results = _parser.parse(_input, baseIndex, ignoreLastRow);
991
+ processResults();
992
+ return _paused ? { meta: { paused: true } } : (_results || { meta: { paused: false } });
993
+ };
994
+
995
+ this.paused = function()
996
+ {
997
+ return _paused;
998
+ };
999
+
1000
+ this.pause = function()
1001
+ {
1002
+ _paused = true;
1003
+ _parser.abort();
1004
+
1005
+ // If it is streaming via "chunking", the reader will start appending correctly already so no need to substring,
1006
+ // otherwise we can get duplicate content within a row
1007
+ _input = isFunction(_config.chunk) ? "" : _input.substring(_parser.getCharIndex());
1008
+ };
1009
+
1010
+ this.resume = function()
1011
+ {
1012
+ if(self.streamer._halted) {
1013
+ _paused = false;
1014
+ self.streamer.parseChunk(_input, true);
1015
+ } else {
1016
+ // Bugfix: #636 In case the processing hasn't halted yet
1017
+ // wait for it to halt in order to resume
1018
+ setTimeout(self.resume, 3);
1019
+ }
1020
+ };
1021
+
1022
+ this.aborted = function()
1023
+ {
1024
+ return _aborted;
1025
+ };
1026
+
1027
+ this.abort = function()
1028
+ {
1029
+ _aborted = true;
1030
+ _parser.abort();
1031
+ _results.meta.aborted = true;
1032
+ if (isFunction(_config.complete))
1033
+ _config.complete(_results);
1034
+ _input = '';
1035
+ };
1036
+
1037
+ function testEmptyLine(s) {
1038
+ return _config.skipEmptyLines === 'greedy' ? s.join('').trim() === '' : s.length === 1 && s[0].length === 0;
1039
+ }
1040
+
1041
+ function testFloat(s) {
1042
+ if (FLOAT.test(s)) {
1043
+ var floatValue = parseFloat(s);
1044
+ if (floatValue > MIN_FLOAT && floatValue < MAX_FLOAT) {
1045
+ return true;
1046
+ }
1047
+ }
1048
+ return false;
1049
+ }
1050
+
1051
+ function processResults()
1052
+ {
1053
+ if (_results && _delimiterError)
1054
+ {
1055
+ addError('Delimiter', 'UndetectableDelimiter', 'Unable to auto-detect delimiting character; defaulted to \'' + Papa.DefaultDelimiter + '\'');
1056
+ _delimiterError = false;
1057
+ }
1058
+
1059
+ if (_config.skipEmptyLines)
1060
+ {
1061
+ _results.data = _results.data.filter(function(d) {
1062
+ return !testEmptyLine(d);
1063
+ });
1064
+ }
1065
+
1066
+ if (needsHeaderRow())
1067
+ fillHeaderFields();
1068
+
1069
+ return applyHeaderAndDynamicTypingAndTransformation();
1070
+ }
1071
+
1072
+ function needsHeaderRow()
1073
+ {
1074
+ return _config.header && _fields.length === 0;
1075
+ }
1076
+
1077
+ function fillHeaderFields()
1078
+ {
1079
+ if (!_results)
1080
+ return;
1081
+
1082
+ function addHeader(header, i)
1083
+ {
1084
+ if (isFunction(_config.transformHeader))
1085
+ header = _config.transformHeader(header, i);
1086
+
1087
+ _fields.push(header);
1088
+ }
1089
+
1090
+ if (Array.isArray(_results.data[0]))
1091
+ {
1092
+ for (var i = 0; needsHeaderRow() && i < _results.data.length; i++)
1093
+ _results.data[i].forEach(addHeader);
1094
+
1095
+ _results.data.splice(0, 1);
1096
+ }
1097
+ // if _results.data[0] is not an array, we are in a step where _results.data is the row.
1098
+ else
1099
+ _results.data.forEach(addHeader);
1100
+ }
1101
+
1102
+ function shouldApplyDynamicTyping(field) {
1103
+ // Cache function values to avoid calling it for each row
1104
+ if (_config.dynamicTypingFunction && _config.dynamicTyping[field] === undefined) {
1105
+ _config.dynamicTyping[field] = _config.dynamicTypingFunction(field);
1106
+ }
1107
+ return (_config.dynamicTyping[field] || _config.dynamicTyping) === true;
1108
+ }
1109
+
1110
+ function parseDynamic(field, value)
1111
+ {
1112
+ if (shouldApplyDynamicTyping(field))
1113
+ {
1114
+ if (value === 'true' || value === 'TRUE')
1115
+ return true;
1116
+ else if (value === 'false' || value === 'FALSE')
1117
+ return false;
1118
+ else if (testFloat(value))
1119
+ return parseFloat(value);
1120
+ else if (ISO_DATE.test(value))
1121
+ return new Date(value);
1122
+ else
1123
+ return (value === '' ? null : value);
1124
+ }
1125
+ return value;
1126
+ }
1127
+
1128
+ function applyHeaderAndDynamicTypingAndTransformation()
1129
+ {
1130
+ if (!_results || (!_config.header && !_config.dynamicTyping && !_config.transform))
1131
+ return _results;
1132
+
1133
+ function processRow(rowSource, i)
1134
+ {
1135
+ var row = _config.header ? {} : [];
1136
+
1137
+ var j;
1138
+ for (j = 0; j < rowSource.length; j++)
1139
+ {
1140
+ var field = j;
1141
+ var value = rowSource[j];
1142
+
1143
+ if (_config.header)
1144
+ field = j >= _fields.length ? '__parsed_extra' : _fields[j];
1145
+
1146
+ if (_config.transform)
1147
+ value = _config.transform(value,field);
1148
+
1149
+ value = parseDynamic(field, value);
1150
+
1151
+ if (field === '__parsed_extra')
1152
+ {
1153
+ row[field] = row[field] || [];
1154
+ row[field].push(value);
1155
+ }
1156
+ else
1157
+ row[field] = value;
1158
+ }
1159
+
1160
+
1161
+ if (_config.header)
1162
+ {
1163
+ if (j > _fields.length)
1164
+ addError('FieldMismatch', 'TooManyFields', 'Too many fields: expected ' + _fields.length + ' fields but parsed ' + j, _rowCounter + i);
1165
+ else if (j < _fields.length)
1166
+ addError('FieldMismatch', 'TooFewFields', 'Too few fields: expected ' + _fields.length + ' fields but parsed ' + j, _rowCounter + i);
1167
+ }
1168
+
1169
+ return row;
1170
+ }
1171
+
1172
+ var incrementBy = 1;
1173
+ if (!_results.data.length || Array.isArray(_results.data[0]))
1174
+ {
1175
+ _results.data = _results.data.map(processRow);
1176
+ incrementBy = _results.data.length;
1177
+ }
1178
+ else
1179
+ _results.data = processRow(_results.data, 0);
1180
+
1181
+
1182
+ if (_config.header && _results.meta)
1183
+ _results.meta.fields = _fields;
1184
+
1185
+ _rowCounter += incrementBy;
1186
+ return _results;
1187
+ }
1188
+
1189
+ function guessDelimiter(input, newline, skipEmptyLines, comments, delimitersToGuess) {
1190
+ var bestDelim, bestDelta, fieldCountPrevRow, maxFieldCount;
1191
+
1192
+ delimitersToGuess = delimitersToGuess || [',', '\t', '|', ';', Papa.RECORD_SEP, Papa.UNIT_SEP];
1193
+
1194
+ for (var i = 0; i < delimitersToGuess.length; i++) {
1195
+ var delim = delimitersToGuess[i];
1196
+ var delta = 0, avgFieldCount = 0, emptyLinesCount = 0;
1197
+ fieldCountPrevRow = undefined;
1198
+
1199
+ var preview = new Parser({
1200
+ comments: comments,
1201
+ delimiter: delim,
1202
+ newline: newline,
1203
+ preview: 10
1204
+ }).parse(input);
1205
+
1206
+ for (var j = 0; j < preview.data.length; j++) {
1207
+ if (skipEmptyLines && testEmptyLine(preview.data[j])) {
1208
+ emptyLinesCount++;
1209
+ continue;
1210
+ }
1211
+ var fieldCount = preview.data[j].length;
1212
+ avgFieldCount += fieldCount;
1213
+
1214
+ if (typeof fieldCountPrevRow === 'undefined') {
1215
+ fieldCountPrevRow = fieldCount;
1216
+ continue;
1217
+ }
1218
+ else if (fieldCount > 0) {
1219
+ delta += Math.abs(fieldCount - fieldCountPrevRow);
1220
+ fieldCountPrevRow = fieldCount;
1221
+ }
1222
+ }
1223
+
1224
+ if (preview.data.length > 0)
1225
+ avgFieldCount /= (preview.data.length - emptyLinesCount);
1226
+
1227
+ if ((typeof bestDelta === 'undefined' || delta <= bestDelta)
1228
+ && (typeof maxFieldCount === 'undefined' || avgFieldCount > maxFieldCount) && avgFieldCount > 1.99) {
1229
+ bestDelta = delta;
1230
+ bestDelim = delim;
1231
+ maxFieldCount = avgFieldCount;
1232
+ }
1233
+ }
1234
+
1235
+ _config.delimiter = bestDelim;
1236
+
1237
+ return {
1238
+ successful: !!bestDelim,
1239
+ bestDelimiter: bestDelim
1240
+ };
1241
+ }
1242
+
1243
+ function guessLineEndings(input, quoteChar)
1244
+ {
1245
+ input = input.substring(0, 1024 * 1024); // max length 1 MB
1246
+ // Replace all the text inside quotes
1247
+ var re = new RegExp(escapeRegExp(quoteChar) + '([^]*?)' + escapeRegExp(quoteChar), 'gm');
1248
+ input = input.replace(re, '');
1249
+
1250
+ var r = input.split('\r');
1251
+
1252
+ var n = input.split('\n');
1253
+
1254
+ var nAppearsFirst = (n.length > 1 && n[0].length < r[0].length);
1255
+
1256
+ if (r.length === 1 || nAppearsFirst)
1257
+ return '\n';
1258
+
1259
+ var numWithN = 0;
1260
+ for (var i = 0; i < r.length; i++)
1261
+ {
1262
+ if (r[i][0] === '\n')
1263
+ numWithN++;
1264
+ }
1265
+
1266
+ return numWithN >= r.length / 2 ? '\r\n' : '\r';
1267
+ }
1268
+
1269
+ function addError(type, code, msg, row)
1270
+ {
1271
+ var error = {
1272
+ type: type,
1273
+ code: code,
1274
+ message: msg
1275
+ };
1276
+ if(row !== undefined) {
1277
+ error.row = row;
1278
+ }
1279
+ _results.errors.push(error);
1280
+ }
1281
+ }
1282
+
1283
+ /** https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions */
1284
+ function escapeRegExp(string)
1285
+ {
1286
+ return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
1287
+ }
1288
+
1289
+ /** The core parser implements speedy and correct CSV parsing */
1290
+ function Parser(config)
1291
+ {
1292
+ // Unpack the config object
1293
+ config = config || {};
1294
+ var delim = config.delimiter;
1295
+ var newline = config.newline;
1296
+ var comments = config.comments;
1297
+ var step = config.step;
1298
+ var preview = config.preview;
1299
+ var fastMode = config.fastMode;
1300
+ var quoteChar;
1301
+ var renamedHeaders = null;
1302
+
1303
+ if (config.quoteChar === undefined || config.quoteChar === null) {
1304
+ quoteChar = '"';
1305
+ } else {
1306
+ quoteChar = config.quoteChar;
1307
+ }
1308
+ var escapeChar = quoteChar;
1309
+ if (config.escapeChar !== undefined) {
1310
+ escapeChar = config.escapeChar;
1311
+ }
1312
+
1313
+ // Delimiter must be valid
1314
+ if (typeof delim !== 'string'
1315
+ || Papa.BAD_DELIMITERS.indexOf(delim) > -1)
1316
+ delim = ',';
1317
+
1318
+ // Comment character must be valid
1319
+ if (comments === delim)
1320
+ throw new Error('Comment character same as delimiter');
1321
+ else if (comments === true)
1322
+ comments = '#';
1323
+ else if (typeof comments !== 'string'
1324
+ || Papa.BAD_DELIMITERS.indexOf(comments) > -1)
1325
+ comments = false;
1326
+
1327
+ // Newline must be valid: \r, \n, or \r\n
1328
+ if (newline !== '\n' && newline !== '\r' && newline !== '\r\n')
1329
+ newline = '\n';
1330
+
1331
+ // We're gonna need these at the Parser scope
1332
+ var cursor = 0;
1333
+ var aborted = false;
1334
+
1335
+ this.parse = function(input, baseIndex, ignoreLastRow)
1336
+ {
1337
+ // For some reason, in Chrome, this speeds things up (!?)
1338
+ if (typeof input !== 'string')
1339
+ throw new Error('Input must be a string');
1340
+
1341
+ // We don't need to compute some of these every time parse() is called,
1342
+ // but having them in a more local scope seems to perform better
1343
+ var inputLen = input.length,
1344
+ delimLen = delim.length,
1345
+ newlineLen = newline.length,
1346
+ commentsLen = comments.length;
1347
+ var stepIsFunction = isFunction(step);
1348
+
1349
+ // Establish starting state
1350
+ cursor = 0;
1351
+ var data = [], errors = [], row = [], lastCursor = 0;
1352
+
1353
+ if (!input)
1354
+ return returnable();
1355
+
1356
+ // Rename headers if there are duplicates
1357
+ var firstLine;
1358
+ if (config.header && !baseIndex)
1359
+ {
1360
+ firstLine = input.split(newline)[0];
1361
+ var headers = firstLine.split(delim);
1362
+ var separator = '_';
1363
+ var headerMap = new Set();
1364
+ var headerCount = {};
1365
+ var duplicateHeaders = false;
1366
+
1367
+ // Using old-style 'for' loop to avoid prototype pollution that would be picked up with 'var j in headers'
1368
+ for (var j = 0; j < headers.length; j++) {
1369
+ var header = headers[j];
1370
+ if (isFunction(config.transformHeader))
1371
+ header = config.transformHeader(header, j);
1372
+ var headerName = header;
1373
+
1374
+ var count = headerCount[header] || 0;
1375
+ if (count > 0) {
1376
+ duplicateHeaders = true;
1377
+ headerName = header + separator + count;
1378
+ // Initialise the variable if it hasn't been.
1379
+ if (renamedHeaders === null) {
1380
+ renamedHeaders = {};
1381
+ }
1382
+ }
1383
+ headerCount[header] = count + 1;
1384
+ // In case it already exists, we add more separators
1385
+ while (headerMap.has(headerName)) {
1386
+ headerName = headerName + separator + count;
1387
+ }
1388
+ headerMap.add(headerName);
1389
+ if (count > 0) {
1390
+ renamedHeaders[headerName] = header;
1391
+ }
1392
+ }
1393
+ if (duplicateHeaders) {
1394
+ var editedInput = input.split(newline);
1395
+ editedInput[0] = Array.from(headerMap).join(delim);
1396
+ input = editedInput.join(newline);
1397
+ }
1398
+ }
1399
+ if (fastMode || (fastMode !== false && input.indexOf(quoteChar) === -1))
1400
+ {
1401
+ var rows = input.split(newline);
1402
+ for (var i = 0; i < rows.length; i++)
1403
+ {
1404
+ row = rows[i];
1405
+ // use firstline as row length may be changed due to duplicated headers
1406
+ if (i === 0 && firstLine !== undefined) {
1407
+ cursor += firstLine.length;
1408
+ }else{
1409
+ cursor += row.length;
1410
+ }
1411
+ if (i !== rows.length - 1)
1412
+ cursor += newline.length;
1413
+ else if (ignoreLastRow)
1414
+ return returnable();
1415
+ if (comments && row.substring(0, commentsLen) === comments)
1416
+ continue;
1417
+ if (stepIsFunction)
1418
+ {
1419
+ data = [];
1420
+ pushRow(row.split(delim));
1421
+ doStep();
1422
+ if (aborted)
1423
+ return returnable();
1424
+ }
1425
+ else
1426
+ pushRow(row.split(delim));
1427
+ if (preview && i >= preview)
1428
+ {
1429
+ data = data.slice(0, preview);
1430
+ return returnable(true);
1431
+ }
1432
+ }
1433
+ return returnable();
1434
+ }
1435
+
1436
+ var nextDelim = input.indexOf(delim, cursor);
1437
+ var nextNewline = input.indexOf(newline, cursor);
1438
+ var quoteCharRegex = new RegExp(escapeRegExp(escapeChar) + escapeRegExp(quoteChar), 'g');
1439
+ var quoteSearch = input.indexOf(quoteChar, cursor);
1440
+
1441
+ // Parser loop
1442
+ for (;;)
1443
+ {
1444
+ // Field has opening quote
1445
+ if (input[cursor] === quoteChar)
1446
+ {
1447
+ // Start our search for the closing quote where the cursor is
1448
+ quoteSearch = cursor;
1449
+
1450
+ // Skip the opening quote
1451
+ cursor++;
1452
+
1453
+ for (;;)
1454
+ {
1455
+ // Find closing quote
1456
+ quoteSearch = input.indexOf(quoteChar, quoteSearch + 1);
1457
+
1458
+ //No other quotes are found - no other delimiters
1459
+ if (quoteSearch === -1)
1460
+ {
1461
+ if (!ignoreLastRow) {
1462
+ // No closing quote... what a pity
1463
+ errors.push({
1464
+ type: 'Quotes',
1465
+ code: 'MissingQuotes',
1466
+ message: 'Quoted field unterminated',
1467
+ row: data.length, // row has yet to be inserted
1468
+ index: cursor
1469
+ });
1470
+ }
1471
+ return finish();
1472
+ }
1473
+
1474
+ // Closing quote at EOF
1475
+ if (quoteSearch === inputLen - 1)
1476
+ {
1477
+ var value = input.substring(cursor, quoteSearch).replace(quoteCharRegex, quoteChar);
1478
+ return finish(value);
1479
+ }
1480
+
1481
+ // If this quote is escaped, it's part of the data; skip it
1482
+ // If the quote character is the escape character, then check if the next character is the escape character
1483
+ if (quoteChar === escapeChar && input[quoteSearch + 1] === escapeChar)
1484
+ {
1485
+ quoteSearch++;
1486
+ continue;
1487
+ }
1488
+
1489
+ // If the quote character is not the escape character, then check if the previous character was the escape character
1490
+ if (quoteChar !== escapeChar && quoteSearch !== 0 && input[quoteSearch - 1] === escapeChar)
1491
+ {
1492
+ continue;
1493
+ }
1494
+
1495
+ if(nextDelim !== -1 && nextDelim < (quoteSearch + 1)) {
1496
+ nextDelim = input.indexOf(delim, (quoteSearch + 1));
1497
+ }
1498
+ if(nextNewline !== -1 && nextNewline < (quoteSearch + 1)) {
1499
+ nextNewline = input.indexOf(newline, (quoteSearch + 1));
1500
+ }
1501
+ // Check up to nextDelim or nextNewline, whichever is closest
1502
+ var checkUpTo = nextNewline === -1 ? nextDelim : Math.min(nextDelim, nextNewline);
1503
+ var spacesBetweenQuoteAndDelimiter = extraSpaces(checkUpTo);
1504
+
1505
+ // Closing quote followed by delimiter or 'unnecessary spaces + delimiter'
1506
+ if (input.substr(quoteSearch + 1 + spacesBetweenQuoteAndDelimiter, delimLen) === delim)
1507
+ {
1508
+ row.push(input.substring(cursor, quoteSearch).replace(quoteCharRegex, quoteChar));
1509
+ cursor = quoteSearch + 1 + spacesBetweenQuoteAndDelimiter + delimLen;
1510
+
1511
+ // If char after following delimiter is not quoteChar, we find next quote char position
1512
+ if (input[quoteSearch + 1 + spacesBetweenQuoteAndDelimiter + delimLen] !== quoteChar)
1513
+ {
1514
+ quoteSearch = input.indexOf(quoteChar, cursor);
1515
+ }
1516
+ nextDelim = input.indexOf(delim, cursor);
1517
+ nextNewline = input.indexOf(newline, cursor);
1518
+ break;
1519
+ }
1520
+
1521
+ var spacesBetweenQuoteAndNewLine = extraSpaces(nextNewline);
1522
+
1523
+ // Closing quote followed by newline or 'unnecessary spaces + newLine'
1524
+ if (input.substring(quoteSearch + 1 + spacesBetweenQuoteAndNewLine, quoteSearch + 1 + spacesBetweenQuoteAndNewLine + newlineLen) === newline)
1525
+ {
1526
+ row.push(input.substring(cursor, quoteSearch).replace(quoteCharRegex, quoteChar));
1527
+ saveRow(quoteSearch + 1 + spacesBetweenQuoteAndNewLine + newlineLen);
1528
+ nextDelim = input.indexOf(delim, cursor); // because we may have skipped the nextDelim in the quoted field
1529
+ quoteSearch = input.indexOf(quoteChar, cursor); // we search for first quote in next line
1530
+
1531
+ if (stepIsFunction)
1532
+ {
1533
+ doStep();
1534
+ if (aborted)
1535
+ return returnable();
1536
+ }
1537
+
1538
+ if (preview && data.length >= preview)
1539
+ return returnable(true);
1540
+
1541
+ break;
1542
+ }
1543
+
1544
+
1545
+ // Checks for valid closing quotes are complete (escaped quotes or quote followed by EOF/delimiter/newline) -- assume these quotes are part of an invalid text string
1546
+ errors.push({
1547
+ type: 'Quotes',
1548
+ code: 'InvalidQuotes',
1549
+ message: 'Trailing quote on quoted field is malformed',
1550
+ row: data.length, // row has yet to be inserted
1551
+ index: cursor
1552
+ });
1553
+
1554
+ quoteSearch++;
1555
+ continue;
1556
+
1557
+ }
1558
+
1559
+ continue;
1560
+ }
1561
+
1562
+ // Comment found at start of new line
1563
+ if (comments && row.length === 0 && input.substring(cursor, cursor + commentsLen) === comments)
1564
+ {
1565
+ if (nextNewline === -1) // Comment ends at EOF
1566
+ return returnable();
1567
+ cursor = nextNewline + newlineLen;
1568
+ nextNewline = input.indexOf(newline, cursor);
1569
+ nextDelim = input.indexOf(delim, cursor);
1570
+ continue;
1571
+ }
1572
+
1573
+ // Next delimiter comes before next newline, so we've reached end of field
1574
+ if (nextDelim !== -1 && (nextDelim < nextNewline || nextNewline === -1))
1575
+ {
1576
+ row.push(input.substring(cursor, nextDelim));
1577
+ cursor = nextDelim + delimLen;
1578
+ // we look for next delimiter char
1579
+ nextDelim = input.indexOf(delim, cursor);
1580
+ continue;
1581
+ }
1582
+
1583
+ // End of row
1584
+ if (nextNewline !== -1)
1585
+ {
1586
+ row.push(input.substring(cursor, nextNewline));
1587
+ saveRow(nextNewline + newlineLen);
1588
+
1589
+ if (stepIsFunction)
1590
+ {
1591
+ doStep();
1592
+ if (aborted)
1593
+ return returnable();
1594
+ }
1595
+
1596
+ if (preview && data.length >= preview)
1597
+ return returnable(true);
1598
+
1599
+ continue;
1600
+ }
1601
+
1602
+ break;
1603
+ }
1604
+
1605
+
1606
+ return finish();
1607
+
1608
+
1609
+ function pushRow(row)
1610
+ {
1611
+ data.push(row);
1612
+ lastCursor = cursor;
1613
+ }
1614
+
1615
+ /**
1616
+ * checks if there are extra spaces after closing quote and given index without any text
1617
+ * if Yes, returns the number of spaces
1618
+ */
1619
+ function extraSpaces(index) {
1620
+ var spaceLength = 0;
1621
+ if (index !== -1) {
1622
+ var textBetweenClosingQuoteAndIndex = input.substring(quoteSearch + 1, index);
1623
+ if (textBetweenClosingQuoteAndIndex && textBetweenClosingQuoteAndIndex.trim() === '') {
1624
+ spaceLength = textBetweenClosingQuoteAndIndex.length;
1625
+ }
1626
+ }
1627
+ return spaceLength;
1628
+ }
1629
+
1630
+ /**
1631
+ * Appends the remaining input from cursor to the end into
1632
+ * row, saves the row, calls step, and returns the results.
1633
+ */
1634
+ function finish(value)
1635
+ {
1636
+ if (ignoreLastRow)
1637
+ return returnable();
1638
+ if (typeof value === 'undefined')
1639
+ value = input.substring(cursor);
1640
+ row.push(value);
1641
+ cursor = inputLen; // important in case parsing is paused
1642
+ pushRow(row);
1643
+ if (stepIsFunction)
1644
+ doStep();
1645
+ return returnable();
1646
+ }
1647
+
1648
+ /**
1649
+ * Appends the current row to the results. It sets the cursor
1650
+ * to newCursor and finds the nextNewline. The caller should
1651
+ * take care to execute user's step function and check for
1652
+ * preview and end parsing if necessary.
1653
+ */
1654
+ function saveRow(newCursor)
1655
+ {
1656
+ cursor = newCursor;
1657
+ pushRow(row);
1658
+ row = [];
1659
+ nextNewline = input.indexOf(newline, cursor);
1660
+ }
1661
+
1662
+ /** Returns an object with the results, errors, and meta. */
1663
+ function returnable(stopped)
1664
+ {
1665
+ return {
1666
+ data: data,
1667
+ errors: errors,
1668
+ meta: {
1669
+ delimiter: delim,
1670
+ linebreak: newline,
1671
+ aborted: aborted,
1672
+ truncated: !!stopped,
1673
+ cursor: lastCursor + (baseIndex || 0),
1674
+ renamedHeaders: renamedHeaders
1675
+ }
1676
+ };
1677
+ }
1678
+
1679
+ /** Executes the user's step function and resets data & errors. */
1680
+ function doStep()
1681
+ {
1682
+ step(returnable());
1683
+ data = [];
1684
+ errors = [];
1685
+ }
1686
+ };
1687
+
1688
+ /** Sets the abort flag */
1689
+ this.abort = function()
1690
+ {
1691
+ aborted = true;
1692
+ };
1693
+
1694
+ /** Gets the cursor position */
1695
+ this.getCharIndex = function()
1696
+ {
1697
+ return cursor;
1698
+ };
1699
+ }
1700
+
1701
+
1702
+ function newWorker()
1703
+ {
1704
+ if (!Papa.WORKERS_SUPPORTED)
1705
+ return false;
1706
+
1707
+ var workerUrl = getWorkerBlob();
1708
+ var w = new global.Worker(workerUrl);
1709
+ w.onmessage = mainThreadReceivedMessage;
1710
+ w.id = workerIdCounter++;
1711
+ workers[w.id] = w;
1712
+ return w;
1713
+ }
1714
+
1715
+ /** Callback when main thread receives a message */
1716
+ function mainThreadReceivedMessage(e)
1717
+ {
1718
+ var msg = e.data;
1719
+ var worker = workers[msg.workerId];
1720
+ var aborted = false;
1721
+
1722
+ if (msg.error)
1723
+ worker.userError(msg.error, msg.file);
1724
+ else if (msg.results && msg.results.data)
1725
+ {
1726
+ var abort = function() {
1727
+ aborted = true;
1728
+ completeWorker(msg.workerId, { data: [], errors: [], meta: { aborted: true } });
1729
+ };
1730
+
1731
+ var handle = {
1732
+ abort: abort,
1733
+ pause: notImplemented,
1734
+ resume: notImplemented
1735
+ };
1736
+
1737
+ if (isFunction(worker.userStep))
1738
+ {
1739
+ for (var i = 0; i < msg.results.data.length; i++)
1740
+ {
1741
+ worker.userStep({
1742
+ data: msg.results.data[i],
1743
+ errors: msg.results.errors,
1744
+ meta: msg.results.meta
1745
+ }, handle);
1746
+ if (aborted)
1747
+ break;
1748
+ }
1749
+ delete msg.results; // free memory ASAP
1750
+ }
1751
+ else if (isFunction(worker.userChunk))
1752
+ {
1753
+ worker.userChunk(msg.results, handle, msg.file);
1754
+ delete msg.results;
1755
+ }
1756
+ }
1757
+
1758
+ if (msg.finished && !aborted)
1759
+ completeWorker(msg.workerId, msg.results);
1760
+ }
1761
+
1762
+ function completeWorker(workerId, results) {
1763
+ var worker = workers[workerId];
1764
+ if (isFunction(worker.userComplete))
1765
+ worker.userComplete(results);
1766
+ worker.terminate();
1767
+ delete workers[workerId];
1768
+ }
1769
+
1770
+ function notImplemented() {
1771
+ throw new Error('Not implemented.');
1772
+ }
1773
+
1774
+ /** Callback when worker thread receives a message */
1775
+ function workerThreadReceivedMessage(e)
1776
+ {
1777
+ var msg = e.data;
1778
+
1779
+ if (typeof Papa.WORKER_ID === 'undefined' && msg)
1780
+ Papa.WORKER_ID = msg.workerId;
1781
+
1782
+ if (typeof msg.input === 'string')
1783
+ {
1784
+ global.postMessage({
1785
+ workerId: Papa.WORKER_ID,
1786
+ results: Papa.parse(msg.input, msg.config),
1787
+ finished: true
1788
+ });
1789
+ }
1790
+ else if ((global.File && msg.input instanceof File) || msg.input instanceof Object) // thank you, Safari (see issue #106)
1791
+ {
1792
+ var results = Papa.parse(msg.input, msg.config);
1793
+ if (results)
1794
+ global.postMessage({
1795
+ workerId: Papa.WORKER_ID,
1796
+ results: results,
1797
+ finished: true
1798
+ });
1799
+ }
1800
+ }
1801
+
1802
+ /** Makes a deep copy of an array or object (mostly) */
1803
+ function copy(obj)
1804
+ {
1805
+ if (typeof obj !== 'object' || obj === null)
1806
+ return obj;
1807
+ var cpy = Array.isArray(obj) ? [] : {};
1808
+ for (var key in obj)
1809
+ cpy[key] = copy(obj[key]);
1810
+ return cpy;
1811
+ }
1812
+
1813
+ function bindFunction(f, self)
1814
+ {
1815
+ return function() { f.apply(self, arguments); };
1816
+ }
1817
+ function isFunction(func)
1818
+ {
1819
+ return typeof func === 'function';
1820
+ }
1821
+
1822
+ export default Papa;
1823
+
1824
+ export {CsvToJson, CsvToJson as parse,JsonToCsv,JsonToCsv as unparse};