@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.
@@ -12,7 +12,6 @@ v5.0.0-beta.0
12
12
  https://github.com/mholt/PapaParse
13
13
  License: MIT
14
14
  */
15
-
16
15
  var BYTE_ORDER_MARK = "\uFEFF";
17
16
  var Papa = {
18
17
  parse: CsvToJson,
@@ -26,7 +25,6 @@ var Papa = {
26
25
  LocalChunkSize: 1024 * 1024 * 10,
27
26
  RemoteChunkSize: 1024 * 1024 * 5,
28
27
  DefaultDelimiter: ',',
29
-
30
28
  Parser: Parser,
31
29
  ParserHandle: ParserHandle,
32
30
  ChunkStreamer: ChunkStreamer,
@@ -34,10 +32,7 @@ var Papa = {
34
32
  };
35
33
  var _default = Papa;
36
34
  exports.default = _default;
37
-
38
- function
39
-
40
- CsvToJson(_input, _config, UserDefinedStreamer) {
35
+ function CsvToJson(_input, _config, UserDefinedStreamer) {
41
36
  _config = _config || {};
42
37
  var dynamicTyping = _config.dynamicTyping || false;
43
38
  if (isFunction(dynamicTyping)) {
@@ -57,7 +52,6 @@ CsvToJson(_input, _config, UserDefinedStreamer) {
57
52
  _config.complete = isFunction(_config.complete);
58
53
  _config.error = isFunction(_config.error);
59
54
  delete _config.worker;
60
-
61
55
  w.postMessage({
62
56
  input: _input,
63
57
  config: _config,
@@ -69,29 +63,19 @@ CsvToJson(_input, _config, UserDefinedStreamer) {
69
63
  if (typeof _input === 'string') {
70
64
  streamer = new StringStreamer(_config);
71
65
  }
72
-
73
66
  if (!streamer) {
74
67
  streamer = new UserDefinedStreamer(_config);
75
68
  }
76
-
77
69
  return streamer.stream(_input);
78
70
  }
79
71
  function JsonToCsv(_input, _config) {
80
-
81
72
  var _quotes = false;
82
-
83
73
  var _writeHeader = true;
84
-
85
74
  var _delimiter = ',';
86
-
87
75
  var _newline = '\r\n';
88
-
89
76
  var _quoteChar = '"';
90
-
91
77
  var _escapedQuote = _quoteChar + _quoteChar;
92
-
93
78
  var _skipEmptyLines = false;
94
-
95
79
  var _columns = null;
96
80
  unpackConfig();
97
81
  var quoteCharRegex = new RegExp(escapeRegExp(_quoteChar), 'g');
@@ -105,10 +89,8 @@ function JsonToCsv(_input, _config) {
105
89
  if (!_input.fields) _input.fields = Array.isArray(_input.data[0]) ? _input.fields : objectKeys(_input.data[0]);
106
90
  if (!Array.isArray(_input.data[0]) && (0, _typeof2.default)(_input.data[0]) !== 'object') _input.data = [_input.data];
107
91
  }
108
-
109
92
  return serialize(_input.fields || [], _input.data || [], _skipEmptyLines);
110
93
  }
111
-
112
94
  throw new Error('Unable to serialize unrecognized input');
113
95
  function unpackConfig() {
114
96
  if ((0, _typeof2.default)(_config) !== 'object') return;
@@ -130,23 +112,18 @@ function JsonToCsv(_input, _config) {
130
112
  _escapedQuote = _config.escapeChar + _quoteChar;
131
113
  }
132
114
  }
133
-
134
115
  function objectKeys(obj) {
135
116
  if ((0, _typeof2.default)(obj) !== 'object') return [];
136
117
  var keys = [];
137
- for (var key in obj) {
138
- keys.push(key);
139
- }
118
+ for (var key in obj) keys.push(key);
140
119
  return keys;
141
120
  }
142
-
143
121
  function serialize(fields, data, skipEmptyLines) {
144
122
  var csv = '';
145
123
  if (typeof fields === 'string') fields = JSON.parse(fields);
146
124
  if (typeof data === 'string') data = JSON.parse(data);
147
125
  var hasHeader = Array.isArray(fields) && fields.length > 0;
148
126
  var dataKeyedByField = !Array.isArray(data[0]);
149
-
150
127
  if (hasHeader && _writeHeader) {
151
128
  for (var i = 0; i < fields.length; i++) {
152
129
  if (i > 0) csv += _delimiter;
@@ -154,7 +131,6 @@ function JsonToCsv(_input, _config) {
154
131
  }
155
132
  if (data.length > 0) csv += _newline;
156
133
  }
157
-
158
134
  for (var row = 0; row < data.length; row++) {
159
135
  var maxCol = hasHeader ? fields.length : data[row].length;
160
136
  var emptyLine = false;
@@ -183,7 +159,6 @@ function JsonToCsv(_input, _config) {
183
159
  }
184
160
  return csv;
185
161
  }
186
-
187
162
  function safe(str, col) {
188
163
  if (typeof str === 'undefined' || str === null) return '';
189
164
  if (str.constructor === Date) return JSON.stringify(str).slice(1, 25);
@@ -192,13 +167,10 @@ function JsonToCsv(_input, _config) {
192
167
  return needsQuotes ? _quoteChar + str + _quoteChar : str;
193
168
  }
194
169
  function hasAny(str, substrings) {
195
- for (var i = 0; i < substrings.length; i++) {
196
- if (str.indexOf(substrings[i]) > -1) return true;
197
- }
170
+ for (var i = 0; i < substrings.length; i++) if (str.indexOf(substrings[i]) > -1) return true;
198
171
  return false;
199
172
  }
200
173
  }
201
-
202
174
  function ChunkStreamer(config) {
203
175
  this._handle = null;
204
176
  this._finished = false;
@@ -222,7 +194,6 @@ function ChunkStreamer(config) {
222
194
  if (modifiedChunk !== undefined) chunk = modifiedChunk;
223
195
  }
224
196
  this.isFirstChunk = false;
225
-
226
197
  var aggregate = this._partialLine + chunk;
227
198
  this._partialLine = '';
228
199
  var results = this._handle.parse(aggregate, this._baseIndex, !this._finished);
@@ -264,7 +235,6 @@ function ChunkStreamer(config) {
264
235
  this._config = configCopy;
265
236
  }
266
237
  }
267
-
268
238
  function StringStreamer(config) {
269
239
  config = config || {};
270
240
  ChunkStreamer.call(this, config);
@@ -284,7 +254,6 @@ function StringStreamer(config) {
284
254
  }
285
255
  StringStreamer.prototype = Object.create(StringStreamer.prototype);
286
256
  StringStreamer.prototype.constructor = StringStreamer;
287
-
288
257
  function ParserHandle(_config) {
289
258
  var FLOAT = /^\s*-?(\d*\.?\d+|\d+\.?\d*)(e[-+]?\d+)?\s*$/i;
290
259
  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))/;
@@ -306,17 +275,14 @@ function ParserHandle(_config) {
306
275
  var userStep = _config.step;
307
276
  _config.step = function (results) {
308
277
  _results = results;
309
- if (needsHeaderRow()) processResults();
310
- else {
278
+ if (needsHeaderRow()) processResults();else {
311
279
  processResults();
312
-
313
280
  if (!_results.data || _results.data.length === 0) return;
314
281
  _stepCounter += results.data.length;
315
282
  if (_config.preview && _stepCounter > _config.preview) _parser.abort();else userStep(_results, self);
316
283
  }
317
284
  };
318
285
  }
319
-
320
286
  this.parse = function (input, baseIndex, ignoreLastRow) {
321
287
  var quoteChar = _config.quoteChar || '"';
322
288
  if (!_config.newline) _config.newline = guessLineEndings(input, quoteChar);
@@ -334,7 +300,6 @@ function ParserHandle(_config) {
334
300
  }
335
301
  var parserConfig = copy(_config);
336
302
  if (_config.preview && _config.header) parserConfig.preview++;
337
-
338
303
  _input = input;
339
304
  _parser = new Parser(parserConfig);
340
305
  _results = _parser.parse(_input, baseIndex, ignoreLastRow);
@@ -380,9 +345,7 @@ function ParserHandle(_config) {
380
345
  _delimiterError = false;
381
346
  }
382
347
  if (_config.skipEmptyLines) {
383
- for (var i = 0; i < _results.data.length; i++) {
384
- if (testEmptyLine(_results.data[i])) _results.data.splice(i--, 1);
385
- }
348
+ for (var i = 0; i < _results.data.length; i++) if (testEmptyLine(_results.data[i])) _results.data.splice(i--, 1);
386
349
  }
387
350
  if (needsHeaderRow()) fillHeaderFields();
388
351
  return applyHeaderAndDynamicTypingAndTransformation();
@@ -397,12 +360,9 @@ function ParserHandle(_config) {
397
360
  _fields.push(header);
398
361
  }
399
362
  if (Array.isArray(_results.data[0])) {
400
- for (var i = 0; needsHeaderRow() && i < _results.data.length; i++) {
401
- _results.data[i].forEach(addHeder);
402
- }
363
+ for (var i = 0; needsHeaderRow() && i < _results.data.length; i++) _results.data[i].forEach(addHeder);
403
364
  _results.data.splice(0, 1);
404
- }
405
- else _results.data.forEach(addHeder);
365
+ } else _results.data.forEach(addHeder);
406
366
  }
407
367
  function shouldApplyDynamicTyping(field) {
408
368
  if (_config.dynamicTypingFunction && _config.dynamicTyping[field] === undefined) {
@@ -511,11 +471,9 @@ function ParserHandle(_config) {
511
471
  });
512
472
  }
513
473
  }
514
-
515
474
  function escapeRegExp(string) {
516
475
  return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
517
476
  }
518
-
519
477
  function Parser(config) {
520
478
  config = config || {};
521
479
  var delim = config.delimiter;
@@ -534,24 +492,18 @@ function Parser(config) {
534
492
  if (config.escapeChar !== undefined) {
535
493
  escapeChar = config.escapeChar;
536
494
  }
537
-
538
495
  if (typeof delim !== 'string' || Papa.BAD_DELIMITERS.indexOf(delim) > -1) delim = ',';
539
-
540
496
  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
-
542
497
  if (newline !== '\n' && newline !== '\r' && newline !== '\r\n') newline = '\n';
543
-
544
498
  var cursor = 0;
545
499
  var aborted = false;
546
500
  this.parse = function (input, baseIndex, ignoreLastRow) {
547
501
  if (typeof input !== 'string') throw new Error('Input must be a string');
548
-
549
502
  var inputLen = input.length,
550
503
  delimLen = delim.length,
551
504
  newlineLen = newline.length,
552
505
  commentsLen = comments.length;
553
506
  var stepIsFunction = isFunction(step);
554
-
555
507
  cursor = 0;
556
508
  var data = [],
557
509
  errors = [],
@@ -582,15 +534,12 @@ function Parser(config) {
582
534
  var nextNewline = input.indexOf(newline, cursor);
583
535
  var quoteCharRegex = new RegExp(escapeRegExp(escapeChar) + escapeRegExp(quoteChar), 'g');
584
536
  var quoteSearch;
585
-
586
537
  for (;;) {
587
538
  if (input[cursor] === quoteChar) {
588
539
  quoteSearch = cursor;
589
-
590
540
  cursor++;
591
541
  for (;;) {
592
542
  quoteSearch = input.indexOf(quoteChar, quoteSearch + 1);
593
-
594
543
  if (quoteSearch === -1) {
595
544
  if (!ignoreLastRow) {
596
545
  errors.push({
@@ -603,24 +552,19 @@ function Parser(config) {
603
552
  }
604
553
  return finish();
605
554
  }
606
-
607
555
  if (quoteSearch === inputLen - 1) {
608
556
  var value = input.substring(cursor, quoteSearch).replace(quoteCharRegex, quoteChar);
609
557
  return finish(value);
610
558
  }
611
-
612
559
  if (quoteChar === escapeChar && input[quoteSearch + 1] === escapeChar) {
613
560
  quoteSearch++;
614
561
  continue;
615
562
  }
616
-
617
563
  if (quoteChar !== escapeChar && quoteSearch !== 0 && input[quoteSearch - 1] === escapeChar) {
618
564
  continue;
619
565
  }
620
-
621
566
  var checkUpTo = nextNewline === -1 ? nextDelim : Math.min(nextDelim, nextNewline);
622
567
  var spacesBetweenQuoteAndDelimiter = extraSpaces(checkUpTo);
623
-
624
568
  if (input[quoteSearch + 1 + spacesBetweenQuoteAndDelimiter] === delim) {
625
569
  row.push(input.substring(cursor, quoteSearch).replace(quoteCharRegex, quoteChar));
626
570
  cursor = quoteSearch + 1 + spacesBetweenQuoteAndDelimiter + delimLen;
@@ -634,12 +578,10 @@ function Parser(config) {
634
578
  break;
635
579
  }
636
580
  var spacesBetweenQuoteAndNewLine = extraSpaces(nextNewline);
637
-
638
581
  if (input.substr(quoteSearch + 1 + spacesBetweenQuoteAndNewLine, newlineLen) === newline) {
639
582
  row.push(input.substring(cursor, quoteSearch).replace(quoteCharRegex, quoteChar));
640
583
  saveRow(quoteSearch + 1 + spacesBetweenQuoteAndNewLine + newlineLen);
641
584
  nextDelim = input.indexOf(delim, cursor);
642
-
643
585
  if (stepIsFunction) {
644
586
  doStep();
645
587
  if (aborted) return returnable();
@@ -647,7 +589,6 @@ function Parser(config) {
647
589
  if (preview && data.length >= preview) return returnable(true);
648
590
  break;
649
591
  }
650
-
651
592
  errors.push({
652
593
  type: 'Quotes',
653
594
  code: 'InvalidQuotes',
@@ -665,23 +606,19 @@ function Parser(config) {
665
606
  if (preview && data.length >= preview) return returnable(true);
666
607
  continue;
667
608
  }
668
-
669
609
  if (comments && row.length === 0 && input.substr(cursor, commentsLen) === comments) {
670
- if (nextNewline === -1)
671
- return returnable();
610
+ if (nextNewline === -1) return returnable();
672
611
  cursor = nextNewline + newlineLen;
673
612
  nextNewline = input.indexOf(newline, cursor);
674
613
  nextDelim = input.indexOf(delim, cursor);
675
614
  continue;
676
615
  }
677
-
678
616
  if (nextDelim !== -1 && (nextDelim < nextNewline || nextNewline === -1)) {
679
617
  row.push(input.substring(cursor, nextDelim));
680
618
  cursor = nextDelim + delimLen;
681
619
  nextDelim = input.indexOf(delim, cursor);
682
620
  continue;
683
621
  }
684
-
685
622
  if (nextNewline !== -1) {
686
623
  row.push(input.substring(cursor, nextNewline));
687
624
  saveRow(nextNewline + newlineLen);
@@ -699,7 +636,6 @@ function Parser(config) {
699
636
  data.push(row);
700
637
  lastCursor = cursor;
701
638
  }
702
-
703
639
  function extraSpaces(index) {
704
640
  var spaceLength = 0;
705
641
  if (index !== -1) {
@@ -710,7 +646,6 @@ function Parser(config) {
710
646
  }
711
647
  return spaceLength;
712
648
  }
713
-
714
649
  function finish(value) {
715
650
  if (ignoreLastRow) return returnable();
716
651
  if (typeof value === 'undefined') value = input.substr(cursor);
@@ -720,14 +655,12 @@ function Parser(config) {
720
655
  if (stepIsFunction) doStep();
721
656
  return returnable();
722
657
  }
723
-
724
658
  function saveRow(newCursor) {
725
659
  cursor = newCursor;
726
660
  pushRow(row);
727
661
  row = [];
728
662
  nextNewline = input.indexOf(newline, cursor);
729
663
  }
730
-
731
664
  function returnable(stopped, step) {
732
665
  var isStep = step || false;
733
666
  return {
@@ -742,18 +675,15 @@ function Parser(config) {
742
675
  }
743
676
  };
744
677
  }
745
-
746
678
  function doStep() {
747
679
  step(returnable(undefined, true));
748
680
  data = [];
749
681
  errors = [];
750
682
  }
751
683
  };
752
-
753
684
  this.abort = function () {
754
685
  aborted = true;
755
686
  };
756
-
757
687
  this.getCharIndex = function () {
758
688
  return cursor;
759
689
  };
@@ -761,13 +691,10 @@ function Parser(config) {
761
691
  function notImplemented() {
762
692
  throw new Error('Not implemented.');
763
693
  }
764
-
765
694
  function copy(obj) {
766
695
  if ((0, _typeof2.default)(obj) !== 'object' || obj === null) return obj;
767
696
  var cpy = Array.isArray(obj) ? [] : {};
768
- for (var key in obj) {
769
- cpy[key] = copy(obj[key]);
770
- }
697
+ for (var key in obj) cpy[key] = copy(obj[key]);
771
698
  return cpy;
772
699
  }
773
700
  function isFunction(func) {