@loaders.gl/csv 3.4.0-alpha.1 → 3.4.0-alpha.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,11 +1,9 @@
1
-
2
1
  /* @license
3
2
  Papa Parse
4
3
  v5.0.0-beta.0
5
4
  https://github.com/mholt/PapaParse
6
5
  License: MIT
7
6
  */
8
-
9
7
  const BYTE_ORDER_MARK = '\ufeff';
10
8
  const Papa = {
11
9
  parse: CsvToJson,
@@ -19,17 +17,13 @@ const Papa = {
19
17
  LocalChunkSize: 1024 * 1024 * 10,
20
18
  RemoteChunkSize: 1024 * 1024 * 5,
21
19
  DefaultDelimiter: ',',
22
-
23
20
  Parser: Parser,
24
21
  ParserHandle: ParserHandle,
25
22
  ChunkStreamer: ChunkStreamer,
26
23
  StringStreamer: StringStreamer
27
24
  };
28
25
  export default Papa;
29
-
30
- function
31
-
32
- CsvToJson(_input, _config, UserDefinedStreamer) {
26
+ function CsvToJson(_input, _config, UserDefinedStreamer) {
33
27
  _config = _config || {};
34
28
  var dynamicTyping = _config.dynamicTyping || false;
35
29
  if (isFunction(dynamicTyping)) {
@@ -49,7 +43,6 @@ CsvToJson(_input, _config, UserDefinedStreamer) {
49
43
  _config.complete = isFunction(_config.complete);
50
44
  _config.error = isFunction(_config.error);
51
45
  delete _config.worker;
52
-
53
46
  w.postMessage({
54
47
  input: _input,
55
48
  config: _config,
@@ -61,29 +54,19 @@ CsvToJson(_input, _config, UserDefinedStreamer) {
61
54
  if (typeof _input === 'string') {
62
55
  streamer = new StringStreamer(_config);
63
56
  }
64
-
65
57
  if (!streamer) {
66
58
  streamer = new UserDefinedStreamer(_config);
67
59
  }
68
-
69
60
  return streamer.stream(_input);
70
61
  }
71
62
  function JsonToCsv(_input, _config) {
72
-
73
63
  var _quotes = false;
74
-
75
64
  var _writeHeader = true;
76
-
77
65
  var _delimiter = ',';
78
-
79
66
  var _newline = '\r\n';
80
-
81
67
  var _quoteChar = '"';
82
-
83
68
  var _escapedQuote = _quoteChar + _quoteChar;
84
-
85
69
  var _skipEmptyLines = false;
86
-
87
70
  var _columns = null;
88
71
  unpackConfig();
89
72
  var quoteCharRegex = new RegExp(escapeRegExp(_quoteChar), 'g');
@@ -97,10 +80,8 @@ function JsonToCsv(_input, _config) {
97
80
  if (!_input.fields) _input.fields = Array.isArray(_input.data[0]) ? _input.fields : objectKeys(_input.data[0]);
98
81
  if (!Array.isArray(_input.data[0]) && typeof _input.data[0] !== 'object') _input.data = [_input.data];
99
82
  }
100
-
101
83
  return serialize(_input.fields || [], _input.data || [], _skipEmptyLines);
102
84
  }
103
-
104
85
  throw new Error('Unable to serialize unrecognized input');
105
86
  function unpackConfig() {
106
87
  if (typeof _config !== 'object') return;
@@ -122,21 +103,18 @@ function JsonToCsv(_input, _config) {
122
103
  _escapedQuote = _config.escapeChar + _quoteChar;
123
104
  }
124
105
  }
125
-
126
106
  function objectKeys(obj) {
127
107
  if (typeof obj !== 'object') return [];
128
108
  var keys = [];
129
109
  for (var key in obj) keys.push(key);
130
110
  return keys;
131
111
  }
132
-
133
112
  function serialize(fields, data, skipEmptyLines) {
134
113
  var csv = '';
135
114
  if (typeof fields === 'string') fields = JSON.parse(fields);
136
115
  if (typeof data === 'string') data = JSON.parse(data);
137
116
  var hasHeader = Array.isArray(fields) && fields.length > 0;
138
117
  var dataKeyedByField = !Array.isArray(data[0]);
139
-
140
118
  if (hasHeader && _writeHeader) {
141
119
  for (var i = 0; i < fields.length; i++) {
142
120
  if (i > 0) csv += _delimiter;
@@ -144,7 +122,6 @@ function JsonToCsv(_input, _config) {
144
122
  }
145
123
  if (data.length > 0) csv += _newline;
146
124
  }
147
-
148
125
  for (var row = 0; row < data.length; row++) {
149
126
  var maxCol = hasHeader ? fields.length : data[row].length;
150
127
  var emptyLine = false;
@@ -173,7 +150,6 @@ function JsonToCsv(_input, _config) {
173
150
  }
174
151
  return csv;
175
152
  }
176
-
177
153
  function safe(str, col) {
178
154
  if (typeof str === 'undefined' || str === null) return '';
179
155
  if (str.constructor === Date) return JSON.stringify(str).slice(1, 25);
@@ -186,7 +162,6 @@ function JsonToCsv(_input, _config) {
186
162
  return false;
187
163
  }
188
164
  }
189
-
190
165
  function ChunkStreamer(config) {
191
166
  this._handle = null;
192
167
  this._finished = false;
@@ -210,7 +185,6 @@ function ChunkStreamer(config) {
210
185
  if (modifiedChunk !== undefined) chunk = modifiedChunk;
211
186
  }
212
187
  this.isFirstChunk = false;
213
-
214
188
  var aggregate = this._partialLine + chunk;
215
189
  this._partialLine = '';
216
190
  var results = this._handle.parse(aggregate, this._baseIndex, !this._finished);
@@ -252,7 +226,6 @@ function ChunkStreamer(config) {
252
226
  this._config = configCopy;
253
227
  }
254
228
  }
255
-
256
229
  function StringStreamer(config) {
257
230
  config = config || {};
258
231
  ChunkStreamer.call(this, config);
@@ -272,7 +245,6 @@ function StringStreamer(config) {
272
245
  }
273
246
  StringStreamer.prototype = Object.create(StringStreamer.prototype);
274
247
  StringStreamer.prototype.constructor = StringStreamer;
275
-
276
248
  function ParserHandle(_config) {
277
249
  var FLOAT = /^\s*-?(\d*\.?\d+|\d+\.?\d*)(e[-+]?\d+)?\s*$/i;
278
250
  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))/;
@@ -294,17 +266,14 @@ function ParserHandle(_config) {
294
266
  var userStep = _config.step;
295
267
  _config.step = function (results) {
296
268
  _results = results;
297
- if (needsHeaderRow()) processResults();
298
- else {
269
+ if (needsHeaderRow()) processResults();else {
299
270
  processResults();
300
-
301
271
  if (!_results.data || _results.data.length === 0) return;
302
272
  _stepCounter += results.data.length;
303
273
  if (_config.preview && _stepCounter > _config.preview) _parser.abort();else userStep(_results, self);
304
274
  }
305
275
  };
306
276
  }
307
-
308
277
  this.parse = function (input, baseIndex, ignoreLastRow) {
309
278
  var quoteChar = _config.quoteChar || '"';
310
279
  if (!_config.newline) _config.newline = guessLineEndings(input, quoteChar);
@@ -322,7 +291,6 @@ function ParserHandle(_config) {
322
291
  }
323
292
  var parserConfig = copy(_config);
324
293
  if (_config.preview && _config.header) parserConfig.preview++;
325
-
326
294
  _input = input;
327
295
  _parser = new Parser(parserConfig);
328
296
  _results = _parser.parse(_input, baseIndex, ignoreLastRow);
@@ -385,8 +353,7 @@ function ParserHandle(_config) {
385
353
  if (Array.isArray(_results.data[0])) {
386
354
  for (var i = 0; needsHeaderRow() && i < _results.data.length; i++) _results.data[i].forEach(addHeder);
387
355
  _results.data.splice(0, 1);
388
- }
389
- else _results.data.forEach(addHeder);
356
+ } else _results.data.forEach(addHeder);
390
357
  }
391
358
  function shouldApplyDynamicTyping(field) {
392
359
  if (_config.dynamicTypingFunction && _config.dynamicTyping[field] === undefined) {
@@ -495,11 +462,9 @@ function ParserHandle(_config) {
495
462
  });
496
463
  }
497
464
  }
498
-
499
465
  function escapeRegExp(string) {
500
466
  return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
501
467
  }
502
-
503
468
  function Parser(config) {
504
469
  config = config || {};
505
470
  var delim = config.delimiter;
@@ -518,24 +483,18 @@ function Parser(config) {
518
483
  if (config.escapeChar !== undefined) {
519
484
  escapeChar = config.escapeChar;
520
485
  }
521
-
522
486
  if (typeof delim !== 'string' || Papa.BAD_DELIMITERS.indexOf(delim) > -1) delim = ',';
523
-
524
487
  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
-
526
488
  if (newline !== '\n' && newline !== '\r' && newline !== '\r\n') newline = '\n';
527
-
528
489
  var cursor = 0;
529
490
  var aborted = false;
530
491
  this.parse = function (input, baseIndex, ignoreLastRow) {
531
492
  if (typeof input !== 'string') throw new Error('Input must be a string');
532
-
533
493
  var inputLen = input.length,
534
494
  delimLen = delim.length,
535
495
  newlineLen = newline.length,
536
496
  commentsLen = comments.length;
537
497
  var stepIsFunction = isFunction(step);
538
-
539
498
  cursor = 0;
540
499
  var data = [],
541
500
  errors = [],
@@ -566,15 +525,12 @@ function Parser(config) {
566
525
  var nextNewline = input.indexOf(newline, cursor);
567
526
  var quoteCharRegex = new RegExp(escapeRegExp(escapeChar) + escapeRegExp(quoteChar), 'g');
568
527
  var quoteSearch;
569
-
570
528
  for (;;) {
571
529
  if (input[cursor] === quoteChar) {
572
530
  quoteSearch = cursor;
573
-
574
531
  cursor++;
575
532
  for (;;) {
576
533
  quoteSearch = input.indexOf(quoteChar, quoteSearch + 1);
577
-
578
534
  if (quoteSearch === -1) {
579
535
  if (!ignoreLastRow) {
580
536
  errors.push({
@@ -587,24 +543,19 @@ function Parser(config) {
587
543
  }
588
544
  return finish();
589
545
  }
590
-
591
546
  if (quoteSearch === inputLen - 1) {
592
547
  var value = input.substring(cursor, quoteSearch).replace(quoteCharRegex, quoteChar);
593
548
  return finish(value);
594
549
  }
595
-
596
550
  if (quoteChar === escapeChar && input[quoteSearch + 1] === escapeChar) {
597
551
  quoteSearch++;
598
552
  continue;
599
553
  }
600
-
601
554
  if (quoteChar !== escapeChar && quoteSearch !== 0 && input[quoteSearch - 1] === escapeChar) {
602
555
  continue;
603
556
  }
604
-
605
557
  var checkUpTo = nextNewline === -1 ? nextDelim : Math.min(nextDelim, nextNewline);
606
558
  var spacesBetweenQuoteAndDelimiter = extraSpaces(checkUpTo);
607
-
608
559
  if (input[quoteSearch + 1 + spacesBetweenQuoteAndDelimiter] === delim) {
609
560
  row.push(input.substring(cursor, quoteSearch).replace(quoteCharRegex, quoteChar));
610
561
  cursor = quoteSearch + 1 + spacesBetweenQuoteAndDelimiter + delimLen;
@@ -618,12 +569,10 @@ function Parser(config) {
618
569
  break;
619
570
  }
620
571
  var spacesBetweenQuoteAndNewLine = extraSpaces(nextNewline);
621
-
622
572
  if (input.substr(quoteSearch + 1 + spacesBetweenQuoteAndNewLine, newlineLen) === newline) {
623
573
  row.push(input.substring(cursor, quoteSearch).replace(quoteCharRegex, quoteChar));
624
574
  saveRow(quoteSearch + 1 + spacesBetweenQuoteAndNewLine + newlineLen);
625
575
  nextDelim = input.indexOf(delim, cursor);
626
-
627
576
  if (stepIsFunction) {
628
577
  doStep();
629
578
  if (aborted) return returnable();
@@ -631,7 +580,6 @@ function Parser(config) {
631
580
  if (preview && data.length >= preview) return returnable(true);
632
581
  break;
633
582
  }
634
-
635
583
  errors.push({
636
584
  type: 'Quotes',
637
585
  code: 'InvalidQuotes',
@@ -649,23 +597,19 @@ function Parser(config) {
649
597
  if (preview && data.length >= preview) return returnable(true);
650
598
  continue;
651
599
  }
652
-
653
600
  if (comments && row.length === 0 && input.substr(cursor, commentsLen) === comments) {
654
- if (nextNewline === -1)
655
- return returnable();
601
+ if (nextNewline === -1) return returnable();
656
602
  cursor = nextNewline + newlineLen;
657
603
  nextNewline = input.indexOf(newline, cursor);
658
604
  nextDelim = input.indexOf(delim, cursor);
659
605
  continue;
660
606
  }
661
-
662
607
  if (nextDelim !== -1 && (nextDelim < nextNewline || nextNewline === -1)) {
663
608
  row.push(input.substring(cursor, nextDelim));
664
609
  cursor = nextDelim + delimLen;
665
610
  nextDelim = input.indexOf(delim, cursor);
666
611
  continue;
667
612
  }
668
-
669
613
  if (nextNewline !== -1) {
670
614
  row.push(input.substring(cursor, nextNewline));
671
615
  saveRow(nextNewline + newlineLen);
@@ -683,7 +627,6 @@ function Parser(config) {
683
627
  data.push(row);
684
628
  lastCursor = cursor;
685
629
  }
686
-
687
630
  function extraSpaces(index) {
688
631
  var spaceLength = 0;
689
632
  if (index !== -1) {
@@ -694,7 +637,6 @@ function Parser(config) {
694
637
  }
695
638
  return spaceLength;
696
639
  }
697
-
698
640
  function finish(value) {
699
641
  if (ignoreLastRow) return returnable();
700
642
  if (typeof value === 'undefined') value = input.substr(cursor);
@@ -704,14 +646,12 @@ function Parser(config) {
704
646
  if (stepIsFunction) doStep();
705
647
  return returnable();
706
648
  }
707
-
708
649
  function saveRow(newCursor) {
709
650
  cursor = newCursor;
710
651
  pushRow(row);
711
652
  row = [];
712
653
  nextNewline = input.indexOf(newline, cursor);
713
654
  }
714
-
715
655
  function returnable(stopped, step) {
716
656
  var isStep = step || false;
717
657
  return {
@@ -726,18 +666,15 @@ function Parser(config) {
726
666
  }
727
667
  };
728
668
  }
729
-
730
669
  function doStep() {
731
670
  step(returnable(undefined, true));
732
671
  data = [];
733
672
  errors = [];
734
673
  }
735
674
  };
736
-
737
675
  this.abort = function () {
738
676
  aborted = true;
739
677
  };
740
-
741
678
  this.getCharIndex = function () {
742
679
  return cursor;
743
680
  };
@@ -745,7 +682,6 @@ function Parser(config) {
745
682
  function notImplemented() {
746
683
  throw new Error('Not implemented.');
747
684
  }
748
-
749
685
  function copy(obj) {
750
686
  if (typeof obj !== 'object' || obj === null) return obj;
751
687
  var cpy = Array.isArray(obj) ? [] : {};