@loaders.gl/csv 3.1.3 → 4.0.0-alpha.5
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/bundle.js +2 -2
- package/dist/bundle.js.map +1 -0
- package/dist/csv-loader.js +220 -247
- package/dist/csv-loader.js.map +1 -0
- package/dist/csv-writer.js +2 -6
- package/dist/{es5/csv-writer.js.map → csv-writer.js.map} +0 -0
- package/dist/index.js +2 -5
- package/dist/index.js.map +1 -0
- package/dist/papaparse/async-iterator-streamer.js +32 -60
- package/dist/papaparse/async-iterator-streamer.js.map +1 -0
- package/dist/papaparse/papaparse.js +795 -870
- package/dist/papaparse/papaparse.js.map +1 -0
- package/package.json +6 -6
- package/dist/es5/bundle.js +0 -7
- package/dist/es5/bundle.js.map +0 -1
- package/dist/es5/csv-loader.js +0 -309
- package/dist/es5/csv-loader.js.map +0 -1
- package/dist/es5/csv-writer.js +0 -2
- package/dist/es5/index.js +0 -14
- package/dist/es5/index.js.map +0 -1
- package/dist/es5/papaparse/async-iterator-streamer.js +0 -140
- package/dist/es5/papaparse/async-iterator-streamer.js.map +0 -1
- package/dist/es5/papaparse/papaparse.js +0 -882
- package/dist/es5/papaparse/papaparse.js.map +0 -1
- package/dist/esm/bundle.js +0 -5
- package/dist/esm/bundle.js.map +0 -1
- package/dist/esm/csv-loader.js +0 -240
- package/dist/esm/csv-loader.js.map +0 -1
- package/dist/esm/csv-writer.js +0 -2
- package/dist/esm/csv-writer.js.map +0 -1
- package/dist/esm/index.js +0 -2
- package/dist/esm/index.js.map +0 -1
- package/dist/esm/papaparse/async-iterator-streamer.js +0 -35
- package/dist/esm/papaparse/async-iterator-streamer.js.map +0 -1
- package/dist/esm/papaparse/papaparse.js +0 -860
- package/dist/esm/papaparse/papaparse.js.map +0 -1
|
@@ -1,860 +0,0 @@
|
|
|
1
|
-
/* @license
|
|
2
|
-
Papa Parse
|
|
3
|
-
v5.0.0-beta.0
|
|
4
|
-
https://github.com/mholt/PapaParse
|
|
5
|
-
License: MIT
|
|
6
|
-
*/
|
|
7
|
-
const BYTE_ORDER_MARK = '\ufeff';
|
|
8
|
-
const Papa = {
|
|
9
|
-
parse: CsvToJson,
|
|
10
|
-
unparse: JsonToCsv,
|
|
11
|
-
RECORD_SEP: String.fromCharCode(30),
|
|
12
|
-
UNIT_SEP: String.fromCharCode(31),
|
|
13
|
-
BYTE_ORDER_MARK,
|
|
14
|
-
BAD_DELIMITERS: ['\r', '\n', '"', BYTE_ORDER_MARK],
|
|
15
|
-
WORKERS_SUPPORTED: false,
|
|
16
|
-
NODE_STREAM_INPUT: 1,
|
|
17
|
-
LocalChunkSize: 1024 * 1024 * 10,
|
|
18
|
-
RemoteChunkSize: 1024 * 1024 * 5,
|
|
19
|
-
DefaultDelimiter: ',',
|
|
20
|
-
Parser: Parser,
|
|
21
|
-
ParserHandle: ParserHandle,
|
|
22
|
-
ChunkStreamer: ChunkStreamer,
|
|
23
|
-
StringStreamer: StringStreamer
|
|
24
|
-
};
|
|
25
|
-
export default Papa;
|
|
26
|
-
|
|
27
|
-
function CsvToJson(_input, _config, UserDefinedStreamer) {
|
|
28
|
-
_config = _config || {};
|
|
29
|
-
var dynamicTyping = _config.dynamicTyping || false;
|
|
30
|
-
|
|
31
|
-
if (isFunction(dynamicTyping)) {
|
|
32
|
-
_config.dynamicTypingFunction = dynamicTyping;
|
|
33
|
-
dynamicTyping = {};
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
_config.dynamicTyping = dynamicTyping;
|
|
37
|
-
_config.transform = isFunction(_config.transform) ? _config.transform : false;
|
|
38
|
-
|
|
39
|
-
if (_config.worker && Papa.WORKERS_SUPPORTED) {
|
|
40
|
-
var w = newWorker();
|
|
41
|
-
w.userStep = _config.step;
|
|
42
|
-
w.userChunk = _config.chunk;
|
|
43
|
-
w.userComplete = _config.complete;
|
|
44
|
-
w.userError = _config.error;
|
|
45
|
-
_config.step = isFunction(_config.step);
|
|
46
|
-
_config.chunk = isFunction(_config.chunk);
|
|
47
|
-
_config.complete = isFunction(_config.complete);
|
|
48
|
-
_config.error = isFunction(_config.error);
|
|
49
|
-
delete _config.worker;
|
|
50
|
-
w.postMessage({
|
|
51
|
-
input: _input,
|
|
52
|
-
config: _config,
|
|
53
|
-
workerId: w.id
|
|
54
|
-
});
|
|
55
|
-
return;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
var streamer = null;
|
|
59
|
-
|
|
60
|
-
if (typeof _input === 'string') {
|
|
61
|
-
streamer = new StringStreamer(_config);
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
if (!streamer) {
|
|
65
|
-
streamer = new UserDefinedStreamer(_config);
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
return streamer.stream(_input);
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
function JsonToCsv(_input, _config) {
|
|
72
|
-
var _quotes = false;
|
|
73
|
-
var _writeHeader = true;
|
|
74
|
-
var _delimiter = ',';
|
|
75
|
-
var _newline = '\r\n';
|
|
76
|
-
var _quoteChar = '"';
|
|
77
|
-
|
|
78
|
-
var _escapedQuote = _quoteChar + _quoteChar;
|
|
79
|
-
|
|
80
|
-
var _skipEmptyLines = false;
|
|
81
|
-
var _columns = null;
|
|
82
|
-
unpackConfig();
|
|
83
|
-
var quoteCharRegex = new RegExp(escapeRegExp(_quoteChar), 'g');
|
|
84
|
-
if (typeof _input === 'string') _input = JSON.parse(_input);
|
|
85
|
-
|
|
86
|
-
if (Array.isArray(_input)) {
|
|
87
|
-
if (!_input.length || Array.isArray(_input[0])) return serialize(null, _input, _skipEmptyLines);else if (typeof _input[0] === 'object') return serialize(_columns || objectKeys(_input[0]), _input, _skipEmptyLines);
|
|
88
|
-
} else if (typeof _input === 'object') {
|
|
89
|
-
if (typeof _input.data === 'string') _input.data = JSON.parse(_input.data);
|
|
90
|
-
|
|
91
|
-
if (Array.isArray(_input.data)) {
|
|
92
|
-
if (!_input.fields) _input.fields = _input.meta && _input.meta.fields;
|
|
93
|
-
if (!_input.fields) _input.fields = Array.isArray(_input.data[0]) ? _input.fields : objectKeys(_input.data[0]);
|
|
94
|
-
if (!Array.isArray(_input.data[0]) && typeof _input.data[0] !== 'object') _input.data = [_input.data];
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
return serialize(_input.fields || [], _input.data || [], _skipEmptyLines);
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
throw new Error('Unable to serialize unrecognized input');
|
|
101
|
-
|
|
102
|
-
function unpackConfig() {
|
|
103
|
-
if (typeof _config !== 'object') return;
|
|
104
|
-
|
|
105
|
-
if (typeof _config.delimiter === 'string' && !Papa.BAD_DELIMITERS.filter(function (value) {
|
|
106
|
-
return _config.delimiter.indexOf(value) !== -1;
|
|
107
|
-
}).length) {
|
|
108
|
-
_delimiter = _config.delimiter;
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
if (typeof _config.quotes === 'boolean' || Array.isArray(_config.quotes)) _quotes = _config.quotes;
|
|
112
|
-
if (typeof _config.skipEmptyLines === 'boolean' || typeof _config.skipEmptyLines === 'string') _skipEmptyLines = _config.skipEmptyLines;
|
|
113
|
-
if (typeof _config.newline === 'string') _newline = _config.newline;
|
|
114
|
-
if (typeof _config.quoteChar === 'string') _quoteChar = _config.quoteChar;
|
|
115
|
-
if (typeof _config.header === 'boolean') _writeHeader = _config.header;
|
|
116
|
-
|
|
117
|
-
if (Array.isArray(_config.columns)) {
|
|
118
|
-
if (_config.columns.length === 0) throw new Error('Option columns is empty');
|
|
119
|
-
_columns = _config.columns;
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
if (_config.escapeChar !== undefined) {
|
|
123
|
-
_escapedQuote = _config.escapeChar + _quoteChar;
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
function objectKeys(obj) {
|
|
128
|
-
if (typeof obj !== 'object') return [];
|
|
129
|
-
var keys = [];
|
|
130
|
-
|
|
131
|
-
for (var key in obj) keys.push(key);
|
|
132
|
-
|
|
133
|
-
return keys;
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
function serialize(fields, data, skipEmptyLines) {
|
|
137
|
-
var csv = '';
|
|
138
|
-
if (typeof fields === 'string') fields = JSON.parse(fields);
|
|
139
|
-
if (typeof data === 'string') data = JSON.parse(data);
|
|
140
|
-
var hasHeader = Array.isArray(fields) && fields.length > 0;
|
|
141
|
-
var dataKeyedByField = !Array.isArray(data[0]);
|
|
142
|
-
|
|
143
|
-
if (hasHeader && _writeHeader) {
|
|
144
|
-
for (var i = 0; i < fields.length; i++) {
|
|
145
|
-
if (i > 0) csv += _delimiter;
|
|
146
|
-
csv += safe(fields[i], i);
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
if (data.length > 0) csv += _newline;
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
for (var row = 0; row < data.length; row++) {
|
|
153
|
-
var maxCol = hasHeader ? fields.length : data[row].length;
|
|
154
|
-
var emptyLine = false;
|
|
155
|
-
var nullLine = hasHeader ? Object.keys(data[row]).length === 0 : data[row].length === 0;
|
|
156
|
-
|
|
157
|
-
if (skipEmptyLines && !hasHeader) {
|
|
158
|
-
emptyLine = skipEmptyLines === 'greedy' ? data[row].join('').trim() === '' : data[row].length === 1 && data[row][0].length === 0;
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
if (skipEmptyLines === 'greedy' && hasHeader) {
|
|
162
|
-
var line = [];
|
|
163
|
-
|
|
164
|
-
for (var c = 0; c < maxCol; c++) {
|
|
165
|
-
var cx = dataKeyedByField ? fields[c] : c;
|
|
166
|
-
line.push(data[row][cx]);
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
emptyLine = line.join('').trim() === '';
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
if (!emptyLine) {
|
|
173
|
-
for (var col = 0; col < maxCol; col++) {
|
|
174
|
-
if (col > 0 && !nullLine) csv += _delimiter;
|
|
175
|
-
var colIdx = hasHeader && dataKeyedByField ? fields[col] : col;
|
|
176
|
-
csv += safe(data[row][colIdx], col);
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
if (row < data.length - 1 && (!skipEmptyLines || maxCol > 0 && !nullLine)) {
|
|
180
|
-
csv += _newline;
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
return csv;
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
function safe(str, col) {
|
|
189
|
-
if (typeof str === 'undefined' || str === null) return '';
|
|
190
|
-
if (str.constructor === Date) return JSON.stringify(str).slice(1, 25);
|
|
191
|
-
str = str.toString().replace(quoteCharRegex, _escapedQuote);
|
|
192
|
-
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) === ' ';
|
|
193
|
-
return needsQuotes ? _quoteChar + str + _quoteChar : str;
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
function hasAny(str, substrings) {
|
|
197
|
-
for (var i = 0; i < substrings.length; i++) if (str.indexOf(substrings[i]) > -1) return true;
|
|
198
|
-
|
|
199
|
-
return false;
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
function ChunkStreamer(config) {
|
|
204
|
-
this._handle = null;
|
|
205
|
-
this._finished = false;
|
|
206
|
-
this._completed = false;
|
|
207
|
-
this._input = null;
|
|
208
|
-
this._baseIndex = 0;
|
|
209
|
-
this._partialLine = '';
|
|
210
|
-
this._rowCount = 0;
|
|
211
|
-
this._start = 0;
|
|
212
|
-
this._nextChunk = null;
|
|
213
|
-
this.isFirstChunk = true;
|
|
214
|
-
this._completeResults = {
|
|
215
|
-
data: [],
|
|
216
|
-
errors: [],
|
|
217
|
-
meta: {}
|
|
218
|
-
};
|
|
219
|
-
replaceConfig.call(this, config);
|
|
220
|
-
|
|
221
|
-
this.parseChunk = function (chunk, isFakeChunk) {
|
|
222
|
-
if (this.isFirstChunk && isFunction(this._config.beforeFirstChunk)) {
|
|
223
|
-
var modifiedChunk = this._config.beforeFirstChunk(chunk);
|
|
224
|
-
|
|
225
|
-
if (modifiedChunk !== undefined) chunk = modifiedChunk;
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
this.isFirstChunk = false;
|
|
229
|
-
var aggregate = this._partialLine + chunk;
|
|
230
|
-
this._partialLine = '';
|
|
231
|
-
|
|
232
|
-
var results = this._handle.parse(aggregate, this._baseIndex, !this._finished);
|
|
233
|
-
|
|
234
|
-
if (this._handle.paused() || this._handle.aborted()) return;
|
|
235
|
-
var lastIndex = results.meta.cursor;
|
|
236
|
-
|
|
237
|
-
if (!this._finished) {
|
|
238
|
-
this._partialLine = aggregate.substring(lastIndex - this._baseIndex);
|
|
239
|
-
this._baseIndex = lastIndex;
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
if (results && results.data) this._rowCount += results.data.length;
|
|
243
|
-
var finishedIncludingPreview = this._finished || this._config.preview && this._rowCount >= this._config.preview;
|
|
244
|
-
|
|
245
|
-
if (isFunction(this._config.chunk) && !isFakeChunk) {
|
|
246
|
-
this._config.chunk(results, this._handle);
|
|
247
|
-
|
|
248
|
-
if (this._handle.paused() || this._handle.aborted()) return;
|
|
249
|
-
results = undefined;
|
|
250
|
-
this._completeResults = undefined;
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
if (!this._config.step && !this._config.chunk) {
|
|
254
|
-
this._completeResults.data = this._completeResults.data.concat(results.data);
|
|
255
|
-
this._completeResults.errors = this._completeResults.errors.concat(results.errors);
|
|
256
|
-
this._completeResults.meta = results.meta;
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
if (!this._completed && finishedIncludingPreview && isFunction(this._config.complete) && (!results || !results.meta.aborted)) {
|
|
260
|
-
this._config.complete(this._completeResults, this._input);
|
|
261
|
-
|
|
262
|
-
this._completed = true;
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
if (!finishedIncludingPreview && (!results || !results.meta.paused)) this._nextChunk();
|
|
266
|
-
return results;
|
|
267
|
-
};
|
|
268
|
-
|
|
269
|
-
this._sendError = function (error) {
|
|
270
|
-
if (isFunction(this._config.error)) this._config.error(error);
|
|
271
|
-
};
|
|
272
|
-
|
|
273
|
-
function replaceConfig(config) {
|
|
274
|
-
var configCopy = copy(config);
|
|
275
|
-
configCopy.chunkSize = parseInt(configCopy.chunkSize);
|
|
276
|
-
if (!config.step && !config.chunk) configCopy.chunkSize = null;
|
|
277
|
-
this._handle = new ParserHandle(configCopy);
|
|
278
|
-
this._handle.streamer = this;
|
|
279
|
-
this._config = configCopy;
|
|
280
|
-
}
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
function StringStreamer(config) {
|
|
284
|
-
config = config || {};
|
|
285
|
-
ChunkStreamer.call(this, config);
|
|
286
|
-
var remaining;
|
|
287
|
-
|
|
288
|
-
this.stream = function (s) {
|
|
289
|
-
remaining = s;
|
|
290
|
-
return this._nextChunk();
|
|
291
|
-
};
|
|
292
|
-
|
|
293
|
-
this._nextChunk = function () {
|
|
294
|
-
if (this._finished) return;
|
|
295
|
-
var size = this._config.chunkSize;
|
|
296
|
-
var chunk = size ? remaining.substr(0, size) : remaining;
|
|
297
|
-
remaining = size ? remaining.substr(size) : '';
|
|
298
|
-
this._finished = !remaining;
|
|
299
|
-
return this.parseChunk(chunk);
|
|
300
|
-
};
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
StringStreamer.prototype = Object.create(StringStreamer.prototype);
|
|
304
|
-
StringStreamer.prototype.constructor = StringStreamer;
|
|
305
|
-
|
|
306
|
-
function ParserHandle(_config) {
|
|
307
|
-
var FLOAT = /^\s*-?(\d*\.?\d+|\d+\.?\d*)(e[-+]?\d+)?\s*$/i;
|
|
308
|
-
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))/;
|
|
309
|
-
var self = this;
|
|
310
|
-
var _stepCounter = 0;
|
|
311
|
-
var _rowCounter = 0;
|
|
312
|
-
|
|
313
|
-
var _input;
|
|
314
|
-
|
|
315
|
-
var _parser;
|
|
316
|
-
|
|
317
|
-
var _paused = false;
|
|
318
|
-
var _aborted = false;
|
|
319
|
-
|
|
320
|
-
var _delimiterError;
|
|
321
|
-
|
|
322
|
-
var _fields = [];
|
|
323
|
-
var _results = {
|
|
324
|
-
data: [],
|
|
325
|
-
errors: [],
|
|
326
|
-
meta: {}
|
|
327
|
-
};
|
|
328
|
-
|
|
329
|
-
if (isFunction(_config.step)) {
|
|
330
|
-
var userStep = _config.step;
|
|
331
|
-
|
|
332
|
-
_config.step = function (results) {
|
|
333
|
-
_results = results;
|
|
334
|
-
if (needsHeaderRow()) processResults();else {
|
|
335
|
-
processResults();
|
|
336
|
-
if (!_results.data || _results.data.length === 0) return;
|
|
337
|
-
_stepCounter += results.data.length;
|
|
338
|
-
if (_config.preview && _stepCounter > _config.preview) _parser.abort();else userStep(_results, self);
|
|
339
|
-
}
|
|
340
|
-
};
|
|
341
|
-
}
|
|
342
|
-
|
|
343
|
-
this.parse = function (input, baseIndex, ignoreLastRow) {
|
|
344
|
-
var quoteChar = _config.quoteChar || '"';
|
|
345
|
-
if (!_config.newline) _config.newline = guessLineEndings(input, quoteChar);
|
|
346
|
-
_delimiterError = false;
|
|
347
|
-
|
|
348
|
-
if (!_config.delimiter) {
|
|
349
|
-
var delimGuess = guessDelimiter(input, _config.newline, _config.skipEmptyLines, _config.comments, _config.delimitersToGuess);
|
|
350
|
-
if (delimGuess.successful) _config.delimiter = delimGuess.bestDelimiter;else {
|
|
351
|
-
_delimiterError = true;
|
|
352
|
-
_config.delimiter = Papa.DefaultDelimiter;
|
|
353
|
-
}
|
|
354
|
-
_results.meta.delimiter = _config.delimiter;
|
|
355
|
-
} else if (isFunction(_config.delimiter)) {
|
|
356
|
-
_config.delimiter = _config.delimiter(input);
|
|
357
|
-
_results.meta.delimiter = _config.delimiter;
|
|
358
|
-
}
|
|
359
|
-
|
|
360
|
-
var parserConfig = copy(_config);
|
|
361
|
-
if (_config.preview && _config.header) parserConfig.preview++;
|
|
362
|
-
_input = input;
|
|
363
|
-
_parser = new Parser(parserConfig);
|
|
364
|
-
_results = _parser.parse(_input, baseIndex, ignoreLastRow);
|
|
365
|
-
processResults();
|
|
366
|
-
return _paused ? {
|
|
367
|
-
meta: {
|
|
368
|
-
paused: true
|
|
369
|
-
}
|
|
370
|
-
} : _results || {
|
|
371
|
-
meta: {
|
|
372
|
-
paused: false
|
|
373
|
-
}
|
|
374
|
-
};
|
|
375
|
-
};
|
|
376
|
-
|
|
377
|
-
this.paused = function () {
|
|
378
|
-
return _paused;
|
|
379
|
-
};
|
|
380
|
-
|
|
381
|
-
this.pause = function () {
|
|
382
|
-
_paused = true;
|
|
383
|
-
|
|
384
|
-
_parser.abort();
|
|
385
|
-
|
|
386
|
-
_input = _input.substr(_parser.getCharIndex());
|
|
387
|
-
};
|
|
388
|
-
|
|
389
|
-
this.resume = function () {
|
|
390
|
-
_paused = false;
|
|
391
|
-
self.streamer.parseChunk(_input, true);
|
|
392
|
-
};
|
|
393
|
-
|
|
394
|
-
this.aborted = function () {
|
|
395
|
-
return _aborted;
|
|
396
|
-
};
|
|
397
|
-
|
|
398
|
-
this.abort = function () {
|
|
399
|
-
_aborted = true;
|
|
400
|
-
|
|
401
|
-
_parser.abort();
|
|
402
|
-
|
|
403
|
-
_results.meta.aborted = true;
|
|
404
|
-
if (isFunction(_config.complete)) _config.complete(_results);
|
|
405
|
-
_input = '';
|
|
406
|
-
};
|
|
407
|
-
|
|
408
|
-
function testEmptyLine(s) {
|
|
409
|
-
return _config.skipEmptyLines === 'greedy' ? s.join('').trim() === '' : s.length === 1 && s[0].length === 0;
|
|
410
|
-
}
|
|
411
|
-
|
|
412
|
-
function processResults() {
|
|
413
|
-
if (_results && _delimiterError) {
|
|
414
|
-
addError('Delimiter', 'UndetectableDelimiter', "Unable to auto-detect delimiting character; defaulted to '" + Papa.DefaultDelimiter + "'");
|
|
415
|
-
_delimiterError = false;
|
|
416
|
-
}
|
|
417
|
-
|
|
418
|
-
if (_config.skipEmptyLines) {
|
|
419
|
-
for (var i = 0; i < _results.data.length; i++) if (testEmptyLine(_results.data[i])) _results.data.splice(i--, 1);
|
|
420
|
-
}
|
|
421
|
-
|
|
422
|
-
if (needsHeaderRow()) fillHeaderFields();
|
|
423
|
-
return applyHeaderAndDynamicTypingAndTransformation();
|
|
424
|
-
}
|
|
425
|
-
|
|
426
|
-
function needsHeaderRow() {
|
|
427
|
-
return _config.header && _fields.length === 0;
|
|
428
|
-
}
|
|
429
|
-
|
|
430
|
-
function fillHeaderFields() {
|
|
431
|
-
if (!_results) return;
|
|
432
|
-
|
|
433
|
-
function addHeder(header) {
|
|
434
|
-
if (isFunction(_config.transformHeader)) header = _config.transformHeader(header);
|
|
435
|
-
|
|
436
|
-
_fields.push(header);
|
|
437
|
-
}
|
|
438
|
-
|
|
439
|
-
if (Array.isArray(_results.data[0])) {
|
|
440
|
-
for (var i = 0; needsHeaderRow() && i < _results.data.length; i++) _results.data[i].forEach(addHeder);
|
|
441
|
-
|
|
442
|
-
_results.data.splice(0, 1);
|
|
443
|
-
} else _results.data.forEach(addHeder);
|
|
444
|
-
}
|
|
445
|
-
|
|
446
|
-
function shouldApplyDynamicTyping(field) {
|
|
447
|
-
if (_config.dynamicTypingFunction && _config.dynamicTyping[field] === undefined) {
|
|
448
|
-
_config.dynamicTyping[field] = _config.dynamicTypingFunction(field);
|
|
449
|
-
}
|
|
450
|
-
|
|
451
|
-
return (_config.dynamicTyping[field] || _config.dynamicTyping) === true;
|
|
452
|
-
}
|
|
453
|
-
|
|
454
|
-
function parseDynamic(field, value) {
|
|
455
|
-
if (shouldApplyDynamicTyping(field)) {
|
|
456
|
-
if (value === 'true' || value === 'TRUE') return true;else if (value === 'false' || value === 'FALSE') return false;else if (FLOAT.test(value)) return parseFloat(value);else if (ISO_DATE.test(value)) return new Date(value);else return value === '' ? null : value;
|
|
457
|
-
}
|
|
458
|
-
|
|
459
|
-
return value;
|
|
460
|
-
}
|
|
461
|
-
|
|
462
|
-
function applyHeaderAndDynamicTypingAndTransformation() {
|
|
463
|
-
if (!_results || !_results.data || !_config.header && !_config.dynamicTyping && !_config.transform) return _results;
|
|
464
|
-
|
|
465
|
-
function processRow(rowSource, i) {
|
|
466
|
-
var row = _config.header ? {} : [];
|
|
467
|
-
var j;
|
|
468
|
-
|
|
469
|
-
for (j = 0; j < rowSource.length; j++) {
|
|
470
|
-
var field = j;
|
|
471
|
-
var value = rowSource[j];
|
|
472
|
-
if (_config.header) field = j >= _fields.length ? '__parsed_extra' : _fields[j];
|
|
473
|
-
if (_config.transform) value = _config.transform(value, field);
|
|
474
|
-
value = parseDynamic(field, value);
|
|
475
|
-
|
|
476
|
-
if (field === '__parsed_extra') {
|
|
477
|
-
row[field] = row[field] || [];
|
|
478
|
-
row[field].push(value);
|
|
479
|
-
} else row[field] = value;
|
|
480
|
-
}
|
|
481
|
-
|
|
482
|
-
if (_config.header) {
|
|
483
|
-
if (j > _fields.length) addError('FieldMismatch', 'TooManyFields', 'Too many fields: expected ' + _fields.length + ' fields but parsed ' + j, _rowCounter + i);else if (j < _fields.length) addError('FieldMismatch', 'TooFewFields', 'Too few fields: expected ' + _fields.length + ' fields but parsed ' + j, _rowCounter + i);
|
|
484
|
-
}
|
|
485
|
-
|
|
486
|
-
return row;
|
|
487
|
-
}
|
|
488
|
-
|
|
489
|
-
var incrementBy = 1;
|
|
490
|
-
|
|
491
|
-
if (!_results.data[0] || Array.isArray(_results.data[0])) {
|
|
492
|
-
_results.data = _results.data.map(processRow);
|
|
493
|
-
incrementBy = _results.data.length;
|
|
494
|
-
} else _results.data = processRow(_results.data, 0);
|
|
495
|
-
|
|
496
|
-
if (_config.header && _results.meta) _results.meta.fields = _fields;
|
|
497
|
-
_rowCounter += incrementBy;
|
|
498
|
-
return _results;
|
|
499
|
-
}
|
|
500
|
-
|
|
501
|
-
function guessDelimiter(input, newline, skipEmptyLines, comments, delimitersToGuess) {
|
|
502
|
-
var bestDelim, bestDelta, fieldCountPrevRow;
|
|
503
|
-
delimitersToGuess = delimitersToGuess || [',', '\t', '|', ';', Papa.RECORD_SEP, Papa.UNIT_SEP];
|
|
504
|
-
|
|
505
|
-
for (var i = 0; i < delimitersToGuess.length; i++) {
|
|
506
|
-
var delim = delimitersToGuess[i];
|
|
507
|
-
var delta = 0,
|
|
508
|
-
avgFieldCount = 0,
|
|
509
|
-
emptyLinesCount = 0;
|
|
510
|
-
fieldCountPrevRow = undefined;
|
|
511
|
-
var preview = new Parser({
|
|
512
|
-
comments: comments,
|
|
513
|
-
delimiter: delim,
|
|
514
|
-
newline: newline,
|
|
515
|
-
preview: 10
|
|
516
|
-
}).parse(input);
|
|
517
|
-
|
|
518
|
-
for (var j = 0; j < preview.data.length; j++) {
|
|
519
|
-
if (skipEmptyLines && testEmptyLine(preview.data[j])) {
|
|
520
|
-
emptyLinesCount++;
|
|
521
|
-
continue;
|
|
522
|
-
}
|
|
523
|
-
|
|
524
|
-
var fieldCount = preview.data[j].length;
|
|
525
|
-
avgFieldCount += fieldCount;
|
|
526
|
-
|
|
527
|
-
if (typeof fieldCountPrevRow === 'undefined') {
|
|
528
|
-
fieldCountPrevRow = 0;
|
|
529
|
-
continue;
|
|
530
|
-
} else if (fieldCount > 1) {
|
|
531
|
-
delta += Math.abs(fieldCount - fieldCountPrevRow);
|
|
532
|
-
fieldCountPrevRow = fieldCount;
|
|
533
|
-
}
|
|
534
|
-
}
|
|
535
|
-
|
|
536
|
-
if (preview.data.length > 0) avgFieldCount /= preview.data.length - emptyLinesCount;
|
|
537
|
-
|
|
538
|
-
if ((typeof bestDelta === 'undefined' || delta > bestDelta) && avgFieldCount > 1.99) {
|
|
539
|
-
bestDelta = delta;
|
|
540
|
-
bestDelim = delim;
|
|
541
|
-
}
|
|
542
|
-
}
|
|
543
|
-
|
|
544
|
-
_config.delimiter = bestDelim;
|
|
545
|
-
return {
|
|
546
|
-
successful: !!bestDelim,
|
|
547
|
-
bestDelimiter: bestDelim
|
|
548
|
-
};
|
|
549
|
-
}
|
|
550
|
-
|
|
551
|
-
function guessLineEndings(input, quoteChar) {
|
|
552
|
-
input = input.substr(0, 1024 * 1024);
|
|
553
|
-
var re = new RegExp(escapeRegExp(quoteChar) + '([^]*?)' + escapeRegExp(quoteChar), 'gm');
|
|
554
|
-
input = input.replace(re, '');
|
|
555
|
-
var r = input.split('\r');
|
|
556
|
-
var n = input.split('\n');
|
|
557
|
-
var nAppearsFirst = n.length > 1 && n[0].length < r[0].length;
|
|
558
|
-
if (r.length === 1 || nAppearsFirst) return '\n';
|
|
559
|
-
var numWithN = 0;
|
|
560
|
-
|
|
561
|
-
for (var i = 0; i < r.length; i++) {
|
|
562
|
-
if (r[i][0] === '\n') numWithN++;
|
|
563
|
-
}
|
|
564
|
-
|
|
565
|
-
return numWithN >= r.length / 2 ? '\r\n' : '\r';
|
|
566
|
-
}
|
|
567
|
-
|
|
568
|
-
function addError(type, code, msg, row) {
|
|
569
|
-
_results.errors.push({
|
|
570
|
-
type: type,
|
|
571
|
-
code: code,
|
|
572
|
-
message: msg,
|
|
573
|
-
row: row
|
|
574
|
-
});
|
|
575
|
-
}
|
|
576
|
-
}
|
|
577
|
-
|
|
578
|
-
function escapeRegExp(string) {
|
|
579
|
-
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
580
|
-
}
|
|
581
|
-
|
|
582
|
-
function Parser(config) {
|
|
583
|
-
config = config || {};
|
|
584
|
-
var delim = config.delimiter;
|
|
585
|
-
var newline = config.newline;
|
|
586
|
-
var comments = config.comments;
|
|
587
|
-
var step = config.step;
|
|
588
|
-
var preview = config.preview;
|
|
589
|
-
var fastMode = config.fastMode;
|
|
590
|
-
var quoteChar;
|
|
591
|
-
|
|
592
|
-
if (config.quoteChar === undefined) {
|
|
593
|
-
quoteChar = '"';
|
|
594
|
-
} else {
|
|
595
|
-
quoteChar = config.quoteChar;
|
|
596
|
-
}
|
|
597
|
-
|
|
598
|
-
var escapeChar = quoteChar;
|
|
599
|
-
|
|
600
|
-
if (config.escapeChar !== undefined) {
|
|
601
|
-
escapeChar = config.escapeChar;
|
|
602
|
-
}
|
|
603
|
-
|
|
604
|
-
if (typeof delim !== 'string' || Papa.BAD_DELIMITERS.indexOf(delim) > -1) delim = ',';
|
|
605
|
-
if (comments === delim) throw new Error('Comment character same as delimiter');else if (comments === true) comments = '#';else if (typeof comments !== 'string' || Papa.BAD_DELIMITERS.indexOf(comments) > -1) comments = false;
|
|
606
|
-
if (newline !== '\n' && newline !== '\r' && newline !== '\r\n') newline = '\n';
|
|
607
|
-
var cursor = 0;
|
|
608
|
-
var aborted = false;
|
|
609
|
-
|
|
610
|
-
this.parse = function (input, baseIndex, ignoreLastRow) {
|
|
611
|
-
if (typeof input !== 'string') throw new Error('Input must be a string');
|
|
612
|
-
var inputLen = input.length,
|
|
613
|
-
delimLen = delim.length,
|
|
614
|
-
newlineLen = newline.length,
|
|
615
|
-
commentsLen = comments.length;
|
|
616
|
-
var stepIsFunction = isFunction(step);
|
|
617
|
-
cursor = 0;
|
|
618
|
-
var data = [],
|
|
619
|
-
errors = [],
|
|
620
|
-
row = [],
|
|
621
|
-
lastCursor = 0;
|
|
622
|
-
if (!input) return returnable();
|
|
623
|
-
|
|
624
|
-
if (fastMode || fastMode !== false && input.indexOf(quoteChar) === -1) {
|
|
625
|
-
var rows = input.split(newline);
|
|
626
|
-
|
|
627
|
-
for (var i = 0; i < rows.length; i++) {
|
|
628
|
-
row = rows[i];
|
|
629
|
-
cursor += row.length;
|
|
630
|
-
if (i !== rows.length - 1) cursor += newline.length;else if (ignoreLastRow) return returnable();
|
|
631
|
-
if (comments && row.substr(0, commentsLen) === comments) continue;
|
|
632
|
-
|
|
633
|
-
if (stepIsFunction) {
|
|
634
|
-
data = [];
|
|
635
|
-
pushRow(row.split(delim));
|
|
636
|
-
doStep();
|
|
637
|
-
if (aborted) return returnable();
|
|
638
|
-
} else pushRow(row.split(delim));
|
|
639
|
-
|
|
640
|
-
if (preview && i >= preview) {
|
|
641
|
-
data = data.slice(0, preview);
|
|
642
|
-
return returnable(true);
|
|
643
|
-
}
|
|
644
|
-
}
|
|
645
|
-
|
|
646
|
-
return returnable();
|
|
647
|
-
}
|
|
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
|
-
|
|
654
|
-
for (;;) {
|
|
655
|
-
if (input[cursor] === quoteChar) {
|
|
656
|
-
quoteSearch = cursor;
|
|
657
|
-
cursor++;
|
|
658
|
-
|
|
659
|
-
for (;;) {
|
|
660
|
-
quoteSearch = input.indexOf(quoteChar, quoteSearch + 1);
|
|
661
|
-
|
|
662
|
-
if (quoteSearch === -1) {
|
|
663
|
-
if (!ignoreLastRow) {
|
|
664
|
-
errors.push({
|
|
665
|
-
type: 'Quotes',
|
|
666
|
-
code: 'MissingQuotes',
|
|
667
|
-
message: 'Quoted field unterminated',
|
|
668
|
-
row: data.length,
|
|
669
|
-
index: cursor
|
|
670
|
-
});
|
|
671
|
-
}
|
|
672
|
-
|
|
673
|
-
return finish();
|
|
674
|
-
}
|
|
675
|
-
|
|
676
|
-
if (quoteSearch === inputLen - 1) {
|
|
677
|
-
var value = input.substring(cursor, quoteSearch).replace(quoteCharRegex, quoteChar);
|
|
678
|
-
return finish(value);
|
|
679
|
-
}
|
|
680
|
-
|
|
681
|
-
if (quoteChar === escapeChar && input[quoteSearch + 1] === escapeChar) {
|
|
682
|
-
quoteSearch++;
|
|
683
|
-
continue;
|
|
684
|
-
}
|
|
685
|
-
|
|
686
|
-
if (quoteChar !== escapeChar && quoteSearch !== 0 && input[quoteSearch - 1] === escapeChar) {
|
|
687
|
-
continue;
|
|
688
|
-
}
|
|
689
|
-
|
|
690
|
-
var checkUpTo = nextNewline === -1 ? nextDelim : Math.min(nextDelim, nextNewline);
|
|
691
|
-
var spacesBetweenQuoteAndDelimiter = extraSpaces(checkUpTo);
|
|
692
|
-
|
|
693
|
-
if (input[quoteSearch + 1 + spacesBetweenQuoteAndDelimiter] === delim) {
|
|
694
|
-
row.push(input.substring(cursor, quoteSearch).replace(quoteCharRegex, quoteChar));
|
|
695
|
-
cursor = quoteSearch + 1 + spacesBetweenQuoteAndDelimiter + delimLen;
|
|
696
|
-
nextDelim = input.indexOf(delim, cursor);
|
|
697
|
-
nextNewline = input.indexOf(newline, cursor);
|
|
698
|
-
|
|
699
|
-
if (stepIsFunction) {
|
|
700
|
-
doStep();
|
|
701
|
-
if (aborted) return returnable();
|
|
702
|
-
}
|
|
703
|
-
|
|
704
|
-
if (preview && data.length >= preview) return returnable(true);
|
|
705
|
-
break;
|
|
706
|
-
}
|
|
707
|
-
|
|
708
|
-
var spacesBetweenQuoteAndNewLine = extraSpaces(nextNewline);
|
|
709
|
-
|
|
710
|
-
if (input.substr(quoteSearch + 1 + spacesBetweenQuoteAndNewLine, newlineLen) === newline) {
|
|
711
|
-
row.push(input.substring(cursor, quoteSearch).replace(quoteCharRegex, quoteChar));
|
|
712
|
-
saveRow(quoteSearch + 1 + spacesBetweenQuoteAndNewLine + newlineLen);
|
|
713
|
-
nextDelim = input.indexOf(delim, cursor);
|
|
714
|
-
|
|
715
|
-
if (stepIsFunction) {
|
|
716
|
-
doStep();
|
|
717
|
-
if (aborted) return returnable();
|
|
718
|
-
}
|
|
719
|
-
|
|
720
|
-
if (preview && data.length >= preview) return returnable(true);
|
|
721
|
-
break;
|
|
722
|
-
}
|
|
723
|
-
|
|
724
|
-
errors.push({
|
|
725
|
-
type: 'Quotes',
|
|
726
|
-
code: 'InvalidQuotes',
|
|
727
|
-
message: 'Trailing quote on quoted field is malformed',
|
|
728
|
-
row: data.length,
|
|
729
|
-
index: cursor
|
|
730
|
-
});
|
|
731
|
-
quoteSearch++;
|
|
732
|
-
continue;
|
|
733
|
-
}
|
|
734
|
-
|
|
735
|
-
if (stepIsFunction) {
|
|
736
|
-
doStep();
|
|
737
|
-
if (aborted) return returnable();
|
|
738
|
-
}
|
|
739
|
-
|
|
740
|
-
if (preview && data.length >= preview) return returnable(true);
|
|
741
|
-
continue;
|
|
742
|
-
}
|
|
743
|
-
|
|
744
|
-
if (comments && row.length === 0 && input.substr(cursor, commentsLen) === comments) {
|
|
745
|
-
if (nextNewline === -1) return returnable();
|
|
746
|
-
cursor = nextNewline + newlineLen;
|
|
747
|
-
nextNewline = input.indexOf(newline, cursor);
|
|
748
|
-
nextDelim = input.indexOf(delim, cursor);
|
|
749
|
-
continue;
|
|
750
|
-
}
|
|
751
|
-
|
|
752
|
-
if (nextDelim !== -1 && (nextDelim < nextNewline || nextNewline === -1)) {
|
|
753
|
-
row.push(input.substring(cursor, nextDelim));
|
|
754
|
-
cursor = nextDelim + delimLen;
|
|
755
|
-
nextDelim = input.indexOf(delim, cursor);
|
|
756
|
-
continue;
|
|
757
|
-
}
|
|
758
|
-
|
|
759
|
-
if (nextNewline !== -1) {
|
|
760
|
-
row.push(input.substring(cursor, nextNewline));
|
|
761
|
-
saveRow(nextNewline + newlineLen);
|
|
762
|
-
|
|
763
|
-
if (stepIsFunction) {
|
|
764
|
-
doStep();
|
|
765
|
-
if (aborted) return returnable();
|
|
766
|
-
}
|
|
767
|
-
|
|
768
|
-
if (preview && data.length >= preview) return returnable(true);
|
|
769
|
-
continue;
|
|
770
|
-
}
|
|
771
|
-
|
|
772
|
-
break;
|
|
773
|
-
}
|
|
774
|
-
|
|
775
|
-
return finish();
|
|
776
|
-
|
|
777
|
-
function pushRow(row) {
|
|
778
|
-
data.push(row);
|
|
779
|
-
lastCursor = cursor;
|
|
780
|
-
}
|
|
781
|
-
|
|
782
|
-
function extraSpaces(index) {
|
|
783
|
-
var spaceLength = 0;
|
|
784
|
-
|
|
785
|
-
if (index !== -1) {
|
|
786
|
-
var textBetweenClosingQuoteAndIndex = input.substring(quoteSearch + 1, index);
|
|
787
|
-
|
|
788
|
-
if (textBetweenClosingQuoteAndIndex && textBetweenClosingQuoteAndIndex.trim() === '') {
|
|
789
|
-
spaceLength = textBetweenClosingQuoteAndIndex.length;
|
|
790
|
-
}
|
|
791
|
-
}
|
|
792
|
-
|
|
793
|
-
return spaceLength;
|
|
794
|
-
}
|
|
795
|
-
|
|
796
|
-
function finish(value) {
|
|
797
|
-
if (ignoreLastRow) return returnable();
|
|
798
|
-
if (typeof value === 'undefined') value = input.substr(cursor);
|
|
799
|
-
row.push(value);
|
|
800
|
-
cursor = inputLen;
|
|
801
|
-
pushRow(row);
|
|
802
|
-
if (stepIsFunction) doStep();
|
|
803
|
-
return returnable();
|
|
804
|
-
}
|
|
805
|
-
|
|
806
|
-
function saveRow(newCursor) {
|
|
807
|
-
cursor = newCursor;
|
|
808
|
-
pushRow(row);
|
|
809
|
-
row = [];
|
|
810
|
-
nextNewline = input.indexOf(newline, cursor);
|
|
811
|
-
}
|
|
812
|
-
|
|
813
|
-
function returnable(stopped, step) {
|
|
814
|
-
var isStep = step || false;
|
|
815
|
-
return {
|
|
816
|
-
data: isStep ? data[0] : data,
|
|
817
|
-
errors: errors,
|
|
818
|
-
meta: {
|
|
819
|
-
delimiter: delim,
|
|
820
|
-
linebreak: newline,
|
|
821
|
-
aborted: aborted,
|
|
822
|
-
truncated: !!stopped,
|
|
823
|
-
cursor: lastCursor + (baseIndex || 0)
|
|
824
|
-
}
|
|
825
|
-
};
|
|
826
|
-
}
|
|
827
|
-
|
|
828
|
-
function doStep() {
|
|
829
|
-
step(returnable(undefined, true));
|
|
830
|
-
data = [];
|
|
831
|
-
errors = [];
|
|
832
|
-
}
|
|
833
|
-
};
|
|
834
|
-
|
|
835
|
-
this.abort = function () {
|
|
836
|
-
aborted = true;
|
|
837
|
-
};
|
|
838
|
-
|
|
839
|
-
this.getCharIndex = function () {
|
|
840
|
-
return cursor;
|
|
841
|
-
};
|
|
842
|
-
}
|
|
843
|
-
|
|
844
|
-
function notImplemented() {
|
|
845
|
-
throw new Error('Not implemented.');
|
|
846
|
-
}
|
|
847
|
-
|
|
848
|
-
function copy(obj) {
|
|
849
|
-
if (typeof obj !== 'object' || obj === null) return obj;
|
|
850
|
-
var cpy = Array.isArray(obj) ? [] : {};
|
|
851
|
-
|
|
852
|
-
for (var key in obj) cpy[key] = copy(obj[key]);
|
|
853
|
-
|
|
854
|
-
return cpy;
|
|
855
|
-
}
|
|
856
|
-
|
|
857
|
-
function isFunction(func) {
|
|
858
|
-
return typeof func === 'function';
|
|
859
|
-
}
|
|
860
|
-
//# sourceMappingURL=papaparse.js.map
|