@loaders.gl/csv 4.0.0-beta.1 → 4.0.0-beta.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.
Files changed (60) hide show
  1. package/dist/csv-loader.d.ts.map +1 -0
  2. package/dist/{esm/csv-loader.js → csv-loader.js} +5 -5
  3. package/dist/csv-loader.js.map +1 -0
  4. package/dist/csv-writer.d.ts.map +1 -0
  5. package/dist/{esm/csv-writer.js → csv-writer.js} +1 -1
  6. package/dist/csv-writer.js.map +1 -0
  7. package/dist/{dist.min.js → dist.dev.js} +604 -602
  8. package/dist/index.cjs +1133 -0
  9. package/dist/index.d.ts.map +1 -0
  10. package/dist/index.js +3 -0
  11. package/dist/index.js.map +1 -0
  12. package/dist/lib/encoders/encode-csv.d.ts.map +1 -0
  13. package/dist/lib/encoders/encode-csv.js.map +1 -0
  14. package/dist/papaparse/async-iterator-streamer.d.ts.map +1 -0
  15. package/dist/{esm/papaparse → papaparse}/async-iterator-streamer.js +1 -1
  16. package/dist/papaparse/async-iterator-streamer.js.map +1 -0
  17. package/dist/papaparse/papaparse.d.ts.map +1 -0
  18. package/dist/papaparse/papaparse.js.map +1 -0
  19. package/package.json +16 -8
  20. package/dist/es5/bundle.js +0 -6
  21. package/dist/es5/bundle.js.map +0 -1
  22. package/dist/es5/csv-loader.js +0 -252
  23. package/dist/es5/csv-loader.js.map +0 -1
  24. package/dist/es5/csv-writer.js +0 -47
  25. package/dist/es5/csv-writer.js.map +0 -1
  26. package/dist/es5/index.js +0 -20
  27. package/dist/es5/index.js.map +0 -1
  28. package/dist/es5/lib/encoders/encode-csv.js +0 -60
  29. package/dist/es5/lib/encoders/encode-csv.js.map +0 -1
  30. package/dist/es5/papaparse/async-iterator-streamer.js +0 -100
  31. package/dist/es5/papaparse/async-iterator-streamer.js.map +0 -1
  32. package/dist/es5/papaparse/papaparse.js +0 -703
  33. package/dist/es5/papaparse/papaparse.js.map +0 -1
  34. package/dist/esm/bundle.js +0 -4
  35. package/dist/esm/bundle.js.map +0 -1
  36. package/dist/esm/csv-loader.js.map +0 -1
  37. package/dist/esm/csv-writer.js.map +0 -1
  38. package/dist/esm/index.js +0 -3
  39. package/dist/esm/index.js.map +0 -1
  40. package/dist/esm/lib/encoders/encode-csv.js.map +0 -1
  41. package/dist/esm/papaparse/async-iterator-streamer.js.map +0 -1
  42. package/dist/esm/papaparse/papaparse.js.map +0 -1
  43. package/dist/src/bundle.d.ts +0 -2
  44. package/dist/src/bundle.d.ts.map +0 -1
  45. package/dist/src/csv-loader.d.ts.map +0 -1
  46. package/dist/src/csv-writer.d.ts.map +0 -1
  47. package/dist/src/index.d.ts.map +0 -1
  48. package/dist/src/lib/encoders/encode-csv.d.ts.map +0 -1
  49. package/dist/src/papaparse/async-iterator-streamer.d.ts.map +0 -1
  50. package/dist/src/papaparse/papaparse.d.ts.map +0 -1
  51. package/dist/tsconfig.tsbuildinfo +0 -1
  52. package/src/bundle.ts +0 -4
  53. /package/dist/{src/csv-loader.d.ts → csv-loader.d.ts} +0 -0
  54. /package/dist/{src/csv-writer.d.ts → csv-writer.d.ts} +0 -0
  55. /package/dist/{src/index.d.ts → index.d.ts} +0 -0
  56. /package/dist/{src/lib → lib}/encoders/encode-csv.d.ts +0 -0
  57. /package/dist/{esm/lib → lib}/encoders/encode-csv.js +0 -0
  58. /package/dist/{src/papaparse → papaparse}/async-iterator-streamer.d.ts +0 -0
  59. /package/dist/{src/papaparse → papaparse}/papaparse.d.ts +0 -0
  60. /package/dist/{esm/papaparse → papaparse}/papaparse.js +0 -0
package/dist/index.cjs ADDED
@@ -0,0 +1,1133 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var src_exports = {};
22
+ __export(src_exports, {
23
+ CSVLoader: () => CSVLoader,
24
+ CSVWriter: () => CSVWriter
25
+ });
26
+ module.exports = __toCommonJS(src_exports);
27
+
28
+ // src/csv-loader.ts
29
+ var import_schema = require("@loaders.gl/schema");
30
+
31
+ // src/papaparse/papaparse.ts
32
+ var BYTE_ORDER_MARK = "\uFEFF";
33
+ var Papa = {
34
+ parse: CsvToJson,
35
+ unparse: JsonToCsv,
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
+ // Exposed for testing and development only
51
+ Parser,
52
+ ParserHandle,
53
+ // BEGIN FORK
54
+ ChunkStreamer,
55
+ StringStreamer
56
+ };
57
+ var papaparse_default = Papa;
58
+ function CsvToJson(_input, _config, UserDefinedStreamer) {
59
+ _config = _config || {};
60
+ var dynamicTyping = _config.dynamicTyping || false;
61
+ if (isFunction(dynamicTyping)) {
62
+ _config.dynamicTypingFunction = dynamicTyping;
63
+ dynamicTyping = {};
64
+ }
65
+ _config.dynamicTyping = dynamicTyping;
66
+ _config.transform = isFunction(_config.transform) ? _config.transform : false;
67
+ if (_config.worker && Papa.WORKERS_SUPPORTED) {
68
+ var w = newWorker();
69
+ w.userStep = _config.step;
70
+ w.userChunk = _config.chunk;
71
+ w.userComplete = _config.complete;
72
+ w.userError = _config.error;
73
+ _config.step = isFunction(_config.step);
74
+ _config.chunk = isFunction(_config.chunk);
75
+ _config.complete = isFunction(_config.complete);
76
+ _config.error = isFunction(_config.error);
77
+ delete _config.worker;
78
+ w.postMessage({
79
+ input: _input,
80
+ config: _config,
81
+ workerId: w.id
82
+ });
83
+ return;
84
+ }
85
+ var streamer = null;
86
+ if (typeof _input === "string") {
87
+ streamer = new StringStreamer(_config);
88
+ }
89
+ if (!streamer) {
90
+ streamer = new UserDefinedStreamer(_config);
91
+ }
92
+ return streamer.stream(_input);
93
+ }
94
+ function JsonToCsv(_input, _config) {
95
+ var _quotes = false;
96
+ var _writeHeader = true;
97
+ var _delimiter = ",";
98
+ var _newline = "\r\n";
99
+ var _quoteChar = '"';
100
+ var _escapedQuote = _quoteChar + _quoteChar;
101
+ var _skipEmptyLines = false;
102
+ var _columns = null;
103
+ unpackConfig();
104
+ var quoteCharRegex = new RegExp(escapeRegExp(_quoteChar), "g");
105
+ if (typeof _input === "string")
106
+ _input = JSON.parse(_input);
107
+ if (Array.isArray(_input)) {
108
+ if (!_input.length || Array.isArray(_input[0]))
109
+ return serialize(null, _input, _skipEmptyLines);
110
+ else if (typeof _input[0] === "object")
111
+ return serialize(_columns || objectKeys(_input[0]), _input, _skipEmptyLines);
112
+ } else if (typeof _input === "object") {
113
+ if (typeof _input.data === "string")
114
+ _input.data = JSON.parse(_input.data);
115
+ if (Array.isArray(_input.data)) {
116
+ if (!_input.fields)
117
+ _input.fields = _input.meta && _input.meta.fields;
118
+ if (!_input.fields)
119
+ _input.fields = Array.isArray(_input.data[0]) ? _input.fields : objectKeys(_input.data[0]);
120
+ if (!Array.isArray(_input.data[0]) && typeof _input.data[0] !== "object")
121
+ _input.data = [_input.data];
122
+ }
123
+ return serialize(_input.fields || [], _input.data || [], _skipEmptyLines);
124
+ }
125
+ throw new Error("Unable to serialize unrecognized input");
126
+ function unpackConfig() {
127
+ if (typeof _config !== "object")
128
+ return;
129
+ if (typeof _config.delimiter === "string" && !Papa.BAD_DELIMITERS.filter(function(value) {
130
+ return _config.delimiter.indexOf(value) !== -1;
131
+ }).length) {
132
+ _delimiter = _config.delimiter;
133
+ }
134
+ if (typeof _config.quotes === "boolean" || Array.isArray(_config.quotes))
135
+ _quotes = _config.quotes;
136
+ if (typeof _config.skipEmptyLines === "boolean" || typeof _config.skipEmptyLines === "string")
137
+ _skipEmptyLines = _config.skipEmptyLines;
138
+ if (typeof _config.newline === "string")
139
+ _newline = _config.newline;
140
+ if (typeof _config.quoteChar === "string")
141
+ _quoteChar = _config.quoteChar;
142
+ if (typeof _config.header === "boolean")
143
+ _writeHeader = _config.header;
144
+ if (Array.isArray(_config.columns)) {
145
+ if (_config.columns.length === 0)
146
+ throw new Error("Option columns is empty");
147
+ _columns = _config.columns;
148
+ }
149
+ if (_config.escapeChar !== void 0) {
150
+ _escapedQuote = _config.escapeChar + _quoteChar;
151
+ }
152
+ }
153
+ function objectKeys(obj) {
154
+ if (typeof obj !== "object")
155
+ return [];
156
+ var keys = [];
157
+ for (var key in obj)
158
+ keys.push(key);
159
+ return keys;
160
+ }
161
+ function serialize(fields, data, skipEmptyLines) {
162
+ var csv = "";
163
+ if (typeof fields === "string")
164
+ fields = JSON.parse(fields);
165
+ if (typeof data === "string")
166
+ data = JSON.parse(data);
167
+ var hasHeader = Array.isArray(fields) && fields.length > 0;
168
+ var dataKeyedByField = !Array.isArray(data[0]);
169
+ if (hasHeader && _writeHeader) {
170
+ for (var i = 0; i < fields.length; i++) {
171
+ if (i > 0)
172
+ csv += _delimiter;
173
+ csv += safe(fields[i], i);
174
+ }
175
+ if (data.length > 0)
176
+ csv += _newline;
177
+ }
178
+ for (var row = 0; row < data.length; row++) {
179
+ var maxCol = hasHeader ? fields.length : data[row].length;
180
+ var emptyLine = false;
181
+ var nullLine = hasHeader ? Object.keys(data[row]).length === 0 : data[row].length === 0;
182
+ if (skipEmptyLines && !hasHeader) {
183
+ emptyLine = skipEmptyLines === "greedy" ? data[row].join("").trim() === "" : data[row].length === 1 && data[row][0].length === 0;
184
+ }
185
+ if (skipEmptyLines === "greedy" && hasHeader) {
186
+ var line = [];
187
+ for (var c = 0; c < maxCol; c++) {
188
+ var cx = dataKeyedByField ? fields[c] : c;
189
+ line.push(data[row][cx]);
190
+ }
191
+ emptyLine = line.join("").trim() === "";
192
+ }
193
+ if (!emptyLine) {
194
+ for (var col = 0; col < maxCol; col++) {
195
+ if (col > 0 && !nullLine)
196
+ csv += _delimiter;
197
+ var colIdx = hasHeader && dataKeyedByField ? fields[col] : col;
198
+ csv += safe(data[row][colIdx], col);
199
+ }
200
+ if (row < data.length - 1 && (!skipEmptyLines || maxCol > 0 && !nullLine)) {
201
+ csv += _newline;
202
+ }
203
+ }
204
+ }
205
+ return csv;
206
+ }
207
+ function safe(str, col) {
208
+ if (typeof str === "undefined" || str === null)
209
+ return "";
210
+ if (str.constructor === Date)
211
+ return JSON.stringify(str).slice(1, 25);
212
+ str = str.toString().replace(quoteCharRegex, _escapedQuote);
213
+ 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) === " ";
214
+ return needsQuotes ? _quoteChar + str + _quoteChar : str;
215
+ }
216
+ function hasAny(str, substrings) {
217
+ for (var i = 0; i < substrings.length; i++)
218
+ if (str.indexOf(substrings[i]) > -1)
219
+ return true;
220
+ return false;
221
+ }
222
+ }
223
+ function ChunkStreamer(config) {
224
+ this._handle = null;
225
+ this._finished = false;
226
+ this._completed = false;
227
+ this._input = null;
228
+ this._baseIndex = 0;
229
+ this._partialLine = "";
230
+ this._rowCount = 0;
231
+ this._start = 0;
232
+ this._nextChunk = null;
233
+ this.isFirstChunk = true;
234
+ this._completeResults = {
235
+ data: [],
236
+ errors: [],
237
+ meta: {}
238
+ };
239
+ replaceConfig.call(this, config);
240
+ this.parseChunk = function(chunk, isFakeChunk) {
241
+ if (this.isFirstChunk && isFunction(this._config.beforeFirstChunk)) {
242
+ var modifiedChunk = this._config.beforeFirstChunk(chunk);
243
+ if (modifiedChunk !== void 0)
244
+ chunk = modifiedChunk;
245
+ }
246
+ this.isFirstChunk = false;
247
+ var aggregate = this._partialLine + chunk;
248
+ this._partialLine = "";
249
+ var results = this._handle.parse(aggregate, this._baseIndex, !this._finished);
250
+ if (this._handle.paused() || this._handle.aborted())
251
+ return;
252
+ var lastIndex = results.meta.cursor;
253
+ if (!this._finished) {
254
+ this._partialLine = aggregate.substring(lastIndex - this._baseIndex);
255
+ this._baseIndex = lastIndex;
256
+ }
257
+ if (results && results.data)
258
+ this._rowCount += results.data.length;
259
+ var finishedIncludingPreview = this._finished || this._config.preview && this._rowCount >= this._config.preview;
260
+ if (isFunction(this._config.chunk) && !isFakeChunk) {
261
+ this._config.chunk(results, this._handle);
262
+ if (this._handle.paused() || this._handle.aborted())
263
+ return;
264
+ results = void 0;
265
+ this._completeResults = void 0;
266
+ }
267
+ if (!this._config.step && !this._config.chunk) {
268
+ this._completeResults.data = this._completeResults.data.concat(results.data);
269
+ this._completeResults.errors = this._completeResults.errors.concat(results.errors);
270
+ this._completeResults.meta = results.meta;
271
+ }
272
+ if (!this._completed && finishedIncludingPreview && isFunction(this._config.complete) && (!results || !results.meta.aborted)) {
273
+ this._config.complete(this._completeResults, this._input);
274
+ this._completed = true;
275
+ }
276
+ if (!finishedIncludingPreview && (!results || !results.meta.paused))
277
+ this._nextChunk();
278
+ return results;
279
+ };
280
+ this._sendError = function(error) {
281
+ if (isFunction(this._config.error))
282
+ this._config.error(error);
283
+ };
284
+ function replaceConfig(config2) {
285
+ var configCopy = copy(config2);
286
+ configCopy.chunkSize = parseInt(configCopy.chunkSize);
287
+ if (!config2.step && !config2.chunk)
288
+ configCopy.chunkSize = null;
289
+ this._handle = new ParserHandle(configCopy);
290
+ this._handle.streamer = this;
291
+ this._config = configCopy;
292
+ }
293
+ }
294
+ function StringStreamer(config) {
295
+ config = config || {};
296
+ ChunkStreamer.call(this, config);
297
+ var remaining;
298
+ this.stream = function(s) {
299
+ remaining = s;
300
+ return this._nextChunk();
301
+ };
302
+ this._nextChunk = function() {
303
+ if (this._finished)
304
+ return;
305
+ var size = this._config.chunkSize;
306
+ var chunk = size ? remaining.substr(0, size) : remaining;
307
+ remaining = size ? remaining.substr(size) : "";
308
+ this._finished = !remaining;
309
+ return this.parseChunk(chunk);
310
+ };
311
+ }
312
+ StringStreamer.prototype = Object.create(StringStreamer.prototype);
313
+ StringStreamer.prototype.constructor = StringStreamer;
314
+ function ParserHandle(_config) {
315
+ var FLOAT = /^\s*-?(\d*\.?\d+|\d+\.?\d*)(e[-+]?\d+)?\s*$/i;
316
+ 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))/;
317
+ var self = this;
318
+ var _stepCounter = 0;
319
+ var _rowCounter = 0;
320
+ var _input;
321
+ var _parser;
322
+ var _paused = false;
323
+ var _aborted = false;
324
+ var _delimiterError;
325
+ var _fields = [];
326
+ var _results = {
327
+ // The last results returned from the parser
328
+ data: [],
329
+ errors: [],
330
+ meta: {}
331
+ };
332
+ if (isFunction(_config.step)) {
333
+ var userStep = _config.step;
334
+ _config.step = function(results) {
335
+ _results = results;
336
+ if (needsHeaderRow())
337
+ processResults();
338
+ else {
339
+ processResults();
340
+ if (!_results.data || _results.data.length === 0)
341
+ return;
342
+ _stepCounter += results.data.length;
343
+ if (_config.preview && _stepCounter > _config.preview)
344
+ _parser.abort();
345
+ else
346
+ userStep(_results, self);
347
+ }
348
+ };
349
+ }
350
+ this.parse = function(input, baseIndex, ignoreLastRow) {
351
+ var quoteChar = _config.quoteChar || '"';
352
+ if (!_config.newline)
353
+ _config.newline = guessLineEndings(input, quoteChar);
354
+ _delimiterError = false;
355
+ if (!_config.delimiter) {
356
+ var delimGuess = guessDelimiter(
357
+ input,
358
+ _config.newline,
359
+ _config.skipEmptyLines,
360
+ _config.comments,
361
+ _config.delimitersToGuess
362
+ );
363
+ if (delimGuess.successful)
364
+ _config.delimiter = delimGuess.bestDelimiter;
365
+ else {
366
+ _delimiterError = true;
367
+ _config.delimiter = Papa.DefaultDelimiter;
368
+ }
369
+ _results.meta.delimiter = _config.delimiter;
370
+ } else if (isFunction(_config.delimiter)) {
371
+ _config.delimiter = _config.delimiter(input);
372
+ _results.meta.delimiter = _config.delimiter;
373
+ }
374
+ var parserConfig = copy(_config);
375
+ if (_config.preview && _config.header)
376
+ parserConfig.preview++;
377
+ _input = input;
378
+ _parser = new Parser(parserConfig);
379
+ _results = _parser.parse(_input, baseIndex, ignoreLastRow);
380
+ processResults();
381
+ return _paused ? { meta: { paused: true } } : _results || { meta: { paused: false } };
382
+ };
383
+ this.paused = function() {
384
+ return _paused;
385
+ };
386
+ this.pause = function() {
387
+ _paused = true;
388
+ _parser.abort();
389
+ _input = _input.substr(_parser.getCharIndex());
390
+ };
391
+ this.resume = function() {
392
+ _paused = false;
393
+ self.streamer.parseChunk(_input, true);
394
+ };
395
+ this.aborted = function() {
396
+ return _aborted;
397
+ };
398
+ this.abort = function() {
399
+ _aborted = true;
400
+ _parser.abort();
401
+ _results.meta.aborted = true;
402
+ if (isFunction(_config.complete))
403
+ _config.complete(_results);
404
+ _input = "";
405
+ };
406
+ function testEmptyLine(s) {
407
+ return _config.skipEmptyLines === "greedy" ? s.join("").trim() === "" : s.length === 1 && s[0].length === 0;
408
+ }
409
+ function processResults() {
410
+ if (_results && _delimiterError) {
411
+ addError(
412
+ "Delimiter",
413
+ "UndetectableDelimiter",
414
+ "Unable to auto-detect delimiting character; defaulted to '" + Papa.DefaultDelimiter + "'"
415
+ );
416
+ _delimiterError = false;
417
+ }
418
+ if (_config.skipEmptyLines) {
419
+ for (var i = 0; i < _results.data.length; i++)
420
+ if (testEmptyLine(_results.data[i]))
421
+ _results.data.splice(i--, 1);
422
+ }
423
+ if (needsHeaderRow())
424
+ fillHeaderFields();
425
+ return applyHeaderAndDynamicTypingAndTransformation();
426
+ }
427
+ function needsHeaderRow() {
428
+ return _config.header && _fields.length === 0;
429
+ }
430
+ function fillHeaderFields() {
431
+ if (!_results)
432
+ return;
433
+ function addHeder(header) {
434
+ if (isFunction(_config.transformHeader))
435
+ header = _config.transformHeader(header);
436
+ _fields.push(header);
437
+ }
438
+ if (Array.isArray(_results.data[0])) {
439
+ for (var i = 0; needsHeaderRow() && i < _results.data.length; i++)
440
+ _results.data[i].forEach(addHeder);
441
+ _results.data.splice(0, 1);
442
+ } else
443
+ _results.data.forEach(addHeder);
444
+ }
445
+ function shouldApplyDynamicTyping(field) {
446
+ if (_config.dynamicTypingFunction && _config.dynamicTyping[field] === void 0) {
447
+ _config.dynamicTyping[field] = _config.dynamicTypingFunction(field);
448
+ }
449
+ return (_config.dynamicTyping[field] || _config.dynamicTyping) === true;
450
+ }
451
+ function parseDynamic(field, value) {
452
+ if (shouldApplyDynamicTyping(field)) {
453
+ if (value === "true" || value === "TRUE")
454
+ return true;
455
+ else if (value === "false" || value === "FALSE")
456
+ return false;
457
+ else if (FLOAT.test(value))
458
+ return parseFloat(value);
459
+ else if (ISO_DATE.test(value))
460
+ return new Date(value);
461
+ else
462
+ return value === "" ? null : value;
463
+ }
464
+ return value;
465
+ }
466
+ function applyHeaderAndDynamicTypingAndTransformation() {
467
+ if (!_results || !_results.data || !_config.header && !_config.dynamicTyping && !_config.transform)
468
+ return _results;
469
+ function processRow(rowSource, i) {
470
+ var row = _config.header ? {} : [];
471
+ var j;
472
+ for (j = 0; j < rowSource.length; j++) {
473
+ var field = j;
474
+ var value = rowSource[j];
475
+ if (_config.header)
476
+ field = j >= _fields.length ? "__parsed_extra" : _fields[j];
477
+ if (_config.transform)
478
+ value = _config.transform(value, field);
479
+ value = parseDynamic(field, value);
480
+ if (field === "__parsed_extra") {
481
+ row[field] = row[field] || [];
482
+ row[field].push(value);
483
+ } else
484
+ row[field] = value;
485
+ }
486
+ if (_config.header) {
487
+ if (j > _fields.length)
488
+ addError(
489
+ "FieldMismatch",
490
+ "TooManyFields",
491
+ "Too many fields: expected " + _fields.length + " fields but parsed " + j,
492
+ _rowCounter + i
493
+ );
494
+ else if (j < _fields.length)
495
+ addError(
496
+ "FieldMismatch",
497
+ "TooFewFields",
498
+ "Too few fields: expected " + _fields.length + " fields but parsed " + j,
499
+ _rowCounter + i
500
+ );
501
+ }
502
+ return row;
503
+ }
504
+ var incrementBy = 1;
505
+ if (!_results.data[0] || Array.isArray(_results.data[0])) {
506
+ _results.data = _results.data.map(processRow);
507
+ incrementBy = _results.data.length;
508
+ } else
509
+ _results.data = processRow(_results.data, 0);
510
+ if (_config.header && _results.meta)
511
+ _results.meta.fields = _fields;
512
+ _rowCounter += incrementBy;
513
+ return _results;
514
+ }
515
+ function guessDelimiter(input, newline, skipEmptyLines, comments, delimitersToGuess) {
516
+ var bestDelim, bestDelta, fieldCountPrevRow;
517
+ delimitersToGuess = delimitersToGuess || [",", " ", "|", ";", Papa.RECORD_SEP, Papa.UNIT_SEP];
518
+ for (var i = 0; i < delimitersToGuess.length; i++) {
519
+ var delim = delimitersToGuess[i];
520
+ var delta = 0, avgFieldCount = 0, emptyLinesCount = 0;
521
+ fieldCountPrevRow = void 0;
522
+ var preview = new Parser({
523
+ comments,
524
+ delimiter: delim,
525
+ newline,
526
+ preview: 10
527
+ }).parse(input);
528
+ for (var j = 0; j < preview.data.length; j++) {
529
+ if (skipEmptyLines && testEmptyLine(preview.data[j])) {
530
+ emptyLinesCount++;
531
+ continue;
532
+ }
533
+ var fieldCount = preview.data[j].length;
534
+ avgFieldCount += fieldCount;
535
+ if (typeof fieldCountPrevRow === "undefined") {
536
+ fieldCountPrevRow = 0;
537
+ continue;
538
+ } else if (fieldCount > 1) {
539
+ delta += Math.abs(fieldCount - fieldCountPrevRow);
540
+ fieldCountPrevRow = fieldCount;
541
+ }
542
+ }
543
+ if (preview.data.length > 0)
544
+ avgFieldCount /= preview.data.length - emptyLinesCount;
545
+ if ((typeof bestDelta === "undefined" || delta > bestDelta) && avgFieldCount > 1.99) {
546
+ bestDelta = delta;
547
+ bestDelim = delim;
548
+ }
549
+ }
550
+ _config.delimiter = bestDelim;
551
+ return {
552
+ successful: !!bestDelim,
553
+ bestDelimiter: bestDelim
554
+ };
555
+ }
556
+ function guessLineEndings(input, quoteChar) {
557
+ input = input.substr(0, 1024 * 1024);
558
+ var re = new RegExp(escapeRegExp(quoteChar) + "([^]*?)" + escapeRegExp(quoteChar), "gm");
559
+ input = input.replace(re, "");
560
+ var r = input.split("\r");
561
+ var n = input.split("\n");
562
+ var nAppearsFirst = n.length > 1 && n[0].length < r[0].length;
563
+ if (r.length === 1 || nAppearsFirst)
564
+ return "\n";
565
+ var numWithN = 0;
566
+ for (var i = 0; i < r.length; i++) {
567
+ if (r[i][0] === "\n")
568
+ numWithN++;
569
+ }
570
+ return numWithN >= r.length / 2 ? "\r\n" : "\r";
571
+ }
572
+ function addError(type, code, msg, row) {
573
+ _results.errors.push({
574
+ type,
575
+ code,
576
+ message: msg,
577
+ row
578
+ });
579
+ }
580
+ }
581
+ function escapeRegExp(string) {
582
+ return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
583
+ }
584
+ function Parser(config) {
585
+ config = config || {};
586
+ var delim = config.delimiter;
587
+ var newline = config.newline;
588
+ var comments = config.comments;
589
+ var step = config.step;
590
+ var preview = config.preview;
591
+ var fastMode = config.fastMode;
592
+ var quoteChar;
593
+ if (config.quoteChar === void 0) {
594
+ quoteChar = '"';
595
+ } else {
596
+ quoteChar = config.quoteChar;
597
+ }
598
+ var escapeChar = quoteChar;
599
+ if (config.escapeChar !== void 0) {
600
+ escapeChar = config.escapeChar;
601
+ }
602
+ if (typeof delim !== "string" || Papa.BAD_DELIMITERS.indexOf(delim) > -1)
603
+ delim = ",";
604
+ if (comments === delim)
605
+ throw new Error("Comment character same as delimiter");
606
+ else if (comments === true)
607
+ comments = "#";
608
+ else if (typeof comments !== "string" || Papa.BAD_DELIMITERS.indexOf(comments) > -1)
609
+ comments = false;
610
+ if (newline !== "\n" && newline !== "\r" && newline !== "\r\n")
611
+ newline = "\n";
612
+ var cursor = 0;
613
+ var aborted = false;
614
+ this.parse = function(input, baseIndex, ignoreLastRow) {
615
+ if (typeof input !== "string")
616
+ throw new Error("Input must be a string");
617
+ var inputLen = input.length, delimLen = delim.length, newlineLen = newline.length, commentsLen = comments.length;
618
+ var stepIsFunction = isFunction(step);
619
+ cursor = 0;
620
+ var data = [], errors = [], row = [], lastCursor = 0;
621
+ if (!input)
622
+ return returnable();
623
+ if (fastMode || fastMode !== false && input.indexOf(quoteChar) === -1) {
624
+ var rows = input.split(newline);
625
+ for (var i = 0; i < rows.length; i++) {
626
+ row = rows[i];
627
+ cursor += row.length;
628
+ if (i !== rows.length - 1)
629
+ cursor += newline.length;
630
+ else if (ignoreLastRow)
631
+ return returnable();
632
+ if (comments && row.substr(0, commentsLen) === comments)
633
+ continue;
634
+ if (stepIsFunction) {
635
+ data = [];
636
+ pushRow(row.split(delim));
637
+ doStep();
638
+ if (aborted)
639
+ return returnable();
640
+ } else
641
+ pushRow(row.split(delim));
642
+ if (preview && i >= preview) {
643
+ data = data.slice(0, preview);
644
+ return returnable(true);
645
+ }
646
+ }
647
+ return returnable();
648
+ }
649
+ var nextDelim = input.indexOf(delim, cursor);
650
+ var nextNewline = input.indexOf(newline, cursor);
651
+ var quoteCharRegex = new RegExp(escapeRegExp(escapeChar) + escapeRegExp(quoteChar), "g");
652
+ var quoteSearch;
653
+ for (; ; ) {
654
+ if (input[cursor] === quoteChar) {
655
+ quoteSearch = cursor;
656
+ cursor++;
657
+ for (; ; ) {
658
+ quoteSearch = input.indexOf(quoteChar, quoteSearch + 1);
659
+ if (quoteSearch === -1) {
660
+ if (!ignoreLastRow) {
661
+ errors.push({
662
+ type: "Quotes",
663
+ code: "MissingQuotes",
664
+ message: "Quoted field unterminated",
665
+ row: data.length,
666
+ // row has yet to be inserted
667
+ index: cursor
668
+ });
669
+ }
670
+ return finish();
671
+ }
672
+ if (quoteSearch === inputLen - 1) {
673
+ var value = input.substring(cursor, quoteSearch).replace(quoteCharRegex, quoteChar);
674
+ return finish(value);
675
+ }
676
+ if (quoteChar === escapeChar && input[quoteSearch + 1] === escapeChar) {
677
+ quoteSearch++;
678
+ continue;
679
+ }
680
+ if (quoteChar !== escapeChar && quoteSearch !== 0 && input[quoteSearch - 1] === escapeChar) {
681
+ continue;
682
+ }
683
+ var checkUpTo = nextNewline === -1 ? nextDelim : Math.min(nextDelim, nextNewline);
684
+ var spacesBetweenQuoteAndDelimiter = extraSpaces(checkUpTo);
685
+ if (input[quoteSearch + 1 + spacesBetweenQuoteAndDelimiter] === delim) {
686
+ row.push(input.substring(cursor, quoteSearch).replace(quoteCharRegex, quoteChar));
687
+ cursor = quoteSearch + 1 + spacesBetweenQuoteAndDelimiter + delimLen;
688
+ nextDelim = input.indexOf(delim, cursor);
689
+ nextNewline = input.indexOf(newline, cursor);
690
+ if (stepIsFunction) {
691
+ doStep();
692
+ if (aborted)
693
+ return returnable();
694
+ }
695
+ if (preview && data.length >= preview)
696
+ return returnable(true);
697
+ break;
698
+ }
699
+ var spacesBetweenQuoteAndNewLine = extraSpaces(nextNewline);
700
+ if (input.substr(quoteSearch + 1 + spacesBetweenQuoteAndNewLine, newlineLen) === newline) {
701
+ row.push(input.substring(cursor, quoteSearch).replace(quoteCharRegex, quoteChar));
702
+ saveRow(quoteSearch + 1 + spacesBetweenQuoteAndNewLine + newlineLen);
703
+ nextDelim = input.indexOf(delim, cursor);
704
+ if (stepIsFunction) {
705
+ doStep();
706
+ if (aborted)
707
+ return returnable();
708
+ }
709
+ if (preview && data.length >= preview)
710
+ return returnable(true);
711
+ break;
712
+ }
713
+ errors.push({
714
+ type: "Quotes",
715
+ code: "InvalidQuotes",
716
+ message: "Trailing quote on quoted field is malformed",
717
+ row: data.length,
718
+ // row has yet to be inserted
719
+ index: cursor
720
+ });
721
+ quoteSearch++;
722
+ continue;
723
+ }
724
+ if (stepIsFunction) {
725
+ doStep();
726
+ if (aborted)
727
+ return returnable();
728
+ }
729
+ if (preview && data.length >= preview)
730
+ return returnable(true);
731
+ continue;
732
+ }
733
+ if (comments && row.length === 0 && input.substr(cursor, commentsLen) === comments) {
734
+ if (nextNewline === -1)
735
+ return returnable();
736
+ cursor = nextNewline + newlineLen;
737
+ nextNewline = input.indexOf(newline, cursor);
738
+ nextDelim = input.indexOf(delim, cursor);
739
+ continue;
740
+ }
741
+ if (nextDelim !== -1 && (nextDelim < nextNewline || nextNewline === -1)) {
742
+ row.push(input.substring(cursor, nextDelim));
743
+ cursor = nextDelim + delimLen;
744
+ nextDelim = input.indexOf(delim, cursor);
745
+ continue;
746
+ }
747
+ if (nextNewline !== -1) {
748
+ row.push(input.substring(cursor, nextNewline));
749
+ saveRow(nextNewline + newlineLen);
750
+ if (stepIsFunction) {
751
+ doStep();
752
+ if (aborted)
753
+ return returnable();
754
+ }
755
+ if (preview && data.length >= preview)
756
+ return returnable(true);
757
+ continue;
758
+ }
759
+ break;
760
+ }
761
+ return finish();
762
+ function pushRow(row2) {
763
+ data.push(row2);
764
+ lastCursor = cursor;
765
+ }
766
+ function extraSpaces(index) {
767
+ var spaceLength = 0;
768
+ if (index !== -1) {
769
+ var textBetweenClosingQuoteAndIndex = input.substring(quoteSearch + 1, index);
770
+ if (textBetweenClosingQuoteAndIndex && textBetweenClosingQuoteAndIndex.trim() === "") {
771
+ spaceLength = textBetweenClosingQuoteAndIndex.length;
772
+ }
773
+ }
774
+ return spaceLength;
775
+ }
776
+ function finish(value2) {
777
+ if (ignoreLastRow)
778
+ return returnable();
779
+ if (typeof value2 === "undefined")
780
+ value2 = input.substr(cursor);
781
+ row.push(value2);
782
+ cursor = inputLen;
783
+ pushRow(row);
784
+ if (stepIsFunction)
785
+ doStep();
786
+ return returnable();
787
+ }
788
+ function saveRow(newCursor) {
789
+ cursor = newCursor;
790
+ pushRow(row);
791
+ row = [];
792
+ nextNewline = input.indexOf(newline, cursor);
793
+ }
794
+ function returnable(stopped, step2) {
795
+ var isStep = step2 || false;
796
+ return {
797
+ data: isStep ? data[0] : data,
798
+ errors,
799
+ meta: {
800
+ delimiter: delim,
801
+ linebreak: newline,
802
+ aborted,
803
+ truncated: !!stopped,
804
+ cursor: lastCursor + (baseIndex || 0)
805
+ }
806
+ };
807
+ }
808
+ function doStep() {
809
+ step(returnable(void 0, true));
810
+ data = [];
811
+ errors = [];
812
+ }
813
+ };
814
+ this.abort = function() {
815
+ aborted = true;
816
+ };
817
+ this.getCharIndex = function() {
818
+ return cursor;
819
+ };
820
+ }
821
+ function copy(obj) {
822
+ if (typeof obj !== "object" || obj === null)
823
+ return obj;
824
+ var cpy = Array.isArray(obj) ? [] : {};
825
+ for (var key in obj)
826
+ cpy[key] = copy(obj[key]);
827
+ return cpy;
828
+ }
829
+ function isFunction(func) {
830
+ return typeof func === "function";
831
+ }
832
+
833
+ // src/papaparse/async-iterator-streamer.ts
834
+ var { ChunkStreamer: ChunkStreamer2 } = papaparse_default;
835
+ function AsyncIteratorStreamer(config) {
836
+ config = config || {};
837
+ ChunkStreamer2.call(this, config);
838
+ this.textDecoder = new TextDecoder(this._config.encoding);
839
+ this.stream = async function(asyncIterator) {
840
+ this._input = asyncIterator;
841
+ try {
842
+ for await (const chunk of asyncIterator) {
843
+ this.parseChunk(this.getStringChunk(chunk));
844
+ }
845
+ this._finished = true;
846
+ this.parseChunk("");
847
+ } catch (error) {
848
+ this._sendError(error);
849
+ }
850
+ };
851
+ this._nextChunk = function nextChunk() {
852
+ };
853
+ this.getStringChunk = function(chunk) {
854
+ return typeof chunk === "string" ? chunk : this.textDecoder.decode(chunk, { stream: true });
855
+ };
856
+ }
857
+ AsyncIteratorStreamer.prototype = Object.create(ChunkStreamer2.prototype);
858
+ AsyncIteratorStreamer.prototype.constructor = AsyncIteratorStreamer;
859
+
860
+ // src/csv-loader.ts
861
+ var VERSION = typeof __VERSION__ !== "undefined" ? __VERSION__ : "latest";
862
+ var DEFAULT_CSV_SHAPE = "object-row-table";
863
+ var CSVLoader = {
864
+ id: "csv",
865
+ module: "csv",
866
+ name: "CSV",
867
+ version: VERSION,
868
+ extensions: ["csv", "tsv", "dsv"],
869
+ mimeTypes: ["text/csv", "text/tab-separated-values", "text/dsv"],
870
+ category: "table",
871
+ parse: async (arrayBuffer, options) => parseCSV(new TextDecoder().decode(arrayBuffer), options),
872
+ parseText: (text, options) => parseCSV(text, options),
873
+ parseInBatches: parseCSVInBatches,
874
+ // @ts-ignore
875
+ // testText: null,
876
+ options: {
877
+ csv: {
878
+ shape: DEFAULT_CSV_SHAPE,
879
+ // 'object-row-table'
880
+ optimizeMemoryUsage: false,
881
+ // CSV options
882
+ header: "auto",
883
+ columnPrefix: "column",
884
+ // delimiter: auto
885
+ // newline: auto
886
+ quoteChar: '"',
887
+ escapeChar: '"',
888
+ dynamicTyping: true,
889
+ comments: false,
890
+ skipEmptyLines: true,
891
+ // transform: null?
892
+ delimitersToGuess: [",", " ", "|", ";"]
893
+ // fastMode: auto
894
+ }
895
+ }
896
+ };
897
+ async function parseCSV(csvText, options) {
898
+ const csvOptions = { ...CSVLoader.options.csv, ...options == null ? void 0 : options.csv };
899
+ const firstRow = readFirstRow(csvText);
900
+ const header = csvOptions.header === "auto" ? isHeaderRow(firstRow) : Boolean(csvOptions.header);
901
+ const parseWithHeader = header;
902
+ const papaparseConfig = {
903
+ // dynamicTyping: true,
904
+ ...csvOptions,
905
+ header: parseWithHeader,
906
+ download: false,
907
+ // We handle loading, no need for papaparse to do it for us
908
+ transformHeader: parseWithHeader ? duplicateColumnTransformer() : void 0,
909
+ error: (e) => {
910
+ throw new Error(e);
911
+ }
912
+ };
913
+ const result = papaparse_default.parse(csvText, papaparseConfig);
914
+ const rows = result.data;
915
+ const headerRow = result.meta.fields || generateHeader(csvOptions.columnPrefix, firstRow.length);
916
+ const shape = csvOptions.shape || DEFAULT_CSV_SHAPE;
917
+ switch (shape) {
918
+ case "object-row-table":
919
+ return {
920
+ shape: "object-row-table",
921
+ data: rows.map((row) => Array.isArray(row) ? (0, import_schema.convertToObjectRow)(row, headerRow) : row)
922
+ };
923
+ case "array-row-table":
924
+ return {
925
+ shape: "array-row-table",
926
+ data: rows.map((row) => Array.isArray(row) ? row : (0, import_schema.convertToArrayRow)(row, headerRow))
927
+ };
928
+ default:
929
+ throw new Error(shape);
930
+ }
931
+ }
932
+ function parseCSVInBatches(asyncIterator, options) {
933
+ options = { ...options };
934
+ if (options.batchSize === "auto") {
935
+ options.batchSize = 4e3;
936
+ }
937
+ const csvOptions = { ...CSVLoader.options.csv, ...options == null ? void 0 : options.csv };
938
+ const asyncQueue = new import_schema.AsyncQueue();
939
+ let isFirstRow = true;
940
+ let headerRow = null;
941
+ let tableBatchBuilder = null;
942
+ let schema = null;
943
+ const config = {
944
+ // dynamicTyping: true, // Convert numbers and boolean values in rows from strings,
945
+ ...csvOptions,
946
+ header: false,
947
+ // Unfortunately, header detection is not automatic and does not infer shapes
948
+ download: false,
949
+ // We handle loading, no need for papaparse to do it for us
950
+ // chunkSize is set to 5MB explicitly (same as Papaparse default) due to a bug where the
951
+ // streaming parser gets stuck if skipEmptyLines and a step callback are both supplied.
952
+ // See https://github.com/mholt/PapaParse/issues/465
953
+ chunkSize: 1024 * 1024 * 5,
954
+ // skipEmptyLines is set to a boolean value if supplied. Greedy is set to true
955
+ // skipEmptyLines is handled manually given two bugs where the streaming parser gets stuck if
956
+ // both of the skipEmptyLines and step callback options are provided:
957
+ // - true doesn't work unless chunkSize is set: https://github.com/mholt/PapaParse/issues/465
958
+ // - greedy doesn't work: https://github.com/mholt/PapaParse/issues/825
959
+ skipEmptyLines: false,
960
+ // step is called on every row
961
+ // eslint-disable-next-line complexity, max-statements
962
+ step(results) {
963
+ let row = results.data;
964
+ if (csvOptions.skipEmptyLines) {
965
+ const collapsedRow = row.flat().join("").trim();
966
+ if (collapsedRow === "") {
967
+ return;
968
+ }
969
+ }
970
+ const bytesUsed = results.meta.cursor;
971
+ if (isFirstRow && !headerRow) {
972
+ const header = csvOptions.header === "auto" ? isHeaderRow(row) : Boolean(csvOptions.header);
973
+ if (header) {
974
+ headerRow = row.map(duplicateColumnTransformer());
975
+ return;
976
+ }
977
+ }
978
+ if (isFirstRow) {
979
+ isFirstRow = false;
980
+ if (!headerRow) {
981
+ headerRow = generateHeader(csvOptions.columnPrefix, row.length);
982
+ }
983
+ schema = deduceSchema(row, headerRow);
984
+ }
985
+ if (csvOptions.optimizeMemoryUsage) {
986
+ row = JSON.parse(JSON.stringify(row));
987
+ }
988
+ const shape = csvOptions.shape || DEFAULT_CSV_SHAPE;
989
+ tableBatchBuilder = tableBatchBuilder || new import_schema.TableBatchBuilder(
990
+ // @ts-expect-error TODO this is not a proper schema
991
+ schema,
992
+ {
993
+ shape,
994
+ ...options
995
+ }
996
+ );
997
+ try {
998
+ tableBatchBuilder.addRow(row);
999
+ const batch = tableBatchBuilder && tableBatchBuilder.getFullBatch({ bytesUsed });
1000
+ if (batch) {
1001
+ asyncQueue.enqueue(batch);
1002
+ }
1003
+ } catch (error) {
1004
+ asyncQueue.enqueue(error);
1005
+ }
1006
+ },
1007
+ // complete is called when all rows have been read
1008
+ complete(results) {
1009
+ try {
1010
+ const bytesUsed = results.meta.cursor;
1011
+ const batch = tableBatchBuilder && tableBatchBuilder.getFinalBatch({ bytesUsed });
1012
+ if (batch) {
1013
+ asyncQueue.enqueue(batch);
1014
+ }
1015
+ } catch (error) {
1016
+ asyncQueue.enqueue(error);
1017
+ }
1018
+ asyncQueue.close();
1019
+ }
1020
+ };
1021
+ papaparse_default.parse(asyncIterator, config, AsyncIteratorStreamer);
1022
+ return asyncQueue;
1023
+ }
1024
+ function isHeaderRow(row) {
1025
+ return row && row.every((value) => typeof value === "string");
1026
+ }
1027
+ function readFirstRow(csvText) {
1028
+ const result = papaparse_default.parse(csvText, {
1029
+ download: false,
1030
+ dynamicTyping: true,
1031
+ preview: 1
1032
+ });
1033
+ return result.data[0];
1034
+ }
1035
+ function duplicateColumnTransformer() {
1036
+ const observedColumns = /* @__PURE__ */ new Set();
1037
+ return (col) => {
1038
+ let colName = col;
1039
+ let counter = 1;
1040
+ while (observedColumns.has(colName)) {
1041
+ colName = `${col}.${counter}`;
1042
+ counter++;
1043
+ }
1044
+ observedColumns.add(colName);
1045
+ return colName;
1046
+ };
1047
+ }
1048
+ function generateHeader(columnPrefix, count = 0) {
1049
+ const headers = [];
1050
+ for (let i = 0; i < count; i++) {
1051
+ headers.push(`${columnPrefix}${i + 1}`);
1052
+ }
1053
+ return headers;
1054
+ }
1055
+ function deduceSchema(row, headerRow) {
1056
+ const schema = headerRow ? {} : [];
1057
+ for (let i = 0; i < row.length; i++) {
1058
+ const columnName = headerRow && headerRow[i] || i;
1059
+ const value = row[i];
1060
+ switch (typeof value) {
1061
+ case "number":
1062
+ case "boolean":
1063
+ schema[columnName] = { name: String(columnName), index: i, type: Float32Array };
1064
+ break;
1065
+ case "string":
1066
+ default:
1067
+ schema[columnName] = { name: String(columnName), index: i, type: Array };
1068
+ }
1069
+ }
1070
+ return schema;
1071
+ }
1072
+
1073
+ // src/lib/encoders/encode-csv.ts
1074
+ var import_schema2 = require("@loaders.gl/schema");
1075
+ var import_d3_dsv = require("d3-dsv");
1076
+ function encodeTableAsCSV(table, options = { csv: { useDisplayNames: true } }) {
1077
+ var _a, _b;
1078
+ const useDisplayNames = options.useDisplayNames || ((_a = options.csv) == null ? void 0 : _a.useDisplayNames);
1079
+ const fields = ((_b = table.schema) == null ? void 0 : _b.fields) || [];
1080
+ const columnNames = fields.map((f) => {
1081
+ var _a2;
1082
+ const displayName = (_a2 = f.metadata) == null ? void 0 : _a2.displayName;
1083
+ return useDisplayNames && typeof displayName === "string" ? displayName : f.name;
1084
+ });
1085
+ const formattedData = [columnNames];
1086
+ for (const row of (0, import_schema2.makeArrayRowIterator)(table)) {
1087
+ const formattedRow = [];
1088
+ for (let columnIndex = 0; columnIndex < (0, import_schema2.getTableNumCols)(table); ++columnIndex) {
1089
+ const value = row[columnIndex];
1090
+ formattedRow[columnIndex] = preformatFieldValue(value);
1091
+ }
1092
+ formattedData.push(formattedRow);
1093
+ }
1094
+ return (0, import_d3_dsv.csvFormatRows)(formattedData);
1095
+ }
1096
+ var preformatFieldValue = (value) => {
1097
+ if (value === null || value === void 0) {
1098
+ return null;
1099
+ }
1100
+ if (value instanceof Date) {
1101
+ return value.toISOString();
1102
+ }
1103
+ if (typeof value === "object") {
1104
+ return JSON.stringify(value);
1105
+ }
1106
+ return String(value);
1107
+ };
1108
+
1109
+ // src/csv-writer.ts
1110
+ var CSVWriter = {
1111
+ id: "csv",
1112
+ version: "latest",
1113
+ module: "csv",
1114
+ name: "CSV",
1115
+ extensions: ["csv"],
1116
+ mimeTypes: ["text/csv"],
1117
+ options: {
1118
+ csv: {
1119
+ useDisplayNames: false
1120
+ },
1121
+ /** @deprecated use csv.displayNames */
1122
+ useDisplayNames: false
1123
+ },
1124
+ text: true,
1125
+ encode: async (table, options) => new TextEncoder().encode(encodeTableAsCSV(table, options)).buffer,
1126
+ encodeTextSync: (table, options) => encodeTableAsCSV(table, options)
1127
+ };
1128
+ /* @license
1129
+ Papa Parse
1130
+ v5.0.0-beta.0
1131
+ https://github.com/mholt/PapaParse
1132
+ License: MIT
1133
+ */