@loaders.gl/csv 4.3.1 → 4.4.0-alpha.1

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.
Files changed (49) hide show
  1. package/dist/csv-arrow-loader.d.ts +37 -0
  2. package/dist/csv-arrow-loader.d.ts.map +1 -0
  3. package/dist/csv-arrow-loader.js +23 -0
  4. package/dist/csv-format.d.ts +10 -0
  5. package/dist/csv-format.d.ts.map +1 -0
  6. package/dist/csv-format.js +12 -0
  7. package/dist/csv-loader.d.ts +6 -6
  8. package/dist/csv-loader.d.ts.map +1 -1
  9. package/dist/csv-loader.js +53 -20
  10. package/dist/csv-writer.d.ts +6 -5
  11. package/dist/csv-writer.d.ts.map +1 -1
  12. package/dist/csv-writer.js +2 -5
  13. package/dist/dist.dev.js +13318 -449
  14. package/dist/dist.min.js +23 -20
  15. package/dist/index.cjs +317 -262
  16. package/dist/index.cjs.map +4 -4
  17. package/dist/index.d.ts +2 -0
  18. package/dist/index.d.ts.map +1 -1
  19. package/dist/index.js +1 -0
  20. package/dist/lib/encoders/encode-csv.d.ts +1 -1
  21. package/dist/lib/encoders/encode-csv.d.ts.map +1 -1
  22. package/dist/lib/encoders/encode-csv.js +1 -1
  23. package/dist/papaparse/async-iterator-streamer.d.ts +1 -21
  24. package/dist/papaparse/async-iterator-streamer.d.ts.map +1 -1
  25. package/dist/papaparse/async-iterator-streamer.js +6 -6
  26. package/dist/papaparse/papa-constants.d.ts +12 -0
  27. package/dist/papaparse/papa-constants.d.ts.map +1 -0
  28. package/dist/papaparse/papa-constants.js +19 -0
  29. package/dist/papaparse/papa-parser.d.ts +110 -0
  30. package/dist/papaparse/papa-parser.d.ts.map +1 -0
  31. package/dist/papaparse/papa-parser.js +733 -0
  32. package/dist/papaparse/papa-writer.d.ts +22 -0
  33. package/dist/papaparse/papa-writer.d.ts.map +1 -0
  34. package/dist/papaparse/papa-writer.js +166 -0
  35. package/dist/papaparse/papaparse.d.ts +9 -113
  36. package/dist/papaparse/papaparse.d.ts.map +1 -1
  37. package/dist/papaparse/papaparse.js +13 -882
  38. package/package.json +5 -5
  39. package/src/csv-arrow-loader.ts +41 -0
  40. package/src/csv-format.ts +15 -0
  41. package/src/csv-loader.ts +58 -25
  42. package/src/csv-writer.ts +2 -5
  43. package/src/index.ts +3 -0
  44. package/src/lib/encoders/encode-csv.ts +2 -1
  45. package/src/papaparse/async-iterator-streamer.ts +6 -6
  46. package/src/papaparse/papa-constants.ts +23 -0
  47. package/src/papaparse/papa-parser.ts +872 -0
  48. package/src/papaparse/papa-writer.ts +219 -0
  49. package/src/papaparse/papaparse.ts +17 -1048
package/dist/index.cjs CHANGED
@@ -20,149 +20,40 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // dist/index.js
21
21
  var dist_exports = {};
22
22
  __export(dist_exports, {
23
+ CSVArrowLoader: () => CSVArrowLoader,
23
24
  CSVLoader: () => CSVLoader,
24
25
  CSVWriter: () => CSVWriter
25
26
  });
26
27
  module.exports = __toCommonJS(dist_exports);
27
28
 
28
29
  // dist/csv-loader.js
29
- var import_schema = require("@loaders.gl/schema");
30
+ var import_loader_utils = require("@loaders.gl/loader-utils");
31
+ var import_schema_utils = require("@loaders.gl/schema-utils");
30
32
 
31
- // dist/papaparse/papaparse.js
33
+ // dist/papaparse/papa-constants.js
32
34
  var BYTE_ORDER_MARK = "\uFEFF";
35
+ var Papa = {
36
+ RECORD_SEP: String.fromCharCode(30),
37
+ UNIT_SEP: String.fromCharCode(31),
38
+ BYTE_ORDER_MARK,
39
+ BAD_DELIMITERS: ["\r", "\n", '"', BYTE_ORDER_MARK],
40
+ WORKERS_SUPPORTED: false,
41
+ // !IS_WORKER && !!globalThis.Worker
42
+ NODE_STREAM_INPUT: 1,
43
+ // Configurable chunk sizes for local and remote files, respectively
44
+ LocalChunkSize: 1024 * 1024 * 10,
45
+ // 10 M,
46
+ RemoteChunkSize: 1024 * 1024 * 5,
47
+ // 5 M,
48
+ DefaultDelimiter: ","
49
+ // Used if not specified and detection fail,
50
+ };
51
+
52
+ // dist/papaparse/papa-parser.js
33
53
  function CsvToJson(_input, _config = {}, Streamer = StringStreamer) {
34
- _config = _config || {};
35
- var dynamicTyping = _config.dynamicTyping || false;
36
- if (isFunction(dynamicTyping)) {
37
- _config.dynamicTypingFunction = dynamicTyping;
38
- dynamicTyping = {};
39
- }
40
- _config.dynamicTyping = dynamicTyping;
41
- _config.transform = isFunction(_config.transform) ? _config.transform : false;
42
- var streamer = new Streamer(_config);
54
+ const streamer = new Streamer(_config);
43
55
  return streamer.stream(_input);
44
56
  }
45
- function JsonToCsv(_input, _config) {
46
- var _quotes = false;
47
- var _writeHeader = true;
48
- var _delimiter = ",";
49
- var _newline = "\r\n";
50
- var _quoteChar = '"';
51
- var _escapedQuote = _quoteChar + _quoteChar;
52
- var _skipEmptyLines = false;
53
- var _columns = null;
54
- unpackConfig();
55
- var quoteCharRegex = new RegExp(escapeRegExp(_quoteChar), "g");
56
- if (typeof _input === "string")
57
- _input = JSON.parse(_input);
58
- if (Array.isArray(_input)) {
59
- if (!_input.length || Array.isArray(_input[0]))
60
- return serialize(null, _input, _skipEmptyLines);
61
- else if (typeof _input[0] === "object")
62
- return serialize(_columns || Object.keys(_input[0]), _input, _skipEmptyLines);
63
- } else if (typeof _input === "object") {
64
- if (typeof _input.data === "string")
65
- _input.data = JSON.parse(_input.data);
66
- if (Array.isArray(_input.data)) {
67
- if (!_input.fields)
68
- _input.fields = _input.meta && _input.meta.fields;
69
- if (!_input.fields)
70
- _input.fields = Array.isArray(_input.data[0]) ? _input.fields : Object.keys(_input.data[0]);
71
- if (!Array.isArray(_input.data[0]) && typeof _input.data[0] !== "object")
72
- _input.data = [_input.data];
73
- }
74
- return serialize(_input.fields || [], _input.data || [], _skipEmptyLines);
75
- }
76
- throw new Error("Unable to serialize unrecognized input");
77
- function unpackConfig() {
78
- if (typeof _config !== "object")
79
- return;
80
- if (typeof _config.delimiter === "string" && !Papa.BAD_DELIMITERS.filter(function(value) {
81
- return _config.delimiter.indexOf(value) !== -1;
82
- }).length) {
83
- _delimiter = _config.delimiter;
84
- }
85
- if (typeof _config.quotes === "boolean" || Array.isArray(_config.quotes))
86
- _quotes = _config.quotes;
87
- if (typeof _config.skipEmptyLines === "boolean" || typeof _config.skipEmptyLines === "string")
88
- _skipEmptyLines = _config.skipEmptyLines;
89
- if (typeof _config.newline === "string")
90
- _newline = _config.newline;
91
- if (typeof _config.quoteChar === "string")
92
- _quoteChar = _config.quoteChar;
93
- if (typeof _config.header === "boolean")
94
- _writeHeader = _config.header;
95
- if (Array.isArray(_config.columns)) {
96
- if (_config.columns.length === 0)
97
- throw new Error("Option columns is empty");
98
- _columns = _config.columns;
99
- }
100
- if (_config.escapeChar !== void 0) {
101
- _escapedQuote = _config.escapeChar + _quoteChar;
102
- }
103
- }
104
- function serialize(fields, data, skipEmptyLines) {
105
- var csv = "";
106
- if (typeof fields === "string")
107
- fields = JSON.parse(fields);
108
- if (typeof data === "string")
109
- data = JSON.parse(data);
110
- var hasHeader = Array.isArray(fields) && fields.length > 0;
111
- var dataKeyedByField = !Array.isArray(data[0]);
112
- if (hasHeader && _writeHeader) {
113
- for (var i = 0; i < fields.length; i++) {
114
- if (i > 0)
115
- csv += _delimiter;
116
- csv += safe(fields[i], i);
117
- }
118
- if (data.length > 0)
119
- csv += _newline;
120
- }
121
- for (var row = 0; row < data.length; row++) {
122
- var maxCol = hasHeader ? fields.length : data[row].length;
123
- var emptyLine = false;
124
- var nullLine = hasHeader ? Object.keys(data[row]).length === 0 : data[row].length === 0;
125
- if (skipEmptyLines && !hasHeader) {
126
- emptyLine = skipEmptyLines === "greedy" ? data[row].join("").trim() === "" : data[row].length === 1 && data[row][0].length === 0;
127
- }
128
- if (skipEmptyLines === "greedy" && hasHeader) {
129
- var line = [];
130
- for (var c = 0; c < maxCol; c++) {
131
- var cx = dataKeyedByField ? fields[c] : c;
132
- line.push(data[row][cx]);
133
- }
134
- emptyLine = line.join("").trim() === "";
135
- }
136
- if (!emptyLine) {
137
- for (var col = 0; col < maxCol; col++) {
138
- if (col > 0 && !nullLine)
139
- csv += _delimiter;
140
- var colIdx = hasHeader && dataKeyedByField ? fields[col] : col;
141
- csv += safe(data[row][colIdx], col);
142
- }
143
- if (row < data.length - 1 && (!skipEmptyLines || maxCol > 0 && !nullLine)) {
144
- csv += _newline;
145
- }
146
- }
147
- }
148
- return csv;
149
- }
150
- function safe(str, col) {
151
- if (typeof str === "undefined" || str === null)
152
- return "";
153
- if (str.constructor === Date)
154
- return JSON.stringify(str).slice(1, 25);
155
- str = str.toString().replace(quoteCharRegex, _escapedQuote);
156
- 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) === " ";
157
- return needsQuotes ? _quoteChar + str + _quoteChar : str;
158
- }
159
- function hasAny(str, substrings) {
160
- for (var i = 0; i < substrings.length; i++)
161
- if (str.indexOf(substrings[i]) > -1)
162
- return true;
163
- return false;
164
- }
165
- }
166
57
  var ChunkStreamer = class {
167
58
  _handle;
168
59
  _config;
@@ -180,7 +71,10 @@ var ChunkStreamer = class {
180
71
  meta: {}
181
72
  };
182
73
  constructor(config) {
183
- var configCopy = { ...config };
74
+ const configCopy = { ...config };
75
+ if (configCopy.dynamicTypingFunction) {
76
+ configCopy.dynamicTyping = {};
77
+ }
184
78
  configCopy.chunkSize = parseInt(configCopy.chunkSize);
185
79
  if (!config.step && !config.chunk) {
186
80
  configCopy.chunkSize = null;
@@ -189,26 +83,27 @@ var ChunkStreamer = class {
189
83
  this._handle.streamer = this;
190
84
  this._config = configCopy;
191
85
  }
86
+ // eslint-disable-next-line complexity, max-statements
192
87
  parseChunk(chunk, isFakeChunk) {
193
88
  if (this.isFirstChunk && isFunction(this._config.beforeFirstChunk)) {
194
- var modifiedChunk = this._config.beforeFirstChunk(chunk);
89
+ const modifiedChunk = this._config.beforeFirstChunk(chunk);
195
90
  if (modifiedChunk !== void 0)
196
91
  chunk = modifiedChunk;
197
92
  }
198
93
  this.isFirstChunk = false;
199
- var aggregate = this._partialLine + chunk;
94
+ const aggregate = this._partialLine + chunk;
200
95
  this._partialLine = "";
201
- var results = this._handle.parse(aggregate, this._baseIndex, !this._finished);
96
+ let results = this._handle.parse(aggregate, this._baseIndex, !this._finished);
202
97
  if (this._handle.paused() || this._handle.aborted())
203
98
  return;
204
- var lastIndex = results.meta.cursor;
99
+ const lastIndex = results.meta.cursor;
205
100
  if (!this._finished) {
206
101
  this._partialLine = aggregate.substring(lastIndex - this._baseIndex);
207
102
  this._baseIndex = lastIndex;
208
103
  }
209
104
  if (results && results.data)
210
105
  this._rowCount += results.data.length;
211
- var finishedIncludingPreview = this._finished || this._config.preview && this._rowCount >= this._config.preview;
106
+ const finishedIncludingPreview = this._finished || this._config.preview && this._rowCount >= this._config.preview;
212
107
  if (isFunction(this._config.chunk) && !isFakeChunk) {
213
108
  this._config.chunk(results, this._handle);
214
109
  if (this._handle.paused() || this._handle.aborted())
@@ -244,8 +139,8 @@ var StringStreamer = class extends ChunkStreamer {
244
139
  _nextChunk() {
245
140
  if (this._finished)
246
141
  return;
247
- var size = this._config.chunkSize;
248
- var chunk = size ? this.remaining.substr(0, size) : this.remaining;
142
+ const size = this._config.chunkSize;
143
+ const chunk = size ? this.remaining.substr(0, size) : this.remaining;
249
144
  this.remaining = size ? this.remaining.substr(size) : "";
250
145
  this._finished = !this.remaining;
251
146
  return this.parseChunk(chunk);
@@ -279,7 +174,7 @@ var ParserHandle = class {
279
174
  };
280
175
  constructor(_config) {
281
176
  if (isFunction(_config.step)) {
282
- var userStep = _config.step;
177
+ const userStep = _config.step;
283
178
  _config.step = (results) => {
284
179
  this._results = results;
285
180
  if (this.needsHeaderRow()) {
@@ -305,12 +200,12 @@ var ParserHandle = class {
305
200
  * when an input comes in multiple chunks, like from a file.
306
201
  */
307
202
  parse(input, baseIndex, ignoreLastRow) {
308
- var quoteChar = this._config.quoteChar || '"';
203
+ const quoteChar = this._config.quoteChar || '"';
309
204
  if (!this._config.newline)
310
205
  this._config.newline = guessLineEndings(input, quoteChar);
311
206
  this._delimiterError = false;
312
207
  if (!this._config.delimiter) {
313
- var delimGuess = this.guessDelimiter(input, this._config.newline, this._config.skipEmptyLines, this._config.comments, this._config.delimitersToGuess);
208
+ const delimGuess = this.guessDelimiter(input, this._config.newline, this._config.skipEmptyLines, this._config.comments, this._config.delimitersToGuess);
314
209
  if (delimGuess.successful) {
315
210
  this._config.delimiter = delimGuess.bestDelimiter;
316
211
  } else {
@@ -322,7 +217,7 @@ var ParserHandle = class {
322
217
  this._config.delimiter = this._config.delimiter(input);
323
218
  this._results.meta.delimiter = this._config.delimiter;
324
219
  }
325
- var parserConfig = copy(this._config);
220
+ const parserConfig = copy(this._config);
326
221
  if (this._config.preview && this._config.header)
327
222
  parserConfig.preview++;
328
223
  this._input = input;
@@ -360,11 +255,11 @@ var ParserHandle = class {
360
255
  }
361
256
  processResults() {
362
257
  if (this._results && this._delimiterError) {
363
- this.addError("Delimiter", "UndetectableDelimiter", "Unable to auto-detect delimiting character; defaulted to '" + Papa.DefaultDelimiter + "'");
258
+ this.addError("Delimiter", "UndetectableDelimiter", `Unable to auto-detect delimiting character; defaulted to '${Papa.DefaultDelimiter}'`);
364
259
  this._delimiterError = false;
365
260
  }
366
261
  if (this._config.skipEmptyLines) {
367
- for (var i = 0; i < this._results.data.length; i++)
262
+ for (let i = 0; i < this._results.data.length; i++)
368
263
  if (this.testEmptyLine(this._results.data[i]))
369
264
  this._results.data.splice(i--, 1);
370
265
  }
@@ -385,7 +280,7 @@ var ParserHandle = class {
385
280
  this._fields.push(header);
386
281
  };
387
282
  if (Array.isArray(this._results.data[0])) {
388
- for (var i = 0; this.needsHeaderRow() && i < this._results.data.length; i++)
283
+ for (let i = 0; this.needsHeaderRow() && i < this._results.data.length; i++)
389
284
  this._results.data[i].forEach(addHeder);
390
285
  this._results.data.splice(0, 1);
391
286
  } else {
@@ -393,10 +288,11 @@ var ParserHandle = class {
393
288
  }
394
289
  }
395
290
  shouldApplyDynamicTyping(field) {
396
- if (this._config.dynamicTypingFunction && this._config.dynamicTyping[field] === void 0) {
291
+ var _a, _b;
292
+ if (this._config.dynamicTypingFunction && ((_a = this._config.dynamicTyping) == null ? void 0 : _a[field]) === void 0) {
397
293
  this._config.dynamicTyping[field] = this._config.dynamicTypingFunction(field);
398
294
  }
399
- return (this._config.dynamicTyping[field] || this._config.dynamicTyping) === true;
295
+ return (((_b = this._config.dynamicTyping) == null ? void 0 : _b[field]) || this._config.dynamicTyping) === true;
400
296
  }
401
297
  parseDynamic(field, value) {
402
298
  if (this.shouldApplyDynamicTyping(field)) {
@@ -408,8 +304,7 @@ var ParserHandle = class {
408
304
  return parseFloat(value);
409
305
  else if (ISO_DATE.test(value))
410
306
  return new Date(value);
411
- else
412
- return value === "" ? null : value;
307
+ return value === "" ? null : value;
413
308
  }
414
309
  return value;
415
310
  }
@@ -417,7 +312,7 @@ var ParserHandle = class {
417
312
  if (!this._results || !this._results.data || !this._config.header && !this._config.dynamicTyping && !this._config.transform) {
418
313
  return this._results;
419
314
  }
420
- var incrementBy = 1;
315
+ let incrementBy = 1;
421
316
  if (!this._results.data[0] || Array.isArray(this._results.data[0])) {
422
317
  this._results.data = this._results.data.map(this.processRow.bind(this));
423
318
  incrementBy = this._results.data.length;
@@ -430,11 +325,11 @@ var ParserHandle = class {
430
325
  return this._results;
431
326
  }
432
327
  processRow(rowSource, i) {
433
- var row = this._config.header ? {} : [];
434
- var j;
328
+ const row = this._config.header ? {} : [];
329
+ let j;
435
330
  for (j = 0; j < rowSource.length; j++) {
436
- var field = j;
437
- var value = rowSource[j];
331
+ let field = j;
332
+ let value = rowSource[j];
438
333
  if (this._config.header)
439
334
  field = j >= this._fields.length ? "__parsed_extra" : this._fields[j];
440
335
  if (this._config.transform)
@@ -448,31 +343,36 @@ var ParserHandle = class {
448
343
  }
449
344
  if (this._config.header) {
450
345
  if (j > this._fields.length)
451
- this.addError("FieldMismatch", "TooManyFields", "Too many fields: expected " + this._fields.length + " fields but parsed " + j, this._rowCounter + i);
346
+ this.addError("FieldMismatch", "TooManyFields", `Too many fields: expected ${this._fields.length} fields but parsed ${j}`, this._rowCounter + i);
452
347
  else if (j < this._fields.length)
453
- this.addError("FieldMismatch", "TooFewFields", "Too few fields: expected " + this._fields.length + " fields but parsed " + j, this._rowCounter + i);
348
+ this.addError("FieldMismatch", "TooFewFields", `Too few fields: expected ${this._fields.length} fields but parsed ${j}`, this._rowCounter + i);
454
349
  }
455
350
  return row;
456
351
  }
352
+ // eslint-disable-next-line complexity, max-statements
457
353
  guessDelimiter(input, newline, skipEmptyLines, comments, delimitersToGuess) {
458
- var bestDelim, bestDelta, fieldCountPrevRow;
354
+ let bestDelim;
355
+ let bestDelta;
356
+ let fieldCountPrevRow;
459
357
  delimitersToGuess = delimitersToGuess || [",", " ", "|", ";", Papa.RECORD_SEP, Papa.UNIT_SEP];
460
- for (var i = 0; i < delimitersToGuess.length; i++) {
461
- var delim = delimitersToGuess[i];
462
- var delta = 0, avgFieldCount = 0, emptyLinesCount = 0;
358
+ for (let i = 0; i < delimitersToGuess.length; i++) {
359
+ const delim = delimitersToGuess[i];
360
+ let avgFieldCount = 0;
361
+ let delta = 0;
362
+ let emptyLinesCount = 0;
463
363
  fieldCountPrevRow = void 0;
464
- var preview = new Parser({
364
+ const preview = new Parser({
465
365
  comments,
466
366
  delimiter: delim,
467
367
  newline,
468
368
  preview: 10
469
369
  }).parse(input);
470
- for (var j = 0; j < preview.data.length; j++) {
370
+ for (let j = 0; j < preview.data.length; j++) {
471
371
  if (skipEmptyLines && this.testEmptyLine(preview.data[j])) {
472
372
  emptyLinesCount++;
473
373
  continue;
474
374
  }
475
- var fieldCount = preview.data[j].length;
375
+ const fieldCount = preview.data[j].length;
476
376
  avgFieldCount += fieldCount;
477
377
  if (typeof fieldCountPrevRow === "undefined") {
478
378
  fieldCountPrevRow = 0;
@@ -491,7 +391,7 @@ var ParserHandle = class {
491
391
  }
492
392
  this._config.delimiter = bestDelim;
493
393
  return {
494
- successful: !!bestDelim,
394
+ successful: Boolean(bestDelim),
495
395
  bestDelimiter: bestDelim
496
396
  };
497
397
  }
@@ -506,15 +406,15 @@ var ParserHandle = class {
506
406
  };
507
407
  function guessLineEndings(input, quoteChar) {
508
408
  input = input.substr(0, 1024 * 1024);
509
- var re = new RegExp(escapeRegExp(quoteChar) + "([^]*?)" + escapeRegExp(quoteChar), "gm");
409
+ const re = new RegExp(`${escapeRegExp(quoteChar)}([^]*?)${escapeRegExp(quoteChar)}`, "gm");
510
410
  input = input.replace(re, "");
511
- var r = input.split("\r");
512
- var n = input.split("\n");
513
- var nAppearsFirst = n.length > 1 && n[0].length < r[0].length;
411
+ const r = input.split("\r");
412
+ const n = input.split("\n");
413
+ const nAppearsFirst = n.length > 1 && n[0].length < r[0].length;
514
414
  if (r.length === 1 || nAppearsFirst)
515
415
  return "\n";
516
- var numWithN = 0;
517
- for (var i = 0; i < r.length; i++) {
416
+ let numWithN = 0;
417
+ for (let i = 0; i < r.length; i++) {
518
418
  if (r[i][0] === "\n")
519
419
  numWithN++;
520
420
  }
@@ -523,48 +423,54 @@ function guessLineEndings(input, quoteChar) {
523
423
  function escapeRegExp(string) {
524
424
  return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
525
425
  }
526
- function Parser(config) {
527
- config = config || {};
528
- var delim = config.delimiter;
529
- var newline = config.newline;
530
- var comments = config.comments;
531
- var step = config.step;
532
- var preview = config.preview;
533
- var fastMode = config.fastMode;
534
- var quoteChar;
426
+ function Parser(config = {}) {
427
+ let delim = config.delimiter;
428
+ let newline = config.newline;
429
+ let comments = config.comments;
430
+ const step = config.step;
431
+ const preview = config.preview;
432
+ const fastMode = config.fastMode;
433
+ let quoteChar;
535
434
  if (config.quoteChar === void 0) {
536
435
  quoteChar = '"';
537
436
  } else {
538
437
  quoteChar = config.quoteChar;
539
438
  }
540
- var escapeChar = quoteChar;
439
+ let escapeChar = quoteChar;
541
440
  if (config.escapeChar !== void 0) {
542
441
  escapeChar = config.escapeChar;
543
442
  }
544
443
  if (typeof delim !== "string" || Papa.BAD_DELIMITERS.indexOf(delim) > -1)
545
444
  delim = ",";
546
- if (comments === delim)
445
+ if (comments === delim) {
547
446
  throw new Error("Comment character same as delimiter");
548
- else if (comments === true)
447
+ } else if (comments === true) {
549
448
  comments = "#";
550
- else if (typeof comments !== "string" || Papa.BAD_DELIMITERS.indexOf(comments) > -1)
449
+ } else if (typeof comments !== "string" || Papa.BAD_DELIMITERS.indexOf(comments) > -1) {
551
450
  comments = false;
451
+ }
552
452
  if (newline !== "\n" && newline !== "\r" && newline !== "\r\n")
553
453
  newline = "\n";
554
- var cursor = 0;
555
- var aborted = false;
454
+ let cursor = 0;
455
+ let aborted = false;
556
456
  this.parse = function(input, baseIndex, ignoreLastRow) {
557
457
  if (typeof input !== "string")
558
458
  throw new Error("Input must be a string");
559
- var inputLen = input.length, delimLen = delim.length, newlineLen = newline.length, commentsLen = comments.length;
560
- var stepIsFunction = isFunction(step);
459
+ const inputLen = input.length;
460
+ const delimLen = delim.length;
461
+ const newlineLen = newline.length;
462
+ const commentsLen = comments.length;
463
+ const stepIsFunction = isFunction(step);
561
464
  cursor = 0;
562
- var data = [], errors = [], row = [], lastCursor = 0;
465
+ let data = [];
466
+ let errors = [];
467
+ let row = [];
468
+ let lastCursor = 0;
563
469
  if (!input)
564
470
  return returnable();
565
471
  if (fastMode || fastMode !== false && input.indexOf(quoteChar) === -1) {
566
- var rows = input.split(newline);
567
- for (var i = 0; i < rows.length; i++) {
472
+ const rows = input.split(newline);
473
+ for (let i = 0; i < rows.length; i++) {
568
474
  const row2 = rows[i];
569
475
  cursor += row2.length;
570
476
  if (i !== rows.length - 1)
@@ -588,10 +494,10 @@ function Parser(config) {
588
494
  }
589
495
  return returnable();
590
496
  }
591
- var nextDelim = input.indexOf(delim, cursor);
592
- var nextNewline = input.indexOf(newline, cursor);
593
- var quoteCharRegex = new RegExp(escapeRegExp(escapeChar) + escapeRegExp(quoteChar), "g");
594
- var quoteSearch;
497
+ let nextDelim = input.indexOf(delim, cursor);
498
+ let nextNewline = input.indexOf(newline, cursor);
499
+ const quoteCharRegex = new RegExp(escapeRegExp(escapeChar) + escapeRegExp(quoteChar), "g");
500
+ let quoteSearch;
595
501
  for (; ; ) {
596
502
  if (input[cursor] === quoteChar) {
597
503
  quoteSearch = cursor;
@@ -612,7 +518,7 @@ function Parser(config) {
612
518
  return finish();
613
519
  }
614
520
  if (quoteSearch === inputLen - 1) {
615
- var value = input.substring(cursor, quoteSearch).replace(quoteCharRegex, quoteChar);
521
+ const value = input.substring(cursor, quoteSearch).replace(quoteCharRegex, quoteChar);
616
522
  return finish(value);
617
523
  }
618
524
  if (quoteChar === escapeChar && input[quoteSearch + 1] === escapeChar) {
@@ -622,8 +528,8 @@ function Parser(config) {
622
528
  if (quoteChar !== escapeChar && quoteSearch !== 0 && input[quoteSearch - 1] === escapeChar) {
623
529
  continue;
624
530
  }
625
- var checkUpTo = nextNewline === -1 ? nextDelim : Math.min(nextDelim, nextNewline);
626
- var spacesBetweenQuoteAndDelimiter = extraSpaces(checkUpTo);
531
+ const checkUpTo = nextNewline === -1 ? nextDelim : Math.min(nextDelim, nextNewline);
532
+ const spacesBetweenQuoteAndDelimiter = extraSpaces(checkUpTo);
627
533
  if (input[quoteSearch + 1 + spacesBetweenQuoteAndDelimiter] === delim) {
628
534
  row.push(input.substring(cursor, quoteSearch).replace(quoteCharRegex, quoteChar));
629
535
  cursor = quoteSearch + 1 + spacesBetweenQuoteAndDelimiter + delimLen;
@@ -638,7 +544,7 @@ function Parser(config) {
638
544
  return returnable(true);
639
545
  break;
640
546
  }
641
- var spacesBetweenQuoteAndNewLine = extraSpaces(nextNewline);
547
+ const spacesBetweenQuoteAndNewLine = extraSpaces(nextNewline);
642
548
  if (input.substr(quoteSearch + 1 + spacesBetweenQuoteAndNewLine, newlineLen) === newline) {
643
549
  row.push(input.substring(cursor, quoteSearch).replace(quoteCharRegex, quoteChar));
644
550
  saveRow(quoteSearch + 1 + spacesBetweenQuoteAndNewLine + newlineLen);
@@ -706,21 +612,21 @@ function Parser(config) {
706
612
  lastCursor = cursor;
707
613
  }
708
614
  function extraSpaces(index) {
709
- var spaceLength = 0;
615
+ let spaceLength = 0;
710
616
  if (index !== -1) {
711
- var textBetweenClosingQuoteAndIndex = input.substring(quoteSearch + 1, index);
617
+ const textBetweenClosingQuoteAndIndex = input.substring(quoteSearch + 1, index);
712
618
  if (textBetweenClosingQuoteAndIndex && textBetweenClosingQuoteAndIndex.trim() === "") {
713
619
  spaceLength = textBetweenClosingQuoteAndIndex.length;
714
620
  }
715
621
  }
716
622
  return spaceLength;
717
623
  }
718
- function finish(value2) {
624
+ function finish(value) {
719
625
  if (ignoreLastRow)
720
626
  return returnable();
721
- if (typeof value2 === "undefined")
722
- value2 = input.substr(cursor);
723
- row.push(value2);
627
+ if (typeof value === "undefined")
628
+ value = input.substr(cursor);
629
+ row.push(value);
724
630
  cursor = inputLen;
725
631
  pushRow(row);
726
632
  if (stepIsFunction)
@@ -734,7 +640,7 @@ function Parser(config) {
734
640
  nextNewline = input.indexOf(newline, cursor);
735
641
  }
736
642
  function returnable(stopped, step2) {
737
- var isStep = step2 || false;
643
+ const isStep = step2 || false;
738
644
  return {
739
645
  data: isStep ? data[0] : data,
740
646
  errors,
@@ -742,7 +648,7 @@ function Parser(config) {
742
648
  delimiter: delim,
743
649
  linebreak: newline,
744
650
  aborted,
745
- truncated: !!stopped,
651
+ truncated: Boolean(stopped),
746
652
  cursor: lastCursor + (baseIndex || 0)
747
653
  }
748
654
  };
@@ -763,38 +669,157 @@ function Parser(config) {
763
669
  function copy(obj) {
764
670
  if (typeof obj !== "object" || obj === null)
765
671
  return obj;
766
- var cpy = Array.isArray(obj) ? [] : {};
767
- for (var key in obj)
672
+ const cpy = Array.isArray(obj) ? [] : {};
673
+ for (const key in obj)
768
674
  cpy[key] = copy(obj[key]);
769
675
  return cpy;
770
676
  }
771
677
  function isFunction(func) {
772
678
  return typeof func === "function";
773
679
  }
774
- var Papa = {
680
+
681
+ // dist/papaparse/papa-writer.js
682
+ function JsonToCsv(_input, _config = {}) {
683
+ let _quotes = false;
684
+ let _writeHeader = true;
685
+ let _delimiter = ",";
686
+ let _newline = "\r\n";
687
+ let _quoteChar = '"';
688
+ let _escapedQuote = _quoteChar + _quoteChar;
689
+ let _skipEmptyLines = false;
690
+ let _columns = null;
691
+ unpackConfig();
692
+ const quoteCharRegex = new RegExp(escapeRegExp2(_quoteChar), "g");
693
+ if (typeof _input === "string")
694
+ _input = JSON.parse(_input);
695
+ if (Array.isArray(_input)) {
696
+ if (!_input.length || Array.isArray(_input[0])) {
697
+ return serialize(null, _input, _skipEmptyLines);
698
+ } else if (typeof _input[0] === "object") {
699
+ return serialize(_columns || Object.keys(_input[0]), _input, _skipEmptyLines);
700
+ }
701
+ } else if (typeof _input === "object") {
702
+ if (typeof _input.data === "string") {
703
+ _input.data = JSON.parse(_input.data);
704
+ }
705
+ if (Array.isArray(_input.data)) {
706
+ if (!_input.fields) {
707
+ _input.fields = _input.meta && _input.meta.fields;
708
+ }
709
+ if (!_input.fields) {
710
+ _input.fields = Array.isArray(_input.data[0]) ? _input.fields : Object.keys(_input.data[0]);
711
+ }
712
+ if (!Array.isArray(_input.data[0]) && typeof _input.data[0] !== "object") {
713
+ _input.data = [_input.data];
714
+ }
715
+ }
716
+ return serialize(_input.fields || [], _input.data || [], _skipEmptyLines);
717
+ }
718
+ throw new Error("Unable to serialize unrecognized input");
719
+ function unpackConfig() {
720
+ if (typeof _config !== "object")
721
+ return;
722
+ if (typeof _config.delimiter === "string" && !Papa.BAD_DELIMITERS.filter(function(value) {
723
+ var _a;
724
+ return ((_a = _config.delimiter) == null ? void 0 : _a.indexOf(value)) !== -1;
725
+ }).length) {
726
+ _delimiter = _config.delimiter;
727
+ }
728
+ if (typeof _config.quotes === "boolean" || Array.isArray(_config.quotes))
729
+ _quotes = _config.quotes;
730
+ if (typeof _config.skipEmptyLines === "boolean" || _config.skipEmptyLines === "greedy")
731
+ _skipEmptyLines = _config.skipEmptyLines;
732
+ if (typeof _config.newline === "string")
733
+ _newline = _config.newline;
734
+ if (typeof _config.quoteChar === "string")
735
+ _quoteChar = _config.quoteChar;
736
+ if (typeof _config.header === "boolean")
737
+ _writeHeader = _config.header;
738
+ if (Array.isArray(_config.columns)) {
739
+ if (_config.columns.length === 0)
740
+ throw new Error("Option columns is empty");
741
+ _columns = _config.columns;
742
+ }
743
+ if (_config.escapeChar !== void 0) {
744
+ _escapedQuote = _config.escapeChar + _quoteChar;
745
+ }
746
+ }
747
+ function serialize(fields, data, skipEmptyLines) {
748
+ let csv = "";
749
+ if (typeof fields === "string")
750
+ fields = JSON.parse(fields);
751
+ if (typeof data === "string")
752
+ data = JSON.parse(data);
753
+ const hasHeader = Array.isArray(fields) && fields.length > 0;
754
+ const dataKeyedByField = !Array.isArray(data[0]);
755
+ if (hasHeader && _writeHeader) {
756
+ for (let i = 0; i < fields.length; i++) {
757
+ if (i > 0)
758
+ csv += _delimiter;
759
+ csv += safe(fields[i], i);
760
+ }
761
+ if (data.length > 0)
762
+ csv += _newline;
763
+ }
764
+ for (let row = 0; row < data.length; row++) {
765
+ const maxCol = hasHeader ? fields.length : data[row].length;
766
+ let emptyLine = false;
767
+ const nullLine = hasHeader ? Object.keys(data[row]).length === 0 : data[row].length === 0;
768
+ if (skipEmptyLines && !hasHeader) {
769
+ emptyLine = skipEmptyLines === "greedy" ? data[row].join("").trim() === "" : data[row].length === 1 && data[row][0].length === 0;
770
+ }
771
+ if (skipEmptyLines === "greedy" && hasHeader) {
772
+ const line = [];
773
+ for (let c = 0; c < maxCol; c++) {
774
+ const cx = dataKeyedByField ? fields[c] : c;
775
+ line.push(data[row][cx]);
776
+ }
777
+ emptyLine = line.join("").trim() === "";
778
+ }
779
+ if (!emptyLine) {
780
+ for (let col = 0; col < maxCol; col++) {
781
+ if (col > 0 && !nullLine)
782
+ csv += _delimiter;
783
+ const colIdx = hasHeader && dataKeyedByField ? fields[col] : col;
784
+ csv += safe(data[row][colIdx], col);
785
+ }
786
+ if (row < data.length - 1 && (!skipEmptyLines || maxCol > 0 && !nullLine)) {
787
+ csv += _newline;
788
+ }
789
+ }
790
+ }
791
+ return csv;
792
+ }
793
+ function safe(str, col) {
794
+ if (typeof str === "undefined" || str === null)
795
+ return "";
796
+ if (str.constructor === Date)
797
+ return JSON.stringify(str).slice(1, 25);
798
+ str = str.toString().replace(quoteCharRegex, _escapedQuote);
799
+ const 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) === " ";
800
+ return needsQuotes ? _quoteChar + str + _quoteChar : str;
801
+ }
802
+ function hasAny(str, substrings) {
803
+ for (let i = 0; i < substrings.length; i++)
804
+ if (str.indexOf(substrings[i]) > -1)
805
+ return true;
806
+ return false;
807
+ }
808
+ }
809
+ function escapeRegExp2(string) {
810
+ return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
811
+ }
812
+
813
+ // dist/papaparse/papaparse.js
814
+ var papaparse_default = {
815
+ ...Papa,
775
816
  parse: CsvToJson,
776
817
  unparse: JsonToCsv,
777
- RECORD_SEP: String.fromCharCode(30),
778
- UNIT_SEP: String.fromCharCode(31),
779
- BYTE_ORDER_MARK,
780
- BAD_DELIMITERS: ["\r", "\n", '"', BYTE_ORDER_MARK],
781
- WORKERS_SUPPORTED: false,
782
- // !IS_WORKER && !!globalThis.Worker
783
- NODE_STREAM_INPUT: 1,
784
- // Configurable chunk sizes for local and remote files, respectively
785
- LocalChunkSize: 1024 * 1024 * 10,
786
- // 10 M,
787
- RemoteChunkSize: 1024 * 1024 * 5,
788
- // 5 M,
789
- DefaultDelimiter: ",",
790
- // Used if not specified and detection fail,
818
+ ChunkStreamer,
791
819
  // Exposed for testing and development only
792
820
  Parser,
793
- ParserHandle,
794
- // BEGIN FORK
795
- ChunkStreamer
821
+ ParserHandle
796
822
  };
797
- var papaparse_default = Papa;
798
823
 
799
824
  // dist/papaparse/async-iterator-streamer.js
800
825
  var { ChunkStreamer: ChunkStreamer2 } = papaparse_default;
@@ -831,19 +856,24 @@ var AsyncIteratorStreamer = class extends ChunkStreamer2 {
831
856
  }
832
857
  };
833
858
 
859
+ // dist/csv-format.js
860
+ var CSVFormat = {
861
+ id: "csv",
862
+ module: "csv",
863
+ name: "CSV",
864
+ extensions: ["csv", "tsv", "dsv"],
865
+ mimeTypes: ["text/csv", "text/tab-separated-values", "text/dsv"],
866
+ category: "table"
867
+ };
868
+
834
869
  // dist/csv-loader.js
835
- var VERSION = true ? "4.3.0" : "latest";
870
+ var VERSION = true ? "4.4.0-alpha.0" : "latest";
836
871
  var DEFAULT_CSV_SHAPE = "object-row-table";
837
872
  var CSVLoader = {
873
+ ...CSVFormat,
838
874
  dataType: null,
839
875
  batchType: null,
840
- id: "csv",
841
- module: "csv",
842
- name: "CSV",
843
876
  version: VERSION,
844
- extensions: ["csv", "tsv", "dsv"],
845
- mimeTypes: ["text/csv", "text/tab-separated-values", "text/dsv"],
846
- category: "table",
847
877
  parse: async (arrayBuffer, options) => parseCSV(new TextDecoder().decode(arrayBuffer), options),
848
878
  parseText: (text, options) => parseCSV(text, options),
849
879
  parseInBatches: parseCSVInBatches,
@@ -890,20 +920,25 @@ async function parseCSV(csvText, options) {
890
920
  const rows = result.data;
891
921
  const headerRow = result.meta.fields || generateHeader(csvOptions.columnPrefix, firstRow.length);
892
922
  const shape = csvOptions.shape || DEFAULT_CSV_SHAPE;
923
+ let table;
893
924
  switch (shape) {
894
925
  case "object-row-table":
895
- return {
926
+ table = {
896
927
  shape: "object-row-table",
897
- data: rows.map((row) => Array.isArray(row) ? (0, import_schema.convertToObjectRow)(row, headerRow) : row)
928
+ data: rows.map((row) => Array.isArray(row) ? (0, import_schema_utils.convertToObjectRow)(row, headerRow) : row)
898
929
  };
930
+ break;
899
931
  case "array-row-table":
900
- return {
932
+ table = {
901
933
  shape: "array-row-table",
902
- data: rows.map((row) => Array.isArray(row) ? row : (0, import_schema.convertToArrayRow)(row, headerRow))
934
+ data: rows.map((row) => Array.isArray(row) ? row : (0, import_schema_utils.convertToArrayRow)(row, headerRow))
903
935
  };
936
+ break;
904
937
  default:
905
938
  throw new Error(shape);
906
939
  }
940
+ table.schema = (0, import_schema_utils.deduceTableSchema)(table);
941
+ return table;
907
942
  }
908
943
  function parseCSVInBatches(asyncIterator, options) {
909
944
  options = { ...options };
@@ -911,7 +946,7 @@ function parseCSVInBatches(asyncIterator, options) {
911
946
  options.batchSize = 4e3;
912
947
  }
913
948
  const csvOptions = { ...CSVLoader.options.csv, ...options == null ? void 0 : options.csv };
914
- const asyncQueue = new import_schema.AsyncQueue();
949
+ const asyncQueue = new import_schema_utils.AsyncQueue();
915
950
  let isFirstRow = true;
916
951
  let headerRow = null;
917
952
  let tableBatchBuilder = null;
@@ -956,13 +991,13 @@ function parseCSVInBatches(asyncIterator, options) {
956
991
  if (!headerRow) {
957
992
  headerRow = generateHeader(csvOptions.columnPrefix, row.length);
958
993
  }
959
- schema = deduceSchema(row, headerRow);
994
+ schema = deduceCSVSchema(row, headerRow);
960
995
  }
961
996
  if (csvOptions.optimizeMemoryUsage) {
962
997
  row = JSON.parse(JSON.stringify(row));
963
998
  }
964
999
  const shape = csvOptions.shape || DEFAULT_CSV_SHAPE;
965
- tableBatchBuilder = tableBatchBuilder || new import_schema.TableBatchBuilder(
1000
+ tableBatchBuilder = tableBatchBuilder || new import_schema_utils.TableBatchBuilder(
966
1001
  // @ts-expect-error TODO this is not a proper schema
967
1002
  schema,
968
1003
  {
@@ -1027,26 +1062,37 @@ function generateHeader(columnPrefix, count = 0) {
1027
1062
  }
1028
1063
  return headers;
1029
1064
  }
1030
- function deduceSchema(row, headerRow) {
1031
- const schema = headerRow ? {} : [];
1065
+ function deduceCSVSchema(row, headerRow) {
1066
+ const fields = [];
1032
1067
  for (let i = 0; i < row.length; i++) {
1033
1068
  const columnName = headerRow && headerRow[i] || i;
1034
1069
  const value = row[i];
1035
1070
  switch (typeof value) {
1036
1071
  case "number":
1072
+ fields.push({ name: String(columnName), type: "float64", nullable: true });
1073
+ break;
1037
1074
  case "boolean":
1038
- schema[columnName] = { name: String(columnName), index: i, type: Float32Array };
1075
+ fields.push({ name: String(columnName), type: "bool", nullable: true });
1039
1076
  break;
1040
1077
  case "string":
1078
+ fields.push({ name: String(columnName), type: "utf8", nullable: true });
1079
+ break;
1041
1080
  default:
1042
- schema[columnName] = { name: String(columnName), index: i, type: Array };
1081
+ import_loader_utils.log.warn(`CSV: Unknown column type: ${typeof value}`)();
1082
+ fields.push({ name: String(columnName), type: "utf8", nullable: true });
1043
1083
  }
1044
1084
  }
1045
- return schema;
1085
+ return {
1086
+ fields,
1087
+ metadata: {
1088
+ "loaders.gl#format": "csv",
1089
+ "loaders.gl#loader": "CSVLoader"
1090
+ }
1091
+ };
1046
1092
  }
1047
1093
 
1048
1094
  // dist/lib/encoders/encode-csv.js
1049
- var import_schema2 = require("@loaders.gl/schema");
1095
+ var import_schema_utils2 = require("@loaders.gl/schema-utils");
1050
1096
  var import_d3_dsv = require("d3-dsv");
1051
1097
  function encodeTableAsCSV(table, options = { csv: { useDisplayNames: true } }) {
1052
1098
  var _a, _b;
@@ -1058,9 +1104,9 @@ function encodeTableAsCSV(table, options = { csv: { useDisplayNames: true } }) {
1058
1104
  return useDisplayNames && typeof displayName === "string" ? displayName : f.name;
1059
1105
  });
1060
1106
  const formattedData = [columnNames];
1061
- for (const row of (0, import_schema2.makeArrayRowIterator)(table)) {
1107
+ for (const row of (0, import_schema_utils2.makeArrayRowIterator)(table)) {
1062
1108
  const formattedRow = [];
1063
- for (let columnIndex = 0; columnIndex < (0, import_schema2.getTableNumCols)(table); ++columnIndex) {
1109
+ for (let columnIndex = 0; columnIndex < (0, import_schema_utils2.getTableNumCols)(table); ++columnIndex) {
1064
1110
  const value = row[columnIndex];
1065
1111
  formattedRow[columnIndex] = preformatFieldValue(value);
1066
1112
  }
@@ -1083,12 +1129,8 @@ var preformatFieldValue = (value) => {
1083
1129
 
1084
1130
  // dist/csv-writer.js
1085
1131
  var CSVWriter = {
1086
- id: "csv",
1132
+ ...CSVFormat,
1087
1133
  version: "latest",
1088
- module: "csv",
1089
- name: "CSV",
1090
- extensions: ["csv"],
1091
- mimeTypes: ["text/csv"],
1092
1134
  options: {
1093
1135
  csv: {
1094
1136
  useDisplayNames: false
@@ -1098,10 +1140,23 @@ var CSVWriter = {
1098
1140
  encode: async (table, options) => new TextEncoder().encode(encodeTableAsCSV(table, options)).buffer,
1099
1141
  encodeTextSync: (table, options) => encodeTableAsCSV(table, options)
1100
1142
  };
1101
- /* @license
1102
- Papa Parse
1103
- v5.0.0-beta.0
1104
- https://github.com/mholt/PapaParse
1105
- License: MIT
1106
- */
1143
+
1144
+ // dist/csv-arrow-loader.js
1145
+ var import_schema_utils3 = require("@loaders.gl/schema-utils");
1146
+ var CSVArrowLoader = {
1147
+ ...CSVLoader,
1148
+ dataType: null,
1149
+ batchType: null,
1150
+ parse: async (arrayBuffer, options) => parseCSVToArrow(new TextDecoder().decode(arrayBuffer), options),
1151
+ parseText: (text, options) => parseCSVToArrow(text, options),
1152
+ parseInBatches: parseCSVToArrowBatches
1153
+ };
1154
+ async function parseCSVToArrow(csvText, options) {
1155
+ const table = await CSVLoader.parseText(csvText, options);
1156
+ return (0, import_schema_utils3.convertTable)(table, "arrow-table");
1157
+ }
1158
+ function parseCSVToArrowBatches(asyncIterator, options) {
1159
+ const tableIterator = CSVLoader.parseInBatches(asyncIterator, options);
1160
+ return (0, import_schema_utils3.convertBatches)(tableIterator, "arrow-table");
1161
+ }
1107
1162
  //# sourceMappingURL=index.cjs.map