@loaders.gl/csv 4.3.4 → 4.4.0-alpha.10
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.
- package/dist/csv-arrow-loader.d.ts +37 -0
- package/dist/csv-arrow-loader.d.ts.map +1 -0
- package/dist/csv-arrow-loader.js +24 -0
- package/dist/csv-arrow-loader.js.map +1 -0
- package/dist/csv-format.d.ts +10 -0
- package/dist/csv-format.d.ts.map +1 -0
- package/dist/csv-format.js +13 -0
- package/dist/csv-format.js.map +1 -0
- package/dist/csv-loader.d.ts +7 -7
- package/dist/csv-loader.d.ts.map +1 -1
- package/dist/csv-loader.js +59 -25
- package/dist/csv-loader.js.map +1 -0
- package/dist/csv-writer.d.ts +6 -5
- package/dist/csv-writer.d.ts.map +1 -1
- package/dist/csv-writer.js +3 -5
- package/dist/csv-writer.js.map +1 -0
- package/dist/dist.dev.js +13346 -454
- package/dist/dist.min.js +23 -20
- package/dist/index.cjs +323 -267
- package/dist/index.cjs.map +4 -4
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -0
- package/dist/lib/encoders/encode-csv.d.ts +1 -1
- package/dist/lib/encoders/encode-csv.d.ts.map +1 -1
- package/dist/lib/encoders/encode-csv.js +2 -1
- package/dist/lib/encoders/encode-csv.js.map +1 -0
- package/dist/papaparse/async-iterator-streamer.d.ts +1 -21
- package/dist/papaparse/async-iterator-streamer.d.ts.map +1 -1
- package/dist/papaparse/async-iterator-streamer.js +7 -6
- package/dist/papaparse/async-iterator-streamer.js.map +1 -0
- package/dist/papaparse/papa-constants.d.ts +12 -0
- package/dist/papaparse/papa-constants.d.ts.map +1 -0
- package/dist/papaparse/papa-constants.js +20 -0
- package/dist/papaparse/papa-constants.js.map +1 -0
- package/dist/papaparse/papa-parser.d.ts +110 -0
- package/dist/papaparse/papa-parser.d.ts.map +1 -0
- package/dist/papaparse/papa-parser.js +734 -0
- package/dist/papaparse/papa-parser.js.map +1 -0
- package/dist/papaparse/papa-writer.d.ts +22 -0
- package/dist/papaparse/papa-writer.d.ts.map +1 -0
- package/dist/papaparse/papa-writer.js +167 -0
- package/dist/papaparse/papa-writer.js.map +1 -0
- package/dist/papaparse/papaparse.d.ts +9 -113
- package/dist/papaparse/papaparse.d.ts.map +1 -1
- package/dist/papaparse/papaparse.js +14 -882
- package/dist/papaparse/papaparse.js.map +1 -0
- package/package.json +5 -5
- package/src/csv-arrow-loader.ts +43 -0
- package/src/csv-format.ts +15 -0
- package/src/csv-loader.ts +66 -31
- package/src/csv-writer.ts +2 -5
- package/src/index.ts +3 -0
- package/src/lib/encoders/encode-csv.ts +2 -1
- package/src/papaparse/async-iterator-streamer.ts +6 -6
- package/src/papaparse/papa-constants.ts +23 -0
- package/src/papaparse/papa-parser.ts +872 -0
- package/src/papaparse/papa-writer.ts +219 -0
- package/src/papaparse/papaparse.ts +17 -1048
|
@@ -1,893 +1,25 @@
|
|
|
1
|
-
//
|
|
1
|
+
// loaders.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// Copyright (c) vis.gl contributors
|
|
4
|
+
// Copyright (c) 2015 Matthew Holt
|
|
5
|
+
// This is a fork of papaparse v5.0.0-beta.0 under MIT license
|
|
2
6
|
// https://github.com/mholt/PapaParse
|
|
3
|
-
/* @license
|
|
4
|
-
Papa Parse
|
|
5
|
-
v5.0.0-beta.0
|
|
6
|
-
https://github.com/mholt/PapaParse
|
|
7
|
-
License: MIT
|
|
8
|
-
*/
|
|
9
7
|
// FORK SUMMARY:
|
|
10
8
|
// - Adopt ES6 exports
|
|
11
9
|
// - Implement new AsyncIteratorStreamer
|
|
12
10
|
// - Remove non Async Iterator streamers (can all be handled by new streamer)
|
|
13
11
|
// - Remove unused Worker support (loaders.gl worker system used instead)
|
|
14
12
|
// - Remove unused jQuery plugin support
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
/* eslint-disable */
|
|
21
|
-
const BYTE_ORDER_MARK = '\ufeff';
|
|
22
|
-
function CsvToJson(_input, _config = {}, Streamer = StringStreamer) {
|
|
23
|
-
_config = _config || {};
|
|
24
|
-
var dynamicTyping = _config.dynamicTyping || false;
|
|
25
|
-
if (isFunction(dynamicTyping)) {
|
|
26
|
-
_config.dynamicTypingFunction = dynamicTyping;
|
|
27
|
-
// Will be filled on first row call
|
|
28
|
-
dynamicTyping = {};
|
|
29
|
-
}
|
|
30
|
-
_config.dynamicTyping = dynamicTyping;
|
|
31
|
-
_config.transform = isFunction(_config.transform) ? _config.transform : false;
|
|
32
|
-
var streamer = new Streamer(_config);
|
|
33
|
-
return streamer.stream(_input);
|
|
34
|
-
}
|
|
35
|
-
function JsonToCsv(_input, _config) {
|
|
36
|
-
// Default configuration
|
|
37
|
-
/** whether to surround every datum with quotes */
|
|
38
|
-
var _quotes = false;
|
|
39
|
-
/** whether to write headers */
|
|
40
|
-
var _writeHeader = true;
|
|
41
|
-
/** delimiting character(s) */
|
|
42
|
-
var _delimiter = ',';
|
|
43
|
-
/** newline character(s) */
|
|
44
|
-
var _newline = '\r\n';
|
|
45
|
-
/** quote character */
|
|
46
|
-
var _quoteChar = '"';
|
|
47
|
-
/** escaped quote character, either "" or <config.escapeChar>" */
|
|
48
|
-
var _escapedQuote = _quoteChar + _quoteChar;
|
|
49
|
-
/** whether to skip empty lines */
|
|
50
|
-
var _skipEmptyLines = false;
|
|
51
|
-
/** the columns (keys) we expect when we unparse objects */
|
|
52
|
-
var _columns = null;
|
|
53
|
-
unpackConfig();
|
|
54
|
-
var quoteCharRegex = new RegExp(escapeRegExp(_quoteChar), 'g');
|
|
55
|
-
if (typeof _input === 'string')
|
|
56
|
-
_input = JSON.parse(_input);
|
|
57
|
-
if (Array.isArray(_input)) {
|
|
58
|
-
if (!_input.length || Array.isArray(_input[0]))
|
|
59
|
-
return serialize(null, _input, _skipEmptyLines);
|
|
60
|
-
else if (typeof _input[0] === 'object')
|
|
61
|
-
return serialize(_columns || Object.keys(_input[0]), _input, _skipEmptyLines);
|
|
62
|
-
}
|
|
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]; // handles input like [1,2,3] or ['asdf']
|
|
73
|
-
}
|
|
74
|
-
return serialize(_input.fields || [], _input.data || [], _skipEmptyLines);
|
|
75
|
-
}
|
|
76
|
-
// Default (any valid paths should return before this)
|
|
77
|
-
throw new Error('Unable to serialize unrecognized input');
|
|
78
|
-
function unpackConfig() {
|
|
79
|
-
if (typeof _config !== 'object')
|
|
80
|
-
return;
|
|
81
|
-
if (typeof _config.delimiter === 'string' &&
|
|
82
|
-
!Papa.BAD_DELIMITERS.filter(function (value) {
|
|
83
|
-
return _config.delimiter.indexOf(value) !== -1;
|
|
84
|
-
}).length) {
|
|
85
|
-
_delimiter = _config.delimiter;
|
|
86
|
-
}
|
|
87
|
-
if (typeof _config.quotes === 'boolean' || Array.isArray(_config.quotes))
|
|
88
|
-
_quotes = _config.quotes;
|
|
89
|
-
if (typeof _config.skipEmptyLines === 'boolean' || typeof _config.skipEmptyLines === 'string')
|
|
90
|
-
_skipEmptyLines = _config.skipEmptyLines;
|
|
91
|
-
if (typeof _config.newline === 'string')
|
|
92
|
-
_newline = _config.newline;
|
|
93
|
-
if (typeof _config.quoteChar === 'string')
|
|
94
|
-
_quoteChar = _config.quoteChar;
|
|
95
|
-
if (typeof _config.header === 'boolean')
|
|
96
|
-
_writeHeader = _config.header;
|
|
97
|
-
if (Array.isArray(_config.columns)) {
|
|
98
|
-
if (_config.columns.length === 0)
|
|
99
|
-
throw new Error('Option columns is empty');
|
|
100
|
-
_columns = _config.columns;
|
|
101
|
-
}
|
|
102
|
-
if (_config.escapeChar !== undefined) {
|
|
103
|
-
_escapedQuote = _config.escapeChar + _quoteChar;
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
/** The double for loop that iterates the data and writes out a CSV string including header row */
|
|
107
|
-
function serialize(fields, data, skipEmptyLines) {
|
|
108
|
-
var csv = '';
|
|
109
|
-
if (typeof fields === 'string')
|
|
110
|
-
fields = JSON.parse(fields);
|
|
111
|
-
if (typeof data === 'string')
|
|
112
|
-
data = JSON.parse(data);
|
|
113
|
-
var hasHeader = Array.isArray(fields) && fields.length > 0;
|
|
114
|
-
var dataKeyedByField = !Array.isArray(data[0]);
|
|
115
|
-
// If there a header row, write it first
|
|
116
|
-
if (hasHeader && _writeHeader) {
|
|
117
|
-
for (var i = 0; i < fields.length; i++) {
|
|
118
|
-
if (i > 0)
|
|
119
|
-
csv += _delimiter;
|
|
120
|
-
csv += safe(fields[i], i);
|
|
121
|
-
}
|
|
122
|
-
if (data.length > 0)
|
|
123
|
-
csv += _newline;
|
|
124
|
-
}
|
|
125
|
-
// Then write out the data
|
|
126
|
-
for (var row = 0; row < data.length; row++) {
|
|
127
|
-
var maxCol = hasHeader ? fields.length : data[row].length;
|
|
128
|
-
var emptyLine = false;
|
|
129
|
-
var nullLine = hasHeader ? Object.keys(data[row]).length === 0 : data[row].length === 0;
|
|
130
|
-
if (skipEmptyLines && !hasHeader) {
|
|
131
|
-
emptyLine =
|
|
132
|
-
skipEmptyLines === 'greedy'
|
|
133
|
-
? data[row].join('').trim() === ''
|
|
134
|
-
: data[row].length === 1 && data[row][0].length === 0;
|
|
135
|
-
}
|
|
136
|
-
if (skipEmptyLines === 'greedy' && hasHeader) {
|
|
137
|
-
var line = [];
|
|
138
|
-
for (var c = 0; c < maxCol; c++) {
|
|
139
|
-
var cx = dataKeyedByField ? fields[c] : c;
|
|
140
|
-
line.push(data[row][cx]);
|
|
141
|
-
}
|
|
142
|
-
emptyLine = line.join('').trim() === '';
|
|
143
|
-
}
|
|
144
|
-
if (!emptyLine) {
|
|
145
|
-
for (var col = 0; col < maxCol; col++) {
|
|
146
|
-
if (col > 0 && !nullLine)
|
|
147
|
-
csv += _delimiter;
|
|
148
|
-
var colIdx = hasHeader && dataKeyedByField ? fields[col] : col;
|
|
149
|
-
csv += safe(data[row][colIdx], col);
|
|
150
|
-
}
|
|
151
|
-
if (row < data.length - 1 && (!skipEmptyLines || (maxCol > 0 && !nullLine))) {
|
|
152
|
-
csv += _newline;
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
return csv;
|
|
157
|
-
}
|
|
158
|
-
/** Encloses a value around quotes if needed (makes a value safe for CSV insertion) */
|
|
159
|
-
function safe(str, col) {
|
|
160
|
-
if (typeof str === 'undefined' || str === null)
|
|
161
|
-
return '';
|
|
162
|
-
if (str.constructor === Date)
|
|
163
|
-
return JSON.stringify(str).slice(1, 25);
|
|
164
|
-
str = str.toString().replace(quoteCharRegex, _escapedQuote);
|
|
165
|
-
var needsQuotes = (typeof _quotes === 'boolean' && _quotes) ||
|
|
166
|
-
(Array.isArray(_quotes) && _quotes[col]) ||
|
|
167
|
-
hasAny(str, Papa.BAD_DELIMITERS) ||
|
|
168
|
-
str.indexOf(_delimiter) > -1 ||
|
|
169
|
-
str.charAt(0) === ' ' ||
|
|
170
|
-
str.charAt(str.length - 1) === ' ';
|
|
171
|
-
return needsQuotes ? _quoteChar + str + _quoteChar : str;
|
|
172
|
-
}
|
|
173
|
-
function hasAny(str, substrings) {
|
|
174
|
-
for (var i = 0; i < substrings.length; i++)
|
|
175
|
-
if (str.indexOf(substrings[i]) > -1)
|
|
176
|
-
return true;
|
|
177
|
-
return false;
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
/** ChunkStreamer is the base prototype for various streamer implementations. */
|
|
181
|
-
class ChunkStreamer {
|
|
182
|
-
_handle;
|
|
183
|
-
_config;
|
|
184
|
-
_finished = false;
|
|
185
|
-
_completed = false;
|
|
186
|
-
_input = null;
|
|
187
|
-
_baseIndex = 0;
|
|
188
|
-
_partialLine = '';
|
|
189
|
-
_rowCount = 0;
|
|
190
|
-
_start = 0;
|
|
191
|
-
isFirstChunk = true;
|
|
192
|
-
_completeResults = {
|
|
193
|
-
data: [],
|
|
194
|
-
errors: [],
|
|
195
|
-
meta: {}
|
|
196
|
-
};
|
|
197
|
-
constructor(config) {
|
|
198
|
-
// Deep-copy the config so we can edit it
|
|
199
|
-
var configCopy = { ...config };
|
|
200
|
-
// @ts-expect-error
|
|
201
|
-
configCopy.chunkSize = parseInt(configCopy.chunkSize); // parseInt VERY important so we don't concatenate strings!
|
|
202
|
-
if (!config.step && !config.chunk) {
|
|
203
|
-
configCopy.chunkSize = null; // disable Range header if not streaming; bad values break IIS - see issue #196
|
|
204
|
-
}
|
|
205
|
-
this._handle = new ParserHandle(configCopy);
|
|
206
|
-
this._handle.streamer = this;
|
|
207
|
-
this._config = configCopy; // persist the copy to the caller
|
|
208
|
-
}
|
|
209
|
-
parseChunk(chunk, isFakeChunk) {
|
|
210
|
-
// First chunk pre-processing
|
|
211
|
-
if (this.isFirstChunk && isFunction(this._config.beforeFirstChunk)) {
|
|
212
|
-
var modifiedChunk = this._config.beforeFirstChunk(chunk);
|
|
213
|
-
if (modifiedChunk !== undefined)
|
|
214
|
-
chunk = modifiedChunk;
|
|
215
|
-
}
|
|
216
|
-
this.isFirstChunk = false;
|
|
217
|
-
// Rejoin the line we likely just split in two by chunking the file
|
|
218
|
-
var aggregate = this._partialLine + chunk;
|
|
219
|
-
this._partialLine = '';
|
|
220
|
-
var results = this._handle.parse(aggregate, this._baseIndex, !this._finished);
|
|
221
|
-
if (this._handle.paused() || this._handle.aborted())
|
|
222
|
-
return;
|
|
223
|
-
var lastIndex = results.meta.cursor;
|
|
224
|
-
if (!this._finished) {
|
|
225
|
-
this._partialLine = aggregate.substring(lastIndex - this._baseIndex);
|
|
226
|
-
this._baseIndex = lastIndex;
|
|
227
|
-
}
|
|
228
|
-
if (results && results.data)
|
|
229
|
-
this._rowCount += results.data.length;
|
|
230
|
-
var finishedIncludingPreview = this._finished || (this._config.preview && this._rowCount >= this._config.preview);
|
|
231
|
-
if (isFunction(this._config.chunk) && !isFakeChunk) {
|
|
232
|
-
this._config.chunk(results, this._handle);
|
|
233
|
-
if (this._handle.paused() || this._handle.aborted())
|
|
234
|
-
return;
|
|
235
|
-
results = undefined;
|
|
236
|
-
// @ts-expect-error
|
|
237
|
-
this._completeResults = undefined;
|
|
238
|
-
}
|
|
239
|
-
if (!this._config.step && !this._config.chunk) {
|
|
240
|
-
this._completeResults.data = this._completeResults.data.concat(results.data);
|
|
241
|
-
this._completeResults.errors = this._completeResults.errors.concat(results.errors);
|
|
242
|
-
this._completeResults.meta = results.meta;
|
|
243
|
-
}
|
|
244
|
-
if (!this._completed &&
|
|
245
|
-
finishedIncludingPreview &&
|
|
246
|
-
isFunction(this._config.complete) &&
|
|
247
|
-
(!results || !results.meta.aborted)) {
|
|
248
|
-
this._config.complete(this._completeResults, this._input);
|
|
249
|
-
this._completed = true;
|
|
250
|
-
}
|
|
251
|
-
// if (!finishedIncludingPreview && (!results || !results.meta.paused)) this._nextChunk();
|
|
252
|
-
return results;
|
|
253
|
-
}
|
|
254
|
-
_sendError(error) {
|
|
255
|
-
if (isFunction(this._config.error))
|
|
256
|
-
this._config.error(error);
|
|
257
|
-
}
|
|
258
|
-
}
|
|
259
|
-
class StringStreamer extends ChunkStreamer {
|
|
260
|
-
remaining;
|
|
261
|
-
constructor(config = {}) {
|
|
262
|
-
super(config);
|
|
263
|
-
}
|
|
264
|
-
stream(s) {
|
|
265
|
-
this.remaining = s;
|
|
266
|
-
return this._nextChunk();
|
|
267
|
-
}
|
|
268
|
-
_nextChunk() {
|
|
269
|
-
if (this._finished)
|
|
270
|
-
return;
|
|
271
|
-
var size = this._config.chunkSize;
|
|
272
|
-
var chunk = size ? this.remaining.substr(0, size) : this.remaining;
|
|
273
|
-
this.remaining = size ? this.remaining.substr(size) : '';
|
|
274
|
-
this._finished = !this.remaining;
|
|
275
|
-
return this.parseChunk(chunk);
|
|
276
|
-
}
|
|
277
|
-
}
|
|
278
|
-
const FLOAT = /^\s*-?(\d*\.?\d+|\d+\.?\d*)(e[-+]?\d+)?\s*$/i;
|
|
279
|
-
const 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))/;
|
|
280
|
-
// Use one ParserHandle per entire CSV file or string
|
|
281
|
-
class ParserHandle {
|
|
282
|
-
_config;
|
|
283
|
-
/** Number of times step was called (number of rows parsed) */
|
|
284
|
-
_stepCounter = 0;
|
|
285
|
-
/** Number of rows that have been parsed so far */
|
|
286
|
-
_rowCounter = 0;
|
|
287
|
-
/** The input being parsed */
|
|
288
|
-
_input;
|
|
289
|
-
/** The core parser being used */
|
|
290
|
-
_parser;
|
|
291
|
-
/** Whether we are paused or not */
|
|
292
|
-
_paused = false;
|
|
293
|
-
/** Whether the parser has aborted or not */
|
|
294
|
-
_aborted = false;
|
|
295
|
-
/** Temporary state between delimiter detection and processing results */
|
|
296
|
-
_delimiterError = false;
|
|
297
|
-
/** Fields are from the header row of the input, if there is one */
|
|
298
|
-
_fields = [];
|
|
299
|
-
/** The last results returned from the parser */
|
|
300
|
-
_results = {
|
|
301
|
-
data: [],
|
|
302
|
-
errors: [],
|
|
303
|
-
meta: {}
|
|
304
|
-
};
|
|
305
|
-
constructor(_config) {
|
|
306
|
-
// One goal is to minimize the use of regular expressions...
|
|
307
|
-
if (isFunction(_config.step)) {
|
|
308
|
-
var userStep = _config.step;
|
|
309
|
-
_config.step = (results) => {
|
|
310
|
-
this._results = results;
|
|
311
|
-
if (this.needsHeaderRow()) {
|
|
312
|
-
this.processResults();
|
|
313
|
-
}
|
|
314
|
-
// only call user's step function after header row
|
|
315
|
-
else {
|
|
316
|
-
this.processResults();
|
|
317
|
-
// It's possbile that this line was empty and there's no row here after all
|
|
318
|
-
if (!this._results.data || this._results.data.length === 0)
|
|
319
|
-
return;
|
|
320
|
-
this._stepCounter += results.data.length;
|
|
321
|
-
if (_config.preview && this._stepCounter > _config.preview) {
|
|
322
|
-
this._parser.abort();
|
|
323
|
-
}
|
|
324
|
-
else {
|
|
325
|
-
userStep(this._results, this);
|
|
326
|
-
}
|
|
327
|
-
}
|
|
328
|
-
};
|
|
329
|
-
}
|
|
330
|
-
this._config = _config;
|
|
331
|
-
}
|
|
332
|
-
/**
|
|
333
|
-
* Parses input. Most users won't need, and shouldn't mess with, the baseIndex
|
|
334
|
-
* and ignoreLastRow parameters. They are used by streamers (wrapper functions)
|
|
335
|
-
* when an input comes in multiple chunks, like from a file.
|
|
336
|
-
*/
|
|
337
|
-
parse(input, baseIndex, ignoreLastRow) {
|
|
338
|
-
var quoteChar = this._config.quoteChar || '"';
|
|
339
|
-
if (!this._config.newline)
|
|
340
|
-
this._config.newline = guessLineEndings(input, quoteChar);
|
|
341
|
-
this._delimiterError = false;
|
|
342
|
-
if (!this._config.delimiter) {
|
|
343
|
-
var delimGuess = this.guessDelimiter(input, this._config.newline, this._config.skipEmptyLines, this._config.comments, this._config.delimitersToGuess);
|
|
344
|
-
if (delimGuess.successful) {
|
|
345
|
-
this._config.delimiter = delimGuess.bestDelimiter;
|
|
346
|
-
}
|
|
347
|
-
else {
|
|
348
|
-
this._delimiterError = true; // add error after parsing (otherwise it would be overwritten)
|
|
349
|
-
this._config.delimiter = Papa.DefaultDelimiter;
|
|
350
|
-
}
|
|
351
|
-
this._results.meta.delimiter = this._config.delimiter;
|
|
352
|
-
}
|
|
353
|
-
else if (isFunction(this._config.delimiter)) {
|
|
354
|
-
this._config.delimiter = this._config.delimiter(input);
|
|
355
|
-
this._results.meta.delimiter = this._config.delimiter;
|
|
356
|
-
}
|
|
357
|
-
var parserConfig = copy(this._config);
|
|
358
|
-
if (this._config.preview && this._config.header)
|
|
359
|
-
parserConfig.preview++; // to compensate for header row
|
|
360
|
-
this._input = input;
|
|
361
|
-
this._parser = new Parser(parserConfig);
|
|
362
|
-
this._results = this._parser.parse(this._input, baseIndex, ignoreLastRow);
|
|
363
|
-
this.processResults();
|
|
364
|
-
return this._paused ? { meta: { paused: true } } : this._results || { meta: { paused: false } };
|
|
365
|
-
}
|
|
366
|
-
paused() {
|
|
367
|
-
return this._paused;
|
|
368
|
-
}
|
|
369
|
-
pause() {
|
|
370
|
-
this._paused = true;
|
|
371
|
-
this._parser.abort();
|
|
372
|
-
this._input = this._input.substr(this._parser.getCharIndex());
|
|
373
|
-
}
|
|
374
|
-
resume() {
|
|
375
|
-
this._paused = false;
|
|
376
|
-
// @ts-expect-error
|
|
377
|
-
this.streamer.parseChunk(this._input, true);
|
|
378
|
-
}
|
|
379
|
-
aborted() {
|
|
380
|
-
return this._aborted;
|
|
381
|
-
}
|
|
382
|
-
abort() {
|
|
383
|
-
this._aborted = true;
|
|
384
|
-
this._parser.abort();
|
|
385
|
-
this._results.meta.aborted = true;
|
|
386
|
-
if (isFunction(this._config.complete)) {
|
|
387
|
-
this._config.complete(this._results);
|
|
388
|
-
}
|
|
389
|
-
this._input = '';
|
|
390
|
-
}
|
|
391
|
-
testEmptyLine(s) {
|
|
392
|
-
return this._config.skipEmptyLines === 'greedy'
|
|
393
|
-
? s.join('').trim() === ''
|
|
394
|
-
: s.length === 1 && s[0].length === 0;
|
|
395
|
-
}
|
|
396
|
-
processResults() {
|
|
397
|
-
if (this._results && this._delimiterError) {
|
|
398
|
-
this.addError('Delimiter', 'UndetectableDelimiter', "Unable to auto-detect delimiting character; defaulted to '" + Papa.DefaultDelimiter + "'");
|
|
399
|
-
this._delimiterError = false;
|
|
400
|
-
}
|
|
401
|
-
if (this._config.skipEmptyLines) {
|
|
402
|
-
for (var i = 0; i < this._results.data.length; i++)
|
|
403
|
-
if (this.testEmptyLine(this._results.data[i]))
|
|
404
|
-
this._results.data.splice(i--, 1);
|
|
405
|
-
}
|
|
406
|
-
if (this.needsHeaderRow()) {
|
|
407
|
-
this.fillHeaderFields();
|
|
408
|
-
}
|
|
409
|
-
return this.applyHeaderAndDynamicTypingAndTransformation();
|
|
410
|
-
}
|
|
411
|
-
needsHeaderRow() {
|
|
412
|
-
return this._config.header && this._fields.length === 0;
|
|
413
|
-
}
|
|
414
|
-
fillHeaderFields() {
|
|
415
|
-
if (!this._results)
|
|
416
|
-
return;
|
|
417
|
-
const addHeder = (header) => {
|
|
418
|
-
if (isFunction(this._config.transformHeader))
|
|
419
|
-
header = this._config.transformHeader(header);
|
|
420
|
-
this._fields.push(header);
|
|
421
|
-
};
|
|
422
|
-
if (Array.isArray(this._results.data[0])) {
|
|
423
|
-
for (var i = 0; this.needsHeaderRow() && i < this._results.data.length; i++)
|
|
424
|
-
this._results.data[i].forEach(addHeder);
|
|
425
|
-
this._results.data.splice(0, 1);
|
|
426
|
-
}
|
|
427
|
-
// if _results.data[0] is not an array, we are in a step where _results.data is the row.
|
|
428
|
-
else {
|
|
429
|
-
this._results.data.forEach(addHeder);
|
|
430
|
-
}
|
|
431
|
-
}
|
|
432
|
-
shouldApplyDynamicTyping(field) {
|
|
433
|
-
// Cache function values to avoid calling it for each row
|
|
434
|
-
if (this._config.dynamicTypingFunction && this._config.dynamicTyping[field] === undefined) {
|
|
435
|
-
this._config.dynamicTyping[field] = this._config.dynamicTypingFunction(field);
|
|
436
|
-
}
|
|
437
|
-
return (this._config.dynamicTyping[field] || this._config.dynamicTyping) === true;
|
|
438
|
-
}
|
|
439
|
-
parseDynamic(field, value) {
|
|
440
|
-
if (this.shouldApplyDynamicTyping(field)) {
|
|
441
|
-
if (value === 'true' || value === 'TRUE')
|
|
442
|
-
return true;
|
|
443
|
-
else if (value === 'false' || value === 'FALSE')
|
|
444
|
-
return false;
|
|
445
|
-
else if (FLOAT.test(value))
|
|
446
|
-
return parseFloat(value);
|
|
447
|
-
else if (ISO_DATE.test(value))
|
|
448
|
-
return new Date(value);
|
|
449
|
-
else
|
|
450
|
-
return value === '' ? null : value;
|
|
451
|
-
}
|
|
452
|
-
return value;
|
|
453
|
-
}
|
|
454
|
-
applyHeaderAndDynamicTypingAndTransformation() {
|
|
455
|
-
if (!this._results ||
|
|
456
|
-
!this._results.data ||
|
|
457
|
-
(!this._config.header && !this._config.dynamicTyping && !this._config.transform)) {
|
|
458
|
-
return this._results;
|
|
459
|
-
}
|
|
460
|
-
var incrementBy = 1;
|
|
461
|
-
if (!this._results.data[0] || Array.isArray(this._results.data[0])) {
|
|
462
|
-
this._results.data = this._results.data.map(this.processRow.bind(this));
|
|
463
|
-
incrementBy = this._results.data.length;
|
|
464
|
-
}
|
|
465
|
-
else {
|
|
466
|
-
// @ts-expect-error
|
|
467
|
-
this._results.data = this.processRow(this._results.data, 0);
|
|
468
|
-
}
|
|
469
|
-
if (this._config.header && this._results.meta)
|
|
470
|
-
this._results.meta.fields = this._fields;
|
|
471
|
-
this._rowCounter += incrementBy;
|
|
472
|
-
return this._results;
|
|
473
|
-
}
|
|
474
|
-
processRow(rowSource, i) {
|
|
475
|
-
var row = this._config.header ? {} : [];
|
|
476
|
-
var j;
|
|
477
|
-
for (j = 0; j < rowSource.length; j++) {
|
|
478
|
-
var field = j;
|
|
479
|
-
var value = rowSource[j];
|
|
480
|
-
if (this._config.header)
|
|
481
|
-
field = j >= this._fields.length ? '__parsed_extra' : this._fields[j];
|
|
482
|
-
if (this._config.transform)
|
|
483
|
-
value = this._config.transform(value, field);
|
|
484
|
-
value = this.parseDynamic(field, value);
|
|
485
|
-
if (field === '__parsed_extra') {
|
|
486
|
-
row[field] = row[field] || [];
|
|
487
|
-
row[field].push(value);
|
|
488
|
-
}
|
|
489
|
-
else
|
|
490
|
-
row[field] = value;
|
|
491
|
-
}
|
|
492
|
-
if (this._config.header) {
|
|
493
|
-
if (j > this._fields.length)
|
|
494
|
-
this.addError('FieldMismatch', 'TooManyFields', 'Too many fields: expected ' + this._fields.length + ' fields but parsed ' + j, this._rowCounter + i);
|
|
495
|
-
else if (j < this._fields.length)
|
|
496
|
-
this.addError('FieldMismatch', 'TooFewFields', 'Too few fields: expected ' + this._fields.length + ' fields but parsed ' + j, this._rowCounter + i);
|
|
497
|
-
}
|
|
498
|
-
return row;
|
|
499
|
-
}
|
|
500
|
-
guessDelimiter(input, newline, skipEmptyLines, comments, delimitersToGuess) {
|
|
501
|
-
var bestDelim, bestDelta, fieldCountPrevRow;
|
|
502
|
-
delimitersToGuess = delimitersToGuess || [',', '\t', '|', ';', Papa.RECORD_SEP, Papa.UNIT_SEP];
|
|
503
|
-
for (var i = 0; i < delimitersToGuess.length; i++) {
|
|
504
|
-
var delim = delimitersToGuess[i];
|
|
505
|
-
var delta = 0, avgFieldCount = 0, emptyLinesCount = 0;
|
|
506
|
-
fieldCountPrevRow = undefined;
|
|
507
|
-
var preview = new Parser({
|
|
508
|
-
comments: comments,
|
|
509
|
-
delimiter: delim,
|
|
510
|
-
newline: newline,
|
|
511
|
-
preview: 10
|
|
512
|
-
}).parse(input);
|
|
513
|
-
for (var j = 0; j < preview.data.length; j++) {
|
|
514
|
-
if (skipEmptyLines && this.testEmptyLine(preview.data[j])) {
|
|
515
|
-
emptyLinesCount++;
|
|
516
|
-
continue;
|
|
517
|
-
}
|
|
518
|
-
var fieldCount = preview.data[j].length;
|
|
519
|
-
avgFieldCount += fieldCount;
|
|
520
|
-
if (typeof fieldCountPrevRow === 'undefined') {
|
|
521
|
-
fieldCountPrevRow = 0;
|
|
522
|
-
continue;
|
|
523
|
-
}
|
|
524
|
-
else if (fieldCount > 1) {
|
|
525
|
-
delta += Math.abs(fieldCount - fieldCountPrevRow);
|
|
526
|
-
fieldCountPrevRow = fieldCount;
|
|
527
|
-
}
|
|
528
|
-
}
|
|
529
|
-
if (preview.data.length > 0)
|
|
530
|
-
avgFieldCount /= preview.data.length - emptyLinesCount;
|
|
531
|
-
if ((typeof bestDelta === 'undefined' || delta > bestDelta) && avgFieldCount > 1.99) {
|
|
532
|
-
bestDelta = delta;
|
|
533
|
-
bestDelim = delim;
|
|
534
|
-
}
|
|
535
|
-
}
|
|
536
|
-
this._config.delimiter = bestDelim;
|
|
537
|
-
return {
|
|
538
|
-
successful: !!bestDelim,
|
|
539
|
-
bestDelimiter: bestDelim
|
|
540
|
-
};
|
|
541
|
-
}
|
|
542
|
-
addError(type, code, msg, row) {
|
|
543
|
-
this._results.errors.push({
|
|
544
|
-
type: type,
|
|
545
|
-
code: code,
|
|
546
|
-
message: msg,
|
|
547
|
-
row: row
|
|
548
|
-
});
|
|
549
|
-
}
|
|
550
|
-
}
|
|
551
|
-
function guessLineEndings(input, quoteChar) {
|
|
552
|
-
input = input.substr(0, 1024 * 1024); // max length 1 MB
|
|
553
|
-
// Replace all the text inside quotes
|
|
554
|
-
var re = new RegExp(escapeRegExp(quoteChar) + '([^]*?)' + escapeRegExp(quoteChar), 'gm');
|
|
555
|
-
input = input.replace(re, '');
|
|
556
|
-
var r = input.split('\r');
|
|
557
|
-
var n = input.split('\n');
|
|
558
|
-
var nAppearsFirst = n.length > 1 && n[0].length < r[0].length;
|
|
559
|
-
if (r.length === 1 || nAppearsFirst)
|
|
560
|
-
return '\n';
|
|
561
|
-
var numWithN = 0;
|
|
562
|
-
for (var i = 0; i < r.length; i++) {
|
|
563
|
-
if (r[i][0] === '\n')
|
|
564
|
-
numWithN++;
|
|
565
|
-
}
|
|
566
|
-
return numWithN >= r.length / 2 ? '\r\n' : '\r';
|
|
567
|
-
}
|
|
568
|
-
/** https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions */
|
|
569
|
-
function escapeRegExp(string) {
|
|
570
|
-
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
|
|
571
|
-
}
|
|
572
|
-
/** The core parser implements speedy and correct CSV parsing */
|
|
573
|
-
function Parser(config) {
|
|
574
|
-
// Unpack the config object
|
|
575
|
-
config = config || {};
|
|
576
|
-
var delim = config.delimiter;
|
|
577
|
-
var newline = config.newline;
|
|
578
|
-
var comments = config.comments;
|
|
579
|
-
var step = config.step;
|
|
580
|
-
var preview = config.preview;
|
|
581
|
-
var fastMode = config.fastMode;
|
|
582
|
-
var quoteChar;
|
|
583
|
-
/** Allows for no quoteChar by setting quoteChar to undefined in config */
|
|
584
|
-
if (config.quoteChar === undefined) {
|
|
585
|
-
quoteChar = '"';
|
|
586
|
-
}
|
|
587
|
-
else {
|
|
588
|
-
quoteChar = config.quoteChar;
|
|
589
|
-
}
|
|
590
|
-
var escapeChar = quoteChar;
|
|
591
|
-
if (config.escapeChar !== undefined) {
|
|
592
|
-
escapeChar = config.escapeChar;
|
|
593
|
-
}
|
|
594
|
-
// Delimiter must be valid
|
|
595
|
-
if (typeof delim !== 'string' || Papa.BAD_DELIMITERS.indexOf(delim) > -1)
|
|
596
|
-
delim = ',';
|
|
597
|
-
// Comment character must be valid
|
|
598
|
-
if (comments === delim)
|
|
599
|
-
throw new Error('Comment character same as delimiter');
|
|
600
|
-
else if (comments === true)
|
|
601
|
-
comments = '#';
|
|
602
|
-
else if (typeof comments !== 'string' || Papa.BAD_DELIMITERS.indexOf(comments) > -1)
|
|
603
|
-
comments = false;
|
|
604
|
-
// Newline must be valid: \r, \n, or \r\n
|
|
605
|
-
if (newline !== '\n' && newline !== '\r' && newline !== '\r\n')
|
|
606
|
-
newline = '\n';
|
|
607
|
-
// We're gonna need these at the Parser scope
|
|
608
|
-
var cursor = 0;
|
|
609
|
-
var aborted = false;
|
|
610
|
-
// @ts-expect-error
|
|
611
|
-
this.parse = function (input, baseIndex, ignoreLastRow) {
|
|
612
|
-
// For some reason, in Chrome, this speeds things up (!?)
|
|
613
|
-
if (typeof input !== 'string')
|
|
614
|
-
throw new Error('Input must be a string');
|
|
615
|
-
// We don't need to compute some of these every time parse() is called,
|
|
616
|
-
// but having them in a more local scope seems to perform better
|
|
617
|
-
var inputLen = input.length, delimLen = delim.length, newlineLen = newline.length, commentsLen = comments.length;
|
|
618
|
-
var stepIsFunction = isFunction(step);
|
|
619
|
-
// Establish starting state
|
|
620
|
-
cursor = 0;
|
|
621
|
-
var data = [], errors = [], row = [], lastCursor = 0;
|
|
622
|
-
if (!input)
|
|
623
|
-
return returnable();
|
|
624
|
-
if (fastMode || (fastMode !== false && input.indexOf(quoteChar) === -1)) {
|
|
625
|
-
var rows = input.split(newline);
|
|
626
|
-
for (var i = 0; i < rows.length; i++) {
|
|
627
|
-
const row = rows[i];
|
|
628
|
-
cursor += row.length;
|
|
629
|
-
if (i !== rows.length - 1)
|
|
630
|
-
cursor += newline.length;
|
|
631
|
-
else if (ignoreLastRow)
|
|
632
|
-
return returnable();
|
|
633
|
-
if (comments && row.substr(0, commentsLen) === comments)
|
|
634
|
-
continue;
|
|
635
|
-
if (stepIsFunction) {
|
|
636
|
-
data = [];
|
|
637
|
-
pushRow(row.split(delim));
|
|
638
|
-
doStep();
|
|
639
|
-
if (aborted)
|
|
640
|
-
return returnable();
|
|
641
|
-
}
|
|
642
|
-
else
|
|
643
|
-
pushRow(row.split(delim));
|
|
644
|
-
if (preview && i >= preview) {
|
|
645
|
-
data = data.slice(0, preview);
|
|
646
|
-
return returnable(true);
|
|
647
|
-
}
|
|
648
|
-
}
|
|
649
|
-
return returnable();
|
|
650
|
-
}
|
|
651
|
-
var nextDelim = input.indexOf(delim, cursor);
|
|
652
|
-
var nextNewline = input.indexOf(newline, cursor);
|
|
653
|
-
var quoteCharRegex = new RegExp(escapeRegExp(escapeChar) + escapeRegExp(quoteChar), 'g');
|
|
654
|
-
var quoteSearch;
|
|
655
|
-
// Parser loop
|
|
656
|
-
for (;;) {
|
|
657
|
-
// Field has opening quote
|
|
658
|
-
if (input[cursor] === quoteChar) {
|
|
659
|
-
// Start our search for the closing quote where the cursor is
|
|
660
|
-
quoteSearch = cursor;
|
|
661
|
-
// Skip the opening quote
|
|
662
|
-
cursor++;
|
|
663
|
-
for (;;) {
|
|
664
|
-
// Find closing quote
|
|
665
|
-
quoteSearch = input.indexOf(quoteChar, quoteSearch + 1);
|
|
666
|
-
//No other quotes are found - no other delimiters
|
|
667
|
-
if (quoteSearch === -1) {
|
|
668
|
-
if (!ignoreLastRow) {
|
|
669
|
-
// No closing quote... what a pity
|
|
670
|
-
errors.push({
|
|
671
|
-
type: 'Quotes',
|
|
672
|
-
code: 'MissingQuotes',
|
|
673
|
-
message: 'Quoted field unterminated',
|
|
674
|
-
row: data.length, // row has yet to be inserted
|
|
675
|
-
index: cursor
|
|
676
|
-
});
|
|
677
|
-
}
|
|
678
|
-
return finish();
|
|
679
|
-
}
|
|
680
|
-
// Closing quote at EOF
|
|
681
|
-
if (quoteSearch === inputLen - 1) {
|
|
682
|
-
var value = input.substring(cursor, quoteSearch).replace(quoteCharRegex, quoteChar);
|
|
683
|
-
return finish(value);
|
|
684
|
-
}
|
|
685
|
-
// If this quote is escaped, it's part of the data; skip it
|
|
686
|
-
// If the quote character is the escape character, then check if the next character is the escape character
|
|
687
|
-
if (quoteChar === escapeChar && input[quoteSearch + 1] === escapeChar) {
|
|
688
|
-
quoteSearch++;
|
|
689
|
-
continue;
|
|
690
|
-
}
|
|
691
|
-
// If the quote character is not the escape character, then check if the previous character was the escape character
|
|
692
|
-
if (quoteChar !== escapeChar &&
|
|
693
|
-
quoteSearch !== 0 &&
|
|
694
|
-
input[quoteSearch - 1] === escapeChar) {
|
|
695
|
-
continue;
|
|
696
|
-
}
|
|
697
|
-
// Check up to nextDelim or nextNewline, whichever is closest
|
|
698
|
-
var checkUpTo = nextNewline === -1 ? nextDelim : Math.min(nextDelim, nextNewline);
|
|
699
|
-
var spacesBetweenQuoteAndDelimiter = extraSpaces(checkUpTo);
|
|
700
|
-
// Closing quote followed by delimiter or 'unnecessary spaces + delimiter'
|
|
701
|
-
if (input[quoteSearch + 1 + spacesBetweenQuoteAndDelimiter] === delim) {
|
|
702
|
-
row.push(input.substring(cursor, quoteSearch).replace(quoteCharRegex, quoteChar));
|
|
703
|
-
cursor = quoteSearch + 1 + spacesBetweenQuoteAndDelimiter + delimLen;
|
|
704
|
-
nextDelim = input.indexOf(delim, cursor);
|
|
705
|
-
nextNewline = input.indexOf(newline, cursor);
|
|
706
|
-
if (stepIsFunction) {
|
|
707
|
-
doStep();
|
|
708
|
-
if (aborted)
|
|
709
|
-
return returnable();
|
|
710
|
-
}
|
|
711
|
-
if (preview && data.length >= preview)
|
|
712
|
-
return returnable(true);
|
|
713
|
-
break;
|
|
714
|
-
}
|
|
715
|
-
var spacesBetweenQuoteAndNewLine = extraSpaces(nextNewline);
|
|
716
|
-
// Closing quote followed by newline or 'unnecessary spaces + newLine'
|
|
717
|
-
if (input.substr(quoteSearch + 1 + spacesBetweenQuoteAndNewLine, newlineLen) === newline) {
|
|
718
|
-
row.push(input.substring(cursor, quoteSearch).replace(quoteCharRegex, quoteChar));
|
|
719
|
-
saveRow(quoteSearch + 1 + spacesBetweenQuoteAndNewLine + newlineLen);
|
|
720
|
-
nextDelim = input.indexOf(delim, cursor); // because we may have skipped the nextDelim in the quoted field
|
|
721
|
-
if (stepIsFunction) {
|
|
722
|
-
doStep();
|
|
723
|
-
if (aborted)
|
|
724
|
-
return returnable();
|
|
725
|
-
}
|
|
726
|
-
if (preview && data.length >= preview)
|
|
727
|
-
return returnable(true);
|
|
728
|
-
break;
|
|
729
|
-
}
|
|
730
|
-
// Checks for valid closing quotes are complete (escaped quotes or quote followed by EOF/delimiter/newline) -- assume these quotes are part of an invalid text string
|
|
731
|
-
errors.push({
|
|
732
|
-
type: 'Quotes',
|
|
733
|
-
code: 'InvalidQuotes',
|
|
734
|
-
message: 'Trailing quote on quoted field is malformed',
|
|
735
|
-
row: data.length, // row has yet to be inserted
|
|
736
|
-
index: cursor
|
|
737
|
-
});
|
|
738
|
-
quoteSearch++;
|
|
739
|
-
continue;
|
|
740
|
-
}
|
|
741
|
-
if (stepIsFunction) {
|
|
742
|
-
doStep();
|
|
743
|
-
if (aborted)
|
|
744
|
-
return returnable();
|
|
745
|
-
}
|
|
746
|
-
if (preview && data.length >= preview)
|
|
747
|
-
return returnable(true);
|
|
748
|
-
continue;
|
|
749
|
-
}
|
|
750
|
-
// Comment found at start of new line
|
|
751
|
-
if (comments && row.length === 0 && input.substr(cursor, commentsLen) === comments) {
|
|
752
|
-
if (nextNewline === -1)
|
|
753
|
-
// Comment ends at EOF
|
|
754
|
-
return returnable();
|
|
755
|
-
cursor = nextNewline + newlineLen;
|
|
756
|
-
nextNewline = input.indexOf(newline, cursor);
|
|
757
|
-
nextDelim = input.indexOf(delim, cursor);
|
|
758
|
-
continue;
|
|
759
|
-
}
|
|
760
|
-
// Next delimiter comes before next newline, so we've reached end of field
|
|
761
|
-
if (nextDelim !== -1 && (nextDelim < nextNewline || nextNewline === -1)) {
|
|
762
|
-
row.push(input.substring(cursor, nextDelim));
|
|
763
|
-
cursor = nextDelim + delimLen;
|
|
764
|
-
nextDelim = input.indexOf(delim, cursor);
|
|
765
|
-
continue;
|
|
766
|
-
}
|
|
767
|
-
// End of row
|
|
768
|
-
if (nextNewline !== -1) {
|
|
769
|
-
row.push(input.substring(cursor, nextNewline));
|
|
770
|
-
saveRow(nextNewline + newlineLen);
|
|
771
|
-
if (stepIsFunction) {
|
|
772
|
-
doStep();
|
|
773
|
-
if (aborted)
|
|
774
|
-
return returnable();
|
|
775
|
-
}
|
|
776
|
-
if (preview && data.length >= preview)
|
|
777
|
-
return returnable(true);
|
|
778
|
-
continue;
|
|
779
|
-
}
|
|
780
|
-
break;
|
|
781
|
-
}
|
|
782
|
-
return finish();
|
|
783
|
-
function pushRow(row) {
|
|
784
|
-
data.push(row);
|
|
785
|
-
lastCursor = cursor;
|
|
786
|
-
}
|
|
787
|
-
/**
|
|
788
|
-
* checks if there are extra spaces after closing quote and given index without any text
|
|
789
|
-
* if Yes, returns the number of spaces
|
|
790
|
-
*/
|
|
791
|
-
function extraSpaces(index) {
|
|
792
|
-
var spaceLength = 0;
|
|
793
|
-
if (index !== -1) {
|
|
794
|
-
var textBetweenClosingQuoteAndIndex = input.substring(quoteSearch + 1, index);
|
|
795
|
-
if (textBetweenClosingQuoteAndIndex && textBetweenClosingQuoteAndIndex.trim() === '') {
|
|
796
|
-
spaceLength = textBetweenClosingQuoteAndIndex.length;
|
|
797
|
-
}
|
|
798
|
-
}
|
|
799
|
-
return spaceLength;
|
|
800
|
-
}
|
|
801
|
-
/**
|
|
802
|
-
* Appends the remaining input from cursor to the end into
|
|
803
|
-
* row, saves the row, calls step, and returns the results.
|
|
804
|
-
*/
|
|
805
|
-
function finish(value) {
|
|
806
|
-
if (ignoreLastRow)
|
|
807
|
-
return returnable();
|
|
808
|
-
if (typeof value === 'undefined')
|
|
809
|
-
value = input.substr(cursor);
|
|
810
|
-
row.push(value);
|
|
811
|
-
cursor = inputLen; // important in case parsing is paused
|
|
812
|
-
pushRow(row);
|
|
813
|
-
if (stepIsFunction)
|
|
814
|
-
doStep();
|
|
815
|
-
return returnable();
|
|
816
|
-
}
|
|
817
|
-
/**
|
|
818
|
-
* Appends the current row to the results. It sets the cursor
|
|
819
|
-
* to newCursor and finds the nextNewline. The caller should
|
|
820
|
-
* take care to execute user's step function and check for
|
|
821
|
-
* preview and end parsing if necessary.
|
|
822
|
-
*/
|
|
823
|
-
function saveRow(newCursor) {
|
|
824
|
-
cursor = newCursor;
|
|
825
|
-
pushRow(row);
|
|
826
|
-
row = [];
|
|
827
|
-
nextNewline = input.indexOf(newline, cursor);
|
|
828
|
-
}
|
|
829
|
-
/** Returns an object with the results, errors, and meta. */
|
|
830
|
-
function returnable(stopped, step) {
|
|
831
|
-
var isStep = step || false;
|
|
832
|
-
return {
|
|
833
|
-
data: isStep ? data[0] : data,
|
|
834
|
-
errors: errors,
|
|
835
|
-
meta: {
|
|
836
|
-
delimiter: delim,
|
|
837
|
-
linebreak: newline,
|
|
838
|
-
aborted: aborted,
|
|
839
|
-
truncated: !!stopped,
|
|
840
|
-
cursor: lastCursor + (baseIndex || 0)
|
|
841
|
-
}
|
|
842
|
-
};
|
|
843
|
-
}
|
|
844
|
-
/** Executes the user's step function and resets data & errors. */
|
|
845
|
-
function doStep() {
|
|
846
|
-
step(returnable(undefined, true));
|
|
847
|
-
data = [];
|
|
848
|
-
errors = [];
|
|
849
|
-
}
|
|
850
|
-
};
|
|
851
|
-
/** Sets the abort flag */
|
|
852
|
-
// @ts-expect-error
|
|
853
|
-
this.abort = function () {
|
|
854
|
-
aborted = true;
|
|
855
|
-
};
|
|
856
|
-
/** Gets the cursor position */
|
|
857
|
-
// @ts-expect-error
|
|
858
|
-
this.getCharIndex = function () {
|
|
859
|
-
return cursor;
|
|
860
|
-
};
|
|
861
|
-
}
|
|
862
|
-
/** Makes a deep copy of an array or object (mostly) */
|
|
863
|
-
function copy(obj) {
|
|
864
|
-
if (typeof obj !== 'object' || obj === null)
|
|
865
|
-
return obj;
|
|
866
|
-
var cpy = Array.isArray(obj) ? [] : {};
|
|
867
|
-
for (var key in obj)
|
|
868
|
-
cpy[key] = copy(obj[key]);
|
|
869
|
-
return cpy;
|
|
870
|
-
}
|
|
871
|
-
function isFunction(func) {
|
|
872
|
-
return typeof func === 'function';
|
|
873
|
-
}
|
|
874
|
-
const Papa = {
|
|
13
|
+
import { CsvToJson, Parser, ParserHandle, ChunkStreamer } from "./papa-parser.js";
|
|
14
|
+
import { JsonToCsv } from "./papa-writer.js";
|
|
15
|
+
import { Papa } from "./papa-constants.js";
|
|
16
|
+
export default {
|
|
17
|
+
...Papa,
|
|
875
18
|
parse: CsvToJson,
|
|
876
19
|
unparse: JsonToCsv,
|
|
877
|
-
|
|
878
|
-
UNIT_SEP: String.fromCharCode(31),
|
|
879
|
-
BYTE_ORDER_MARK,
|
|
880
|
-
BAD_DELIMITERS: ['\r', '\n', '"', BYTE_ORDER_MARK],
|
|
881
|
-
WORKERS_SUPPORTED: false, // !IS_WORKER && !!globalThis.Worker
|
|
882
|
-
NODE_STREAM_INPUT: 1,
|
|
883
|
-
// Configurable chunk sizes for local and remote files, respectively
|
|
884
|
-
LocalChunkSize: 1024 * 1024 * 10, // 10 M,
|
|
885
|
-
RemoteChunkSize: 1024 * 1024 * 5, // 5 M,
|
|
886
|
-
DefaultDelimiter: ',', // Used if not specified and detection fail,
|
|
20
|
+
ChunkStreamer,
|
|
887
21
|
// Exposed for testing and development only
|
|
888
|
-
Parser
|
|
889
|
-
ParserHandle
|
|
890
|
-
// BEGIN FORK
|
|
891
|
-
ChunkStreamer: ChunkStreamer
|
|
22
|
+
Parser,
|
|
23
|
+
ParserHandle
|
|
892
24
|
};
|
|
893
|
-
|
|
25
|
+
//# sourceMappingURL=papaparse.js.map
|