convert-buddy-js 0.2.0 → 0.4.0
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/README.md +26 -2
- package/dist/{chunk-B44HYXEP.js → chunk-27H3T556.js} +11 -3
- package/dist/src/index.d.ts +1 -1
- package/dist/src/index.js +1 -2
- package/package.json +7 -1
- package/dist/chunk-VUNV25KB.js +0 -16
- package/dist/fxp-TCYZYIHZ.js +0 -1701
- package/dist/papaparse-4KNMW7KX.js +0 -1324
- package/dist/src-YEKAFGYK.js +0 -3001
- package/dist/sync-FQGEFK5M.js +0 -1492
package/dist/sync-FQGEFK5M.js
DELETED
|
@@ -1,1492 +0,0 @@
|
|
|
1
|
-
import "./chunk-VUNV25KB.js";
|
|
2
|
-
|
|
3
|
-
// ../../node_modules/csv-parse/lib/api/CsvError.js
|
|
4
|
-
var CsvError = class _CsvError extends Error {
|
|
5
|
-
constructor(code, message, options, ...contexts) {
|
|
6
|
-
if (Array.isArray(message)) message = message.join(" ").trim();
|
|
7
|
-
super(message);
|
|
8
|
-
if (Error.captureStackTrace !== void 0) {
|
|
9
|
-
Error.captureStackTrace(this, _CsvError);
|
|
10
|
-
}
|
|
11
|
-
this.code = code;
|
|
12
|
-
for (const context of contexts) {
|
|
13
|
-
for (const key in context) {
|
|
14
|
-
const value = context[key];
|
|
15
|
-
this[key] = Buffer.isBuffer(value) ? value.toString(options.encoding) : value == null ? value : JSON.parse(JSON.stringify(value));
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
// ../../node_modules/csv-parse/lib/utils/is_object.js
|
|
22
|
-
var is_object = function(obj) {
|
|
23
|
-
return typeof obj === "object" && obj !== null && !Array.isArray(obj);
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
// ../../node_modules/csv-parse/lib/api/normalize_columns_array.js
|
|
27
|
-
var normalize_columns_array = function(columns) {
|
|
28
|
-
const normalizedColumns = [];
|
|
29
|
-
for (let i = 0, l = columns.length; i < l; i++) {
|
|
30
|
-
const column = columns[i];
|
|
31
|
-
if (column === void 0 || column === null || column === false) {
|
|
32
|
-
normalizedColumns[i] = { disabled: true };
|
|
33
|
-
} else if (typeof column === "string") {
|
|
34
|
-
normalizedColumns[i] = { name: column };
|
|
35
|
-
} else if (is_object(column)) {
|
|
36
|
-
if (typeof column.name !== "string") {
|
|
37
|
-
throw new CsvError("CSV_OPTION_COLUMNS_MISSING_NAME", [
|
|
38
|
-
"Option columns missing name:",
|
|
39
|
-
`property "name" is required at position ${i}`,
|
|
40
|
-
"when column is an object literal"
|
|
41
|
-
]);
|
|
42
|
-
}
|
|
43
|
-
normalizedColumns[i] = column;
|
|
44
|
-
} else {
|
|
45
|
-
throw new CsvError("CSV_INVALID_COLUMN_DEFINITION", [
|
|
46
|
-
"Invalid column definition:",
|
|
47
|
-
"expect a string or a literal object,",
|
|
48
|
-
`got ${JSON.stringify(column)} at position ${i}`
|
|
49
|
-
]);
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
return normalizedColumns;
|
|
53
|
-
};
|
|
54
|
-
|
|
55
|
-
// ../../node_modules/csv-parse/lib/utils/ResizeableBuffer.js
|
|
56
|
-
var ResizeableBuffer = class {
|
|
57
|
-
constructor(size = 100) {
|
|
58
|
-
this.size = size;
|
|
59
|
-
this.length = 0;
|
|
60
|
-
this.buf = Buffer.allocUnsafe(size);
|
|
61
|
-
}
|
|
62
|
-
prepend(val) {
|
|
63
|
-
if (Buffer.isBuffer(val)) {
|
|
64
|
-
const length = this.length + val.length;
|
|
65
|
-
if (length >= this.size) {
|
|
66
|
-
this.resize();
|
|
67
|
-
if (length >= this.size) {
|
|
68
|
-
throw Error("INVALID_BUFFER_STATE");
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
const buf = this.buf;
|
|
72
|
-
this.buf = Buffer.allocUnsafe(this.size);
|
|
73
|
-
val.copy(this.buf, 0);
|
|
74
|
-
buf.copy(this.buf, val.length);
|
|
75
|
-
this.length += val.length;
|
|
76
|
-
} else {
|
|
77
|
-
const length = this.length++;
|
|
78
|
-
if (length === this.size) {
|
|
79
|
-
this.resize();
|
|
80
|
-
}
|
|
81
|
-
const buf = this.clone();
|
|
82
|
-
this.buf[0] = val;
|
|
83
|
-
buf.copy(this.buf, 1, 0, length);
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
append(val) {
|
|
87
|
-
const length = this.length++;
|
|
88
|
-
if (length === this.size) {
|
|
89
|
-
this.resize();
|
|
90
|
-
}
|
|
91
|
-
this.buf[length] = val;
|
|
92
|
-
}
|
|
93
|
-
clone() {
|
|
94
|
-
return Buffer.from(this.buf.slice(0, this.length));
|
|
95
|
-
}
|
|
96
|
-
resize() {
|
|
97
|
-
const length = this.length;
|
|
98
|
-
this.size = this.size * 2;
|
|
99
|
-
const buf = Buffer.allocUnsafe(this.size);
|
|
100
|
-
this.buf.copy(buf, 0, 0, length);
|
|
101
|
-
this.buf = buf;
|
|
102
|
-
}
|
|
103
|
-
toString(encoding) {
|
|
104
|
-
if (encoding) {
|
|
105
|
-
return this.buf.slice(0, this.length).toString(encoding);
|
|
106
|
-
} else {
|
|
107
|
-
return Uint8Array.prototype.slice.call(this.buf.slice(0, this.length));
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
toJSON() {
|
|
111
|
-
return this.toString("utf8");
|
|
112
|
-
}
|
|
113
|
-
reset() {
|
|
114
|
-
this.length = 0;
|
|
115
|
-
}
|
|
116
|
-
};
|
|
117
|
-
var ResizeableBuffer_default = ResizeableBuffer;
|
|
118
|
-
|
|
119
|
-
// ../../node_modules/csv-parse/lib/api/init_state.js
|
|
120
|
-
var np = 12;
|
|
121
|
-
var cr = 13;
|
|
122
|
-
var nl = 10;
|
|
123
|
-
var space = 32;
|
|
124
|
-
var tab = 9;
|
|
125
|
-
var init_state = function(options) {
|
|
126
|
-
return {
|
|
127
|
-
bomSkipped: false,
|
|
128
|
-
bufBytesStart: 0,
|
|
129
|
-
castField: options.cast_function,
|
|
130
|
-
commenting: false,
|
|
131
|
-
// Current error encountered by a record
|
|
132
|
-
error: void 0,
|
|
133
|
-
enabled: options.from_line === 1,
|
|
134
|
-
escaping: false,
|
|
135
|
-
escapeIsQuote: Buffer.isBuffer(options.escape) && Buffer.isBuffer(options.quote) && Buffer.compare(options.escape, options.quote) === 0,
|
|
136
|
-
// columns can be `false`, `true`, `Array`
|
|
137
|
-
expectedRecordLength: Array.isArray(options.columns) ? options.columns.length : void 0,
|
|
138
|
-
field: new ResizeableBuffer_default(20),
|
|
139
|
-
firstLineToHeaders: options.cast_first_line_to_header,
|
|
140
|
-
needMoreDataSize: Math.max(
|
|
141
|
-
// Skip if the remaining buffer smaller than comment
|
|
142
|
-
options.comment !== null ? options.comment.length : 0,
|
|
143
|
-
...options.delimiter.map((delimiter) => delimiter.length),
|
|
144
|
-
// Skip if the remaining buffer can be escape sequence
|
|
145
|
-
options.quote !== null ? options.quote.length : 0
|
|
146
|
-
),
|
|
147
|
-
previousBuf: void 0,
|
|
148
|
-
quoting: false,
|
|
149
|
-
stop: false,
|
|
150
|
-
rawBuffer: new ResizeableBuffer_default(100),
|
|
151
|
-
record: [],
|
|
152
|
-
recordHasError: false,
|
|
153
|
-
record_length: 0,
|
|
154
|
-
recordDelimiterMaxLength: options.record_delimiter.length === 0 ? 0 : Math.max(...options.record_delimiter.map((v) => v.length)),
|
|
155
|
-
trimChars: [
|
|
156
|
-
Buffer.from(" ", options.encoding)[0],
|
|
157
|
-
Buffer.from(" ", options.encoding)[0]
|
|
158
|
-
],
|
|
159
|
-
wasQuoting: false,
|
|
160
|
-
wasRowDelimiter: false,
|
|
161
|
-
timchars: [
|
|
162
|
-
Buffer.from(Buffer.from([cr], "utf8").toString(), options.encoding),
|
|
163
|
-
Buffer.from(Buffer.from([nl], "utf8").toString(), options.encoding),
|
|
164
|
-
Buffer.from(Buffer.from([np], "utf8").toString(), options.encoding),
|
|
165
|
-
Buffer.from(Buffer.from([space], "utf8").toString(), options.encoding),
|
|
166
|
-
Buffer.from(Buffer.from([tab], "utf8").toString(), options.encoding)
|
|
167
|
-
]
|
|
168
|
-
};
|
|
169
|
-
};
|
|
170
|
-
|
|
171
|
-
// ../../node_modules/csv-parse/lib/utils/underscore.js
|
|
172
|
-
var underscore = function(str) {
|
|
173
|
-
return str.replace(/([A-Z])/g, function(_, match) {
|
|
174
|
-
return "_" + match.toLowerCase();
|
|
175
|
-
});
|
|
176
|
-
};
|
|
177
|
-
|
|
178
|
-
// ../../node_modules/csv-parse/lib/api/normalize_options.js
|
|
179
|
-
var normalize_options = function(opts) {
|
|
180
|
-
const options = {};
|
|
181
|
-
for (const opt in opts) {
|
|
182
|
-
options[underscore(opt)] = opts[opt];
|
|
183
|
-
}
|
|
184
|
-
if (options.encoding === void 0 || options.encoding === true) {
|
|
185
|
-
options.encoding = "utf8";
|
|
186
|
-
} else if (options.encoding === null || options.encoding === false) {
|
|
187
|
-
options.encoding = null;
|
|
188
|
-
} else if (typeof options.encoding !== "string" && options.encoding !== null) {
|
|
189
|
-
throw new CsvError(
|
|
190
|
-
"CSV_INVALID_OPTION_ENCODING",
|
|
191
|
-
[
|
|
192
|
-
"Invalid option encoding:",
|
|
193
|
-
"encoding must be a string or null to return a buffer,",
|
|
194
|
-
`got ${JSON.stringify(options.encoding)}`
|
|
195
|
-
],
|
|
196
|
-
options
|
|
197
|
-
);
|
|
198
|
-
}
|
|
199
|
-
if (options.bom === void 0 || options.bom === null || options.bom === false) {
|
|
200
|
-
options.bom = false;
|
|
201
|
-
} else if (options.bom !== true) {
|
|
202
|
-
throw new CsvError(
|
|
203
|
-
"CSV_INVALID_OPTION_BOM",
|
|
204
|
-
[
|
|
205
|
-
"Invalid option bom:",
|
|
206
|
-
"bom must be true,",
|
|
207
|
-
`got ${JSON.stringify(options.bom)}`
|
|
208
|
-
],
|
|
209
|
-
options
|
|
210
|
-
);
|
|
211
|
-
}
|
|
212
|
-
options.cast_function = null;
|
|
213
|
-
if (options.cast === void 0 || options.cast === null || options.cast === false || options.cast === "") {
|
|
214
|
-
options.cast = void 0;
|
|
215
|
-
} else if (typeof options.cast === "function") {
|
|
216
|
-
options.cast_function = options.cast;
|
|
217
|
-
options.cast = true;
|
|
218
|
-
} else if (options.cast !== true) {
|
|
219
|
-
throw new CsvError(
|
|
220
|
-
"CSV_INVALID_OPTION_CAST",
|
|
221
|
-
[
|
|
222
|
-
"Invalid option cast:",
|
|
223
|
-
"cast must be true or a function,",
|
|
224
|
-
`got ${JSON.stringify(options.cast)}`
|
|
225
|
-
],
|
|
226
|
-
options
|
|
227
|
-
);
|
|
228
|
-
}
|
|
229
|
-
if (options.cast_date === void 0 || options.cast_date === null || options.cast_date === false || options.cast_date === "") {
|
|
230
|
-
options.cast_date = false;
|
|
231
|
-
} else if (options.cast_date === true) {
|
|
232
|
-
options.cast_date = function(value) {
|
|
233
|
-
const date = Date.parse(value);
|
|
234
|
-
return !isNaN(date) ? new Date(date) : value;
|
|
235
|
-
};
|
|
236
|
-
} else if (typeof options.cast_date !== "function") {
|
|
237
|
-
throw new CsvError(
|
|
238
|
-
"CSV_INVALID_OPTION_CAST_DATE",
|
|
239
|
-
[
|
|
240
|
-
"Invalid option cast_date:",
|
|
241
|
-
"cast_date must be true or a function,",
|
|
242
|
-
`got ${JSON.stringify(options.cast_date)}`
|
|
243
|
-
],
|
|
244
|
-
options
|
|
245
|
-
);
|
|
246
|
-
}
|
|
247
|
-
options.cast_first_line_to_header = null;
|
|
248
|
-
if (options.columns === true) {
|
|
249
|
-
options.cast_first_line_to_header = void 0;
|
|
250
|
-
} else if (typeof options.columns === "function") {
|
|
251
|
-
options.cast_first_line_to_header = options.columns;
|
|
252
|
-
options.columns = true;
|
|
253
|
-
} else if (Array.isArray(options.columns)) {
|
|
254
|
-
options.columns = normalize_columns_array(options.columns);
|
|
255
|
-
} else if (options.columns === void 0 || options.columns === null || options.columns === false) {
|
|
256
|
-
options.columns = false;
|
|
257
|
-
} else {
|
|
258
|
-
throw new CsvError(
|
|
259
|
-
"CSV_INVALID_OPTION_COLUMNS",
|
|
260
|
-
[
|
|
261
|
-
"Invalid option columns:",
|
|
262
|
-
"expect an array, a function or true,",
|
|
263
|
-
`got ${JSON.stringify(options.columns)}`
|
|
264
|
-
],
|
|
265
|
-
options
|
|
266
|
-
);
|
|
267
|
-
}
|
|
268
|
-
if (options.group_columns_by_name === void 0 || options.group_columns_by_name === null || options.group_columns_by_name === false) {
|
|
269
|
-
options.group_columns_by_name = false;
|
|
270
|
-
} else if (options.group_columns_by_name !== true) {
|
|
271
|
-
throw new CsvError(
|
|
272
|
-
"CSV_INVALID_OPTION_GROUP_COLUMNS_BY_NAME",
|
|
273
|
-
[
|
|
274
|
-
"Invalid option group_columns_by_name:",
|
|
275
|
-
"expect an boolean,",
|
|
276
|
-
`got ${JSON.stringify(options.group_columns_by_name)}`
|
|
277
|
-
],
|
|
278
|
-
options
|
|
279
|
-
);
|
|
280
|
-
} else if (options.columns === false) {
|
|
281
|
-
throw new CsvError(
|
|
282
|
-
"CSV_INVALID_OPTION_GROUP_COLUMNS_BY_NAME",
|
|
283
|
-
[
|
|
284
|
-
"Invalid option group_columns_by_name:",
|
|
285
|
-
"the `columns` mode must be activated."
|
|
286
|
-
],
|
|
287
|
-
options
|
|
288
|
-
);
|
|
289
|
-
}
|
|
290
|
-
if (options.comment === void 0 || options.comment === null || options.comment === false || options.comment === "") {
|
|
291
|
-
options.comment = null;
|
|
292
|
-
} else {
|
|
293
|
-
if (typeof options.comment === "string") {
|
|
294
|
-
options.comment = Buffer.from(options.comment, options.encoding);
|
|
295
|
-
}
|
|
296
|
-
if (!Buffer.isBuffer(options.comment)) {
|
|
297
|
-
throw new CsvError(
|
|
298
|
-
"CSV_INVALID_OPTION_COMMENT",
|
|
299
|
-
[
|
|
300
|
-
"Invalid option comment:",
|
|
301
|
-
"comment must be a buffer or a string,",
|
|
302
|
-
`got ${JSON.stringify(options.comment)}`
|
|
303
|
-
],
|
|
304
|
-
options
|
|
305
|
-
);
|
|
306
|
-
}
|
|
307
|
-
}
|
|
308
|
-
if (options.comment_no_infix === void 0 || options.comment_no_infix === null || options.comment_no_infix === false) {
|
|
309
|
-
options.comment_no_infix = false;
|
|
310
|
-
} else if (options.comment_no_infix !== true) {
|
|
311
|
-
throw new CsvError(
|
|
312
|
-
"CSV_INVALID_OPTION_COMMENT",
|
|
313
|
-
[
|
|
314
|
-
"Invalid option comment_no_infix:",
|
|
315
|
-
"value must be a boolean,",
|
|
316
|
-
`got ${JSON.stringify(options.comment_no_infix)}`
|
|
317
|
-
],
|
|
318
|
-
options
|
|
319
|
-
);
|
|
320
|
-
}
|
|
321
|
-
const delimiter_json = JSON.stringify(options.delimiter);
|
|
322
|
-
if (!Array.isArray(options.delimiter))
|
|
323
|
-
options.delimiter = [options.delimiter];
|
|
324
|
-
if (options.delimiter.length === 0) {
|
|
325
|
-
throw new CsvError(
|
|
326
|
-
"CSV_INVALID_OPTION_DELIMITER",
|
|
327
|
-
[
|
|
328
|
-
"Invalid option delimiter:",
|
|
329
|
-
"delimiter must be a non empty string or buffer or array of string|buffer,",
|
|
330
|
-
`got ${delimiter_json}`
|
|
331
|
-
],
|
|
332
|
-
options
|
|
333
|
-
);
|
|
334
|
-
}
|
|
335
|
-
options.delimiter = options.delimiter.map(function(delimiter) {
|
|
336
|
-
if (delimiter === void 0 || delimiter === null || delimiter === false) {
|
|
337
|
-
return Buffer.from(",", options.encoding);
|
|
338
|
-
}
|
|
339
|
-
if (typeof delimiter === "string") {
|
|
340
|
-
delimiter = Buffer.from(delimiter, options.encoding);
|
|
341
|
-
}
|
|
342
|
-
if (!Buffer.isBuffer(delimiter) || delimiter.length === 0) {
|
|
343
|
-
throw new CsvError(
|
|
344
|
-
"CSV_INVALID_OPTION_DELIMITER",
|
|
345
|
-
[
|
|
346
|
-
"Invalid option delimiter:",
|
|
347
|
-
"delimiter must be a non empty string or buffer or array of string|buffer,",
|
|
348
|
-
`got ${delimiter_json}`
|
|
349
|
-
],
|
|
350
|
-
options
|
|
351
|
-
);
|
|
352
|
-
}
|
|
353
|
-
return delimiter;
|
|
354
|
-
});
|
|
355
|
-
if (options.escape === void 0 || options.escape === true) {
|
|
356
|
-
options.escape = Buffer.from('"', options.encoding);
|
|
357
|
-
} else if (typeof options.escape === "string") {
|
|
358
|
-
options.escape = Buffer.from(options.escape, options.encoding);
|
|
359
|
-
} else if (options.escape === null || options.escape === false) {
|
|
360
|
-
options.escape = null;
|
|
361
|
-
}
|
|
362
|
-
if (options.escape !== null) {
|
|
363
|
-
if (!Buffer.isBuffer(options.escape)) {
|
|
364
|
-
throw new Error(
|
|
365
|
-
`Invalid Option: escape must be a buffer, a string or a boolean, got ${JSON.stringify(options.escape)}`
|
|
366
|
-
);
|
|
367
|
-
}
|
|
368
|
-
}
|
|
369
|
-
if (options.from === void 0 || options.from === null) {
|
|
370
|
-
options.from = 1;
|
|
371
|
-
} else {
|
|
372
|
-
if (typeof options.from === "string" && /\d+/.test(options.from)) {
|
|
373
|
-
options.from = parseInt(options.from);
|
|
374
|
-
}
|
|
375
|
-
if (Number.isInteger(options.from)) {
|
|
376
|
-
if (options.from < 0) {
|
|
377
|
-
throw new Error(
|
|
378
|
-
`Invalid Option: from must be a positive integer, got ${JSON.stringify(opts.from)}`
|
|
379
|
-
);
|
|
380
|
-
}
|
|
381
|
-
} else {
|
|
382
|
-
throw new Error(
|
|
383
|
-
`Invalid Option: from must be an integer, got ${JSON.stringify(options.from)}`
|
|
384
|
-
);
|
|
385
|
-
}
|
|
386
|
-
}
|
|
387
|
-
if (options.from_line === void 0 || options.from_line === null) {
|
|
388
|
-
options.from_line = 1;
|
|
389
|
-
} else {
|
|
390
|
-
if (typeof options.from_line === "string" && /\d+/.test(options.from_line)) {
|
|
391
|
-
options.from_line = parseInt(options.from_line);
|
|
392
|
-
}
|
|
393
|
-
if (Number.isInteger(options.from_line)) {
|
|
394
|
-
if (options.from_line <= 0) {
|
|
395
|
-
throw new Error(
|
|
396
|
-
`Invalid Option: from_line must be a positive integer greater than 0, got ${JSON.stringify(opts.from_line)}`
|
|
397
|
-
);
|
|
398
|
-
}
|
|
399
|
-
} else {
|
|
400
|
-
throw new Error(
|
|
401
|
-
`Invalid Option: from_line must be an integer, got ${JSON.stringify(opts.from_line)}`
|
|
402
|
-
);
|
|
403
|
-
}
|
|
404
|
-
}
|
|
405
|
-
if (options.ignore_last_delimiters === void 0 || options.ignore_last_delimiters === null) {
|
|
406
|
-
options.ignore_last_delimiters = false;
|
|
407
|
-
} else if (typeof options.ignore_last_delimiters === "number") {
|
|
408
|
-
options.ignore_last_delimiters = Math.floor(options.ignore_last_delimiters);
|
|
409
|
-
if (options.ignore_last_delimiters === 0) {
|
|
410
|
-
options.ignore_last_delimiters = false;
|
|
411
|
-
}
|
|
412
|
-
} else if (typeof options.ignore_last_delimiters !== "boolean") {
|
|
413
|
-
throw new CsvError(
|
|
414
|
-
"CSV_INVALID_OPTION_IGNORE_LAST_DELIMITERS",
|
|
415
|
-
[
|
|
416
|
-
"Invalid option `ignore_last_delimiters`:",
|
|
417
|
-
"the value must be a boolean value or an integer,",
|
|
418
|
-
`got ${JSON.stringify(options.ignore_last_delimiters)}`
|
|
419
|
-
],
|
|
420
|
-
options
|
|
421
|
-
);
|
|
422
|
-
}
|
|
423
|
-
if (options.ignore_last_delimiters === true && options.columns === false) {
|
|
424
|
-
throw new CsvError(
|
|
425
|
-
"CSV_IGNORE_LAST_DELIMITERS_REQUIRES_COLUMNS",
|
|
426
|
-
[
|
|
427
|
-
"The option `ignore_last_delimiters`",
|
|
428
|
-
"requires the activation of the `columns` option"
|
|
429
|
-
],
|
|
430
|
-
options
|
|
431
|
-
);
|
|
432
|
-
}
|
|
433
|
-
if (options.info === void 0 || options.info === null || options.info === false) {
|
|
434
|
-
options.info = false;
|
|
435
|
-
} else if (options.info !== true) {
|
|
436
|
-
throw new Error(
|
|
437
|
-
`Invalid Option: info must be true, got ${JSON.stringify(options.info)}`
|
|
438
|
-
);
|
|
439
|
-
}
|
|
440
|
-
if (options.max_record_size === void 0 || options.max_record_size === null || options.max_record_size === false) {
|
|
441
|
-
options.max_record_size = 0;
|
|
442
|
-
} else if (Number.isInteger(options.max_record_size) && options.max_record_size >= 0) {
|
|
443
|
-
} else if (typeof options.max_record_size === "string" && /\d+/.test(options.max_record_size)) {
|
|
444
|
-
options.max_record_size = parseInt(options.max_record_size);
|
|
445
|
-
} else {
|
|
446
|
-
throw new Error(
|
|
447
|
-
`Invalid Option: max_record_size must be a positive integer, got ${JSON.stringify(options.max_record_size)}`
|
|
448
|
-
);
|
|
449
|
-
}
|
|
450
|
-
if (options.objname === void 0 || options.objname === null || options.objname === false) {
|
|
451
|
-
options.objname = void 0;
|
|
452
|
-
} else if (Buffer.isBuffer(options.objname)) {
|
|
453
|
-
if (options.objname.length === 0) {
|
|
454
|
-
throw new Error(`Invalid Option: objname must be a non empty buffer`);
|
|
455
|
-
}
|
|
456
|
-
if (options.encoding === null) {
|
|
457
|
-
} else {
|
|
458
|
-
options.objname = options.objname.toString(options.encoding);
|
|
459
|
-
}
|
|
460
|
-
} else if (typeof options.objname === "string") {
|
|
461
|
-
if (options.objname.length === 0) {
|
|
462
|
-
throw new Error(`Invalid Option: objname must be a non empty string`);
|
|
463
|
-
}
|
|
464
|
-
} else if (typeof options.objname === "number") {
|
|
465
|
-
} else {
|
|
466
|
-
throw new Error(
|
|
467
|
-
`Invalid Option: objname must be a string or a buffer, got ${options.objname}`
|
|
468
|
-
);
|
|
469
|
-
}
|
|
470
|
-
if (options.objname !== void 0) {
|
|
471
|
-
if (typeof options.objname === "number") {
|
|
472
|
-
if (options.columns !== false) {
|
|
473
|
-
throw Error(
|
|
474
|
-
"Invalid Option: objname index cannot be combined with columns or be defined as a field"
|
|
475
|
-
);
|
|
476
|
-
}
|
|
477
|
-
} else {
|
|
478
|
-
if (options.columns === false) {
|
|
479
|
-
throw Error(
|
|
480
|
-
"Invalid Option: objname field must be combined with columns or be defined as an index"
|
|
481
|
-
);
|
|
482
|
-
}
|
|
483
|
-
}
|
|
484
|
-
}
|
|
485
|
-
if (options.on_record === void 0 || options.on_record === null) {
|
|
486
|
-
options.on_record = void 0;
|
|
487
|
-
} else if (typeof options.on_record !== "function") {
|
|
488
|
-
throw new CsvError(
|
|
489
|
-
"CSV_INVALID_OPTION_ON_RECORD",
|
|
490
|
-
[
|
|
491
|
-
"Invalid option `on_record`:",
|
|
492
|
-
"expect a function,",
|
|
493
|
-
`got ${JSON.stringify(options.on_record)}`
|
|
494
|
-
],
|
|
495
|
-
options
|
|
496
|
-
);
|
|
497
|
-
}
|
|
498
|
-
if (options.on_skip !== void 0 && options.on_skip !== null && typeof options.on_skip !== "function") {
|
|
499
|
-
throw new Error(
|
|
500
|
-
`Invalid Option: on_skip must be a function, got ${JSON.stringify(options.on_skip)}`
|
|
501
|
-
);
|
|
502
|
-
}
|
|
503
|
-
if (options.quote === null || options.quote === false || options.quote === "") {
|
|
504
|
-
options.quote = null;
|
|
505
|
-
} else {
|
|
506
|
-
if (options.quote === void 0 || options.quote === true) {
|
|
507
|
-
options.quote = Buffer.from('"', options.encoding);
|
|
508
|
-
} else if (typeof options.quote === "string") {
|
|
509
|
-
options.quote = Buffer.from(options.quote, options.encoding);
|
|
510
|
-
}
|
|
511
|
-
if (!Buffer.isBuffer(options.quote)) {
|
|
512
|
-
throw new Error(
|
|
513
|
-
`Invalid Option: quote must be a buffer or a string, got ${JSON.stringify(options.quote)}`
|
|
514
|
-
);
|
|
515
|
-
}
|
|
516
|
-
}
|
|
517
|
-
if (options.raw === void 0 || options.raw === null || options.raw === false) {
|
|
518
|
-
options.raw = false;
|
|
519
|
-
} else if (options.raw !== true) {
|
|
520
|
-
throw new Error(
|
|
521
|
-
`Invalid Option: raw must be true, got ${JSON.stringify(options.raw)}`
|
|
522
|
-
);
|
|
523
|
-
}
|
|
524
|
-
if (options.record_delimiter === void 0) {
|
|
525
|
-
options.record_delimiter = [];
|
|
526
|
-
} else if (typeof options.record_delimiter === "string" || Buffer.isBuffer(options.record_delimiter)) {
|
|
527
|
-
if (options.record_delimiter.length === 0) {
|
|
528
|
-
throw new CsvError(
|
|
529
|
-
"CSV_INVALID_OPTION_RECORD_DELIMITER",
|
|
530
|
-
[
|
|
531
|
-
"Invalid option `record_delimiter`:",
|
|
532
|
-
"value must be a non empty string or buffer,",
|
|
533
|
-
`got ${JSON.stringify(options.record_delimiter)}`
|
|
534
|
-
],
|
|
535
|
-
options
|
|
536
|
-
);
|
|
537
|
-
}
|
|
538
|
-
options.record_delimiter = [options.record_delimiter];
|
|
539
|
-
} else if (!Array.isArray(options.record_delimiter)) {
|
|
540
|
-
throw new CsvError(
|
|
541
|
-
"CSV_INVALID_OPTION_RECORD_DELIMITER",
|
|
542
|
-
[
|
|
543
|
-
"Invalid option `record_delimiter`:",
|
|
544
|
-
"value must be a string, a buffer or array of string|buffer,",
|
|
545
|
-
`got ${JSON.stringify(options.record_delimiter)}`
|
|
546
|
-
],
|
|
547
|
-
options
|
|
548
|
-
);
|
|
549
|
-
}
|
|
550
|
-
options.record_delimiter = options.record_delimiter.map(function(rd, i) {
|
|
551
|
-
if (typeof rd !== "string" && !Buffer.isBuffer(rd)) {
|
|
552
|
-
throw new CsvError(
|
|
553
|
-
"CSV_INVALID_OPTION_RECORD_DELIMITER",
|
|
554
|
-
[
|
|
555
|
-
"Invalid option `record_delimiter`:",
|
|
556
|
-
"value must be a string, a buffer or array of string|buffer",
|
|
557
|
-
`at index ${i},`,
|
|
558
|
-
`got ${JSON.stringify(rd)}`
|
|
559
|
-
],
|
|
560
|
-
options
|
|
561
|
-
);
|
|
562
|
-
} else if (rd.length === 0) {
|
|
563
|
-
throw new CsvError(
|
|
564
|
-
"CSV_INVALID_OPTION_RECORD_DELIMITER",
|
|
565
|
-
[
|
|
566
|
-
"Invalid option `record_delimiter`:",
|
|
567
|
-
"value must be a non empty string or buffer",
|
|
568
|
-
`at index ${i},`,
|
|
569
|
-
`got ${JSON.stringify(rd)}`
|
|
570
|
-
],
|
|
571
|
-
options
|
|
572
|
-
);
|
|
573
|
-
}
|
|
574
|
-
if (typeof rd === "string") {
|
|
575
|
-
rd = Buffer.from(rd, options.encoding);
|
|
576
|
-
}
|
|
577
|
-
return rd;
|
|
578
|
-
});
|
|
579
|
-
if (typeof options.relax_column_count === "boolean") {
|
|
580
|
-
} else if (options.relax_column_count === void 0 || options.relax_column_count === null) {
|
|
581
|
-
options.relax_column_count = false;
|
|
582
|
-
} else {
|
|
583
|
-
throw new Error(
|
|
584
|
-
`Invalid Option: relax_column_count must be a boolean, got ${JSON.stringify(options.relax_column_count)}`
|
|
585
|
-
);
|
|
586
|
-
}
|
|
587
|
-
if (typeof options.relax_column_count_less === "boolean") {
|
|
588
|
-
} else if (options.relax_column_count_less === void 0 || options.relax_column_count_less === null) {
|
|
589
|
-
options.relax_column_count_less = false;
|
|
590
|
-
} else {
|
|
591
|
-
throw new Error(
|
|
592
|
-
`Invalid Option: relax_column_count_less must be a boolean, got ${JSON.stringify(options.relax_column_count_less)}`
|
|
593
|
-
);
|
|
594
|
-
}
|
|
595
|
-
if (typeof options.relax_column_count_more === "boolean") {
|
|
596
|
-
} else if (options.relax_column_count_more === void 0 || options.relax_column_count_more === null) {
|
|
597
|
-
options.relax_column_count_more = false;
|
|
598
|
-
} else {
|
|
599
|
-
throw new Error(
|
|
600
|
-
`Invalid Option: relax_column_count_more must be a boolean, got ${JSON.stringify(options.relax_column_count_more)}`
|
|
601
|
-
);
|
|
602
|
-
}
|
|
603
|
-
if (typeof options.relax_quotes === "boolean") {
|
|
604
|
-
} else if (options.relax_quotes === void 0 || options.relax_quotes === null) {
|
|
605
|
-
options.relax_quotes = false;
|
|
606
|
-
} else {
|
|
607
|
-
throw new Error(
|
|
608
|
-
`Invalid Option: relax_quotes must be a boolean, got ${JSON.stringify(options.relax_quotes)}`
|
|
609
|
-
);
|
|
610
|
-
}
|
|
611
|
-
if (typeof options.skip_empty_lines === "boolean") {
|
|
612
|
-
} else if (options.skip_empty_lines === void 0 || options.skip_empty_lines === null) {
|
|
613
|
-
options.skip_empty_lines = false;
|
|
614
|
-
} else {
|
|
615
|
-
throw new Error(
|
|
616
|
-
`Invalid Option: skip_empty_lines must be a boolean, got ${JSON.stringify(options.skip_empty_lines)}`
|
|
617
|
-
);
|
|
618
|
-
}
|
|
619
|
-
if (typeof options.skip_records_with_empty_values === "boolean") {
|
|
620
|
-
} else if (options.skip_records_with_empty_values === void 0 || options.skip_records_with_empty_values === null) {
|
|
621
|
-
options.skip_records_with_empty_values = false;
|
|
622
|
-
} else {
|
|
623
|
-
throw new Error(
|
|
624
|
-
`Invalid Option: skip_records_with_empty_values must be a boolean, got ${JSON.stringify(options.skip_records_with_empty_values)}`
|
|
625
|
-
);
|
|
626
|
-
}
|
|
627
|
-
if (typeof options.skip_records_with_error === "boolean") {
|
|
628
|
-
} else if (options.skip_records_with_error === void 0 || options.skip_records_with_error === null) {
|
|
629
|
-
options.skip_records_with_error = false;
|
|
630
|
-
} else {
|
|
631
|
-
throw new Error(
|
|
632
|
-
`Invalid Option: skip_records_with_error must be a boolean, got ${JSON.stringify(options.skip_records_with_error)}`
|
|
633
|
-
);
|
|
634
|
-
}
|
|
635
|
-
if (options.rtrim === void 0 || options.rtrim === null || options.rtrim === false) {
|
|
636
|
-
options.rtrim = false;
|
|
637
|
-
} else if (options.rtrim !== true) {
|
|
638
|
-
throw new Error(
|
|
639
|
-
`Invalid Option: rtrim must be a boolean, got ${JSON.stringify(options.rtrim)}`
|
|
640
|
-
);
|
|
641
|
-
}
|
|
642
|
-
if (options.ltrim === void 0 || options.ltrim === null || options.ltrim === false) {
|
|
643
|
-
options.ltrim = false;
|
|
644
|
-
} else if (options.ltrim !== true) {
|
|
645
|
-
throw new Error(
|
|
646
|
-
`Invalid Option: ltrim must be a boolean, got ${JSON.stringify(options.ltrim)}`
|
|
647
|
-
);
|
|
648
|
-
}
|
|
649
|
-
if (options.trim === void 0 || options.trim === null || options.trim === false) {
|
|
650
|
-
options.trim = false;
|
|
651
|
-
} else if (options.trim !== true) {
|
|
652
|
-
throw new Error(
|
|
653
|
-
`Invalid Option: trim must be a boolean, got ${JSON.stringify(options.trim)}`
|
|
654
|
-
);
|
|
655
|
-
}
|
|
656
|
-
if (options.trim === true && opts.ltrim !== false) {
|
|
657
|
-
options.ltrim = true;
|
|
658
|
-
} else if (options.ltrim !== true) {
|
|
659
|
-
options.ltrim = false;
|
|
660
|
-
}
|
|
661
|
-
if (options.trim === true && opts.rtrim !== false) {
|
|
662
|
-
options.rtrim = true;
|
|
663
|
-
} else if (options.rtrim !== true) {
|
|
664
|
-
options.rtrim = false;
|
|
665
|
-
}
|
|
666
|
-
if (options.to === void 0 || options.to === null) {
|
|
667
|
-
options.to = -1;
|
|
668
|
-
} else {
|
|
669
|
-
if (typeof options.to === "string" && /\d+/.test(options.to)) {
|
|
670
|
-
options.to = parseInt(options.to);
|
|
671
|
-
}
|
|
672
|
-
if (Number.isInteger(options.to)) {
|
|
673
|
-
if (options.to <= 0) {
|
|
674
|
-
throw new Error(
|
|
675
|
-
`Invalid Option: to must be a positive integer greater than 0, got ${JSON.stringify(opts.to)}`
|
|
676
|
-
);
|
|
677
|
-
}
|
|
678
|
-
} else {
|
|
679
|
-
throw new Error(
|
|
680
|
-
`Invalid Option: to must be an integer, got ${JSON.stringify(opts.to)}`
|
|
681
|
-
);
|
|
682
|
-
}
|
|
683
|
-
}
|
|
684
|
-
if (options.to_line === void 0 || options.to_line === null) {
|
|
685
|
-
options.to_line = -1;
|
|
686
|
-
} else {
|
|
687
|
-
if (typeof options.to_line === "string" && /\d+/.test(options.to_line)) {
|
|
688
|
-
options.to_line = parseInt(options.to_line);
|
|
689
|
-
}
|
|
690
|
-
if (Number.isInteger(options.to_line)) {
|
|
691
|
-
if (options.to_line <= 0) {
|
|
692
|
-
throw new Error(
|
|
693
|
-
`Invalid Option: to_line must be a positive integer greater than 0, got ${JSON.stringify(opts.to_line)}`
|
|
694
|
-
);
|
|
695
|
-
}
|
|
696
|
-
} else {
|
|
697
|
-
throw new Error(
|
|
698
|
-
`Invalid Option: to_line must be an integer, got ${JSON.stringify(opts.to_line)}`
|
|
699
|
-
);
|
|
700
|
-
}
|
|
701
|
-
}
|
|
702
|
-
return options;
|
|
703
|
-
};
|
|
704
|
-
|
|
705
|
-
// ../../node_modules/csv-parse/lib/api/index.js
|
|
706
|
-
var isRecordEmpty = function(record) {
|
|
707
|
-
return record.every(
|
|
708
|
-
(field) => field == null || field.toString && field.toString().trim() === ""
|
|
709
|
-
);
|
|
710
|
-
};
|
|
711
|
-
var cr2 = 13;
|
|
712
|
-
var nl2 = 10;
|
|
713
|
-
var boms = {
|
|
714
|
-
// Note, the following are equals:
|
|
715
|
-
// Buffer.from("\ufeff")
|
|
716
|
-
// Buffer.from([239, 187, 191])
|
|
717
|
-
// Buffer.from('EFBBBF', 'hex')
|
|
718
|
-
utf8: Buffer.from([239, 187, 191]),
|
|
719
|
-
// Note, the following are equals:
|
|
720
|
-
// Buffer.from "\ufeff", 'utf16le
|
|
721
|
-
// Buffer.from([255, 254])
|
|
722
|
-
utf16le: Buffer.from([255, 254])
|
|
723
|
-
};
|
|
724
|
-
var transform = function(original_options = {}) {
|
|
725
|
-
const info = {
|
|
726
|
-
bytes: 0,
|
|
727
|
-
comment_lines: 0,
|
|
728
|
-
empty_lines: 0,
|
|
729
|
-
invalid_field_length: 0,
|
|
730
|
-
lines: 1,
|
|
731
|
-
records: 0
|
|
732
|
-
};
|
|
733
|
-
const options = normalize_options(original_options);
|
|
734
|
-
return {
|
|
735
|
-
info,
|
|
736
|
-
original_options,
|
|
737
|
-
options,
|
|
738
|
-
state: init_state(options),
|
|
739
|
-
__needMoreData: function(i, bufLen, end) {
|
|
740
|
-
if (end) return false;
|
|
741
|
-
const { encoding, escape, quote } = this.options;
|
|
742
|
-
const { quoting, needMoreDataSize, recordDelimiterMaxLength } = this.state;
|
|
743
|
-
const numOfCharLeft = bufLen - i - 1;
|
|
744
|
-
const requiredLength = Math.max(
|
|
745
|
-
needMoreDataSize,
|
|
746
|
-
// Skip if the remaining buffer smaller than record delimiter
|
|
747
|
-
// If "record_delimiter" is yet to be discovered:
|
|
748
|
-
// 1. It is equals to `[]` and "recordDelimiterMaxLength" equals `0`
|
|
749
|
-
// 2. We set the length to windows line ending in the current encoding
|
|
750
|
-
// Note, that encoding is known from user or bom discovery at that point
|
|
751
|
-
// recordDelimiterMaxLength,
|
|
752
|
-
recordDelimiterMaxLength === 0 ? Buffer.from("\r\n", encoding).length : recordDelimiterMaxLength,
|
|
753
|
-
// Skip if remaining buffer can be an escaped quote
|
|
754
|
-
quoting ? (escape === null ? 0 : escape.length) + quote.length : 0,
|
|
755
|
-
// Skip if remaining buffer can be record delimiter following the closing quote
|
|
756
|
-
quoting ? quote.length + recordDelimiterMaxLength : 0
|
|
757
|
-
);
|
|
758
|
-
return numOfCharLeft < requiredLength;
|
|
759
|
-
},
|
|
760
|
-
// Central parser implementation
|
|
761
|
-
parse: function(nextBuf, end, push, close) {
|
|
762
|
-
const {
|
|
763
|
-
bom,
|
|
764
|
-
comment_no_infix,
|
|
765
|
-
encoding,
|
|
766
|
-
from_line,
|
|
767
|
-
ltrim,
|
|
768
|
-
max_record_size,
|
|
769
|
-
raw,
|
|
770
|
-
relax_quotes,
|
|
771
|
-
rtrim,
|
|
772
|
-
skip_empty_lines,
|
|
773
|
-
to,
|
|
774
|
-
to_line
|
|
775
|
-
} = this.options;
|
|
776
|
-
let { comment, escape, quote, record_delimiter } = this.options;
|
|
777
|
-
const { bomSkipped, previousBuf, rawBuffer, escapeIsQuote } = this.state;
|
|
778
|
-
let buf;
|
|
779
|
-
if (previousBuf === void 0) {
|
|
780
|
-
if (nextBuf === void 0) {
|
|
781
|
-
close();
|
|
782
|
-
return;
|
|
783
|
-
} else {
|
|
784
|
-
buf = nextBuf;
|
|
785
|
-
}
|
|
786
|
-
} else if (previousBuf !== void 0 && nextBuf === void 0) {
|
|
787
|
-
buf = previousBuf;
|
|
788
|
-
} else {
|
|
789
|
-
buf = Buffer.concat([previousBuf, nextBuf]);
|
|
790
|
-
}
|
|
791
|
-
if (bomSkipped === false) {
|
|
792
|
-
if (bom === false) {
|
|
793
|
-
this.state.bomSkipped = true;
|
|
794
|
-
} else if (buf.length < 3) {
|
|
795
|
-
if (end === false) {
|
|
796
|
-
this.state.previousBuf = buf;
|
|
797
|
-
return;
|
|
798
|
-
}
|
|
799
|
-
} else {
|
|
800
|
-
for (const encoding2 in boms) {
|
|
801
|
-
if (boms[encoding2].compare(buf, 0, boms[encoding2].length) === 0) {
|
|
802
|
-
const bomLength = boms[encoding2].length;
|
|
803
|
-
this.state.bufBytesStart += bomLength;
|
|
804
|
-
buf = buf.slice(bomLength);
|
|
805
|
-
this.options = normalize_options({
|
|
806
|
-
...this.original_options,
|
|
807
|
-
encoding: encoding2
|
|
808
|
-
});
|
|
809
|
-
({ comment, escape, quote } = this.options);
|
|
810
|
-
break;
|
|
811
|
-
}
|
|
812
|
-
}
|
|
813
|
-
this.state.bomSkipped = true;
|
|
814
|
-
}
|
|
815
|
-
}
|
|
816
|
-
const bufLen = buf.length;
|
|
817
|
-
let pos;
|
|
818
|
-
for (pos = 0; pos < bufLen; pos++) {
|
|
819
|
-
if (this.__needMoreData(pos, bufLen, end)) {
|
|
820
|
-
break;
|
|
821
|
-
}
|
|
822
|
-
if (this.state.wasRowDelimiter === true) {
|
|
823
|
-
this.info.lines++;
|
|
824
|
-
this.state.wasRowDelimiter = false;
|
|
825
|
-
}
|
|
826
|
-
if (to_line !== -1 && this.info.lines > to_line) {
|
|
827
|
-
this.state.stop = true;
|
|
828
|
-
close();
|
|
829
|
-
return;
|
|
830
|
-
}
|
|
831
|
-
if (this.state.quoting === false && record_delimiter.length === 0) {
|
|
832
|
-
const record_delimiterCount = this.__autoDiscoverRecordDelimiter(
|
|
833
|
-
buf,
|
|
834
|
-
pos
|
|
835
|
-
);
|
|
836
|
-
if (record_delimiterCount) {
|
|
837
|
-
record_delimiter = this.options.record_delimiter;
|
|
838
|
-
}
|
|
839
|
-
}
|
|
840
|
-
const chr = buf[pos];
|
|
841
|
-
if (raw === true) {
|
|
842
|
-
rawBuffer.append(chr);
|
|
843
|
-
}
|
|
844
|
-
if ((chr === cr2 || chr === nl2) && this.state.wasRowDelimiter === false) {
|
|
845
|
-
this.state.wasRowDelimiter = true;
|
|
846
|
-
}
|
|
847
|
-
if (this.state.escaping === true) {
|
|
848
|
-
this.state.escaping = false;
|
|
849
|
-
} else {
|
|
850
|
-
if (escape !== null && this.state.quoting === true && this.__isEscape(buf, pos, chr) && pos + escape.length < bufLen) {
|
|
851
|
-
if (escapeIsQuote) {
|
|
852
|
-
if (this.__isQuote(buf, pos + escape.length)) {
|
|
853
|
-
this.state.escaping = true;
|
|
854
|
-
pos += escape.length - 1;
|
|
855
|
-
continue;
|
|
856
|
-
}
|
|
857
|
-
} else {
|
|
858
|
-
this.state.escaping = true;
|
|
859
|
-
pos += escape.length - 1;
|
|
860
|
-
continue;
|
|
861
|
-
}
|
|
862
|
-
}
|
|
863
|
-
if (this.state.commenting === false && this.__isQuote(buf, pos)) {
|
|
864
|
-
if (this.state.quoting === true) {
|
|
865
|
-
const nextChr = buf[pos + quote.length];
|
|
866
|
-
const isNextChrTrimable = rtrim && this.__isCharTrimable(buf, pos + quote.length);
|
|
867
|
-
const isNextChrComment = comment !== null && this.__compareBytes(comment, buf, pos + quote.length, nextChr);
|
|
868
|
-
const isNextChrDelimiter = this.__isDelimiter(
|
|
869
|
-
buf,
|
|
870
|
-
pos + quote.length,
|
|
871
|
-
nextChr
|
|
872
|
-
);
|
|
873
|
-
const isNextChrRecordDelimiter = record_delimiter.length === 0 ? this.__autoDiscoverRecordDelimiter(buf, pos + quote.length) : this.__isRecordDelimiter(nextChr, buf, pos + quote.length);
|
|
874
|
-
if (escape !== null && this.__isEscape(buf, pos, chr) && this.__isQuote(buf, pos + escape.length)) {
|
|
875
|
-
pos += escape.length - 1;
|
|
876
|
-
} else if (!nextChr || isNextChrDelimiter || isNextChrRecordDelimiter || isNextChrComment || isNextChrTrimable) {
|
|
877
|
-
this.state.quoting = false;
|
|
878
|
-
this.state.wasQuoting = true;
|
|
879
|
-
pos += quote.length - 1;
|
|
880
|
-
continue;
|
|
881
|
-
} else if (relax_quotes === false) {
|
|
882
|
-
const err = this.__error(
|
|
883
|
-
new CsvError(
|
|
884
|
-
"CSV_INVALID_CLOSING_QUOTE",
|
|
885
|
-
[
|
|
886
|
-
"Invalid Closing Quote:",
|
|
887
|
-
`got "${String.fromCharCode(nextChr)}"`,
|
|
888
|
-
`at line ${this.info.lines}`,
|
|
889
|
-
"instead of delimiter, record delimiter, trimable character",
|
|
890
|
-
"(if activated) or comment"
|
|
891
|
-
],
|
|
892
|
-
this.options,
|
|
893
|
-
this.__infoField()
|
|
894
|
-
)
|
|
895
|
-
);
|
|
896
|
-
if (err !== void 0) return err;
|
|
897
|
-
} else {
|
|
898
|
-
this.state.quoting = false;
|
|
899
|
-
this.state.wasQuoting = true;
|
|
900
|
-
this.state.field.prepend(quote);
|
|
901
|
-
pos += quote.length - 1;
|
|
902
|
-
}
|
|
903
|
-
} else {
|
|
904
|
-
if (this.state.field.length !== 0) {
|
|
905
|
-
if (relax_quotes === false) {
|
|
906
|
-
const info2 = this.__infoField();
|
|
907
|
-
const bom2 = Object.keys(boms).map(
|
|
908
|
-
(b) => boms[b].equals(this.state.field.toString()) ? b : false
|
|
909
|
-
).filter(Boolean)[0];
|
|
910
|
-
const err = this.__error(
|
|
911
|
-
new CsvError(
|
|
912
|
-
"INVALID_OPENING_QUOTE",
|
|
913
|
-
[
|
|
914
|
-
"Invalid Opening Quote:",
|
|
915
|
-
`a quote is found on field ${JSON.stringify(info2.column)} at line ${info2.lines}, value is ${JSON.stringify(this.state.field.toString(encoding))}`,
|
|
916
|
-
bom2 ? `(${bom2} bom)` : void 0
|
|
917
|
-
],
|
|
918
|
-
this.options,
|
|
919
|
-
info2,
|
|
920
|
-
{
|
|
921
|
-
field: this.state.field
|
|
922
|
-
}
|
|
923
|
-
)
|
|
924
|
-
);
|
|
925
|
-
if (err !== void 0) return err;
|
|
926
|
-
}
|
|
927
|
-
} else {
|
|
928
|
-
this.state.quoting = true;
|
|
929
|
-
pos += quote.length - 1;
|
|
930
|
-
continue;
|
|
931
|
-
}
|
|
932
|
-
}
|
|
933
|
-
}
|
|
934
|
-
if (this.state.quoting === false) {
|
|
935
|
-
const recordDelimiterLength = this.__isRecordDelimiter(
|
|
936
|
-
chr,
|
|
937
|
-
buf,
|
|
938
|
-
pos
|
|
939
|
-
);
|
|
940
|
-
if (recordDelimiterLength !== 0) {
|
|
941
|
-
const skipCommentLine = this.state.commenting && this.state.wasQuoting === false && this.state.record.length === 0 && this.state.field.length === 0;
|
|
942
|
-
if (skipCommentLine) {
|
|
943
|
-
this.info.comment_lines++;
|
|
944
|
-
} else {
|
|
945
|
-
if (this.state.enabled === false && this.info.lines + (this.state.wasRowDelimiter === true ? 1 : 0) >= from_line) {
|
|
946
|
-
this.state.enabled = true;
|
|
947
|
-
this.__resetField();
|
|
948
|
-
this.__resetRecord();
|
|
949
|
-
pos += recordDelimiterLength - 1;
|
|
950
|
-
continue;
|
|
951
|
-
}
|
|
952
|
-
if (skip_empty_lines === true && this.state.wasQuoting === false && this.state.record.length === 0 && this.state.field.length === 0) {
|
|
953
|
-
this.info.empty_lines++;
|
|
954
|
-
pos += recordDelimiterLength - 1;
|
|
955
|
-
continue;
|
|
956
|
-
}
|
|
957
|
-
this.info.bytes = this.state.bufBytesStart + pos;
|
|
958
|
-
const errField = this.__onField();
|
|
959
|
-
if (errField !== void 0) return errField;
|
|
960
|
-
this.info.bytes = this.state.bufBytesStart + pos + recordDelimiterLength;
|
|
961
|
-
const errRecord = this.__onRecord(push);
|
|
962
|
-
if (errRecord !== void 0) return errRecord;
|
|
963
|
-
if (to !== -1 && this.info.records >= to) {
|
|
964
|
-
this.state.stop = true;
|
|
965
|
-
close();
|
|
966
|
-
return;
|
|
967
|
-
}
|
|
968
|
-
}
|
|
969
|
-
this.state.commenting = false;
|
|
970
|
-
pos += recordDelimiterLength - 1;
|
|
971
|
-
continue;
|
|
972
|
-
}
|
|
973
|
-
if (this.state.commenting) {
|
|
974
|
-
continue;
|
|
975
|
-
}
|
|
976
|
-
if (comment !== null && (comment_no_infix === false || this.state.record.length === 0 && this.state.field.length === 0)) {
|
|
977
|
-
const commentCount = this.__compareBytes(comment, buf, pos, chr);
|
|
978
|
-
if (commentCount !== 0) {
|
|
979
|
-
this.state.commenting = true;
|
|
980
|
-
continue;
|
|
981
|
-
}
|
|
982
|
-
}
|
|
983
|
-
const delimiterLength = this.__isDelimiter(buf, pos, chr);
|
|
984
|
-
if (delimiterLength !== 0) {
|
|
985
|
-
this.info.bytes = this.state.bufBytesStart + pos;
|
|
986
|
-
const errField = this.__onField();
|
|
987
|
-
if (errField !== void 0) return errField;
|
|
988
|
-
pos += delimiterLength - 1;
|
|
989
|
-
continue;
|
|
990
|
-
}
|
|
991
|
-
}
|
|
992
|
-
}
|
|
993
|
-
if (this.state.commenting === false) {
|
|
994
|
-
if (max_record_size !== 0 && this.state.record_length + this.state.field.length > max_record_size) {
|
|
995
|
-
return this.__error(
|
|
996
|
-
new CsvError(
|
|
997
|
-
"CSV_MAX_RECORD_SIZE",
|
|
998
|
-
[
|
|
999
|
-
"Max Record Size:",
|
|
1000
|
-
"record exceed the maximum number of tolerated bytes",
|
|
1001
|
-
`of ${max_record_size}`,
|
|
1002
|
-
`at line ${this.info.lines}`
|
|
1003
|
-
],
|
|
1004
|
-
this.options,
|
|
1005
|
-
this.__infoField()
|
|
1006
|
-
)
|
|
1007
|
-
);
|
|
1008
|
-
}
|
|
1009
|
-
}
|
|
1010
|
-
const lappend = ltrim === false || this.state.quoting === true || this.state.field.length !== 0 || !this.__isCharTrimable(buf, pos);
|
|
1011
|
-
const rappend = rtrim === false || this.state.wasQuoting === false;
|
|
1012
|
-
if (lappend === true && rappend === true) {
|
|
1013
|
-
this.state.field.append(chr);
|
|
1014
|
-
} else if (rtrim === true && !this.__isCharTrimable(buf, pos)) {
|
|
1015
|
-
return this.__error(
|
|
1016
|
-
new CsvError(
|
|
1017
|
-
"CSV_NON_TRIMABLE_CHAR_AFTER_CLOSING_QUOTE",
|
|
1018
|
-
[
|
|
1019
|
-
"Invalid Closing Quote:",
|
|
1020
|
-
"found non trimable byte after quote",
|
|
1021
|
-
`at line ${this.info.lines}`
|
|
1022
|
-
],
|
|
1023
|
-
this.options,
|
|
1024
|
-
this.__infoField()
|
|
1025
|
-
)
|
|
1026
|
-
);
|
|
1027
|
-
} else {
|
|
1028
|
-
if (lappend === false) {
|
|
1029
|
-
pos += this.__isCharTrimable(buf, pos) - 1;
|
|
1030
|
-
}
|
|
1031
|
-
continue;
|
|
1032
|
-
}
|
|
1033
|
-
}
|
|
1034
|
-
if (end === true) {
|
|
1035
|
-
if (this.state.quoting === true) {
|
|
1036
|
-
const err = this.__error(
|
|
1037
|
-
new CsvError(
|
|
1038
|
-
"CSV_QUOTE_NOT_CLOSED",
|
|
1039
|
-
[
|
|
1040
|
-
"Quote Not Closed:",
|
|
1041
|
-
`the parsing is finished with an opening quote at line ${this.info.lines}`
|
|
1042
|
-
],
|
|
1043
|
-
this.options,
|
|
1044
|
-
this.__infoField()
|
|
1045
|
-
)
|
|
1046
|
-
);
|
|
1047
|
-
if (err !== void 0) return err;
|
|
1048
|
-
} else {
|
|
1049
|
-
if (this.state.wasQuoting === true || this.state.record.length !== 0 || this.state.field.length !== 0) {
|
|
1050
|
-
this.info.bytes = this.state.bufBytesStart + pos;
|
|
1051
|
-
const errField = this.__onField();
|
|
1052
|
-
if (errField !== void 0) return errField;
|
|
1053
|
-
const errRecord = this.__onRecord(push);
|
|
1054
|
-
if (errRecord !== void 0) return errRecord;
|
|
1055
|
-
} else if (this.state.wasRowDelimiter === true) {
|
|
1056
|
-
this.info.empty_lines++;
|
|
1057
|
-
} else if (this.state.commenting === true) {
|
|
1058
|
-
this.info.comment_lines++;
|
|
1059
|
-
}
|
|
1060
|
-
}
|
|
1061
|
-
} else {
|
|
1062
|
-
this.state.bufBytesStart += pos;
|
|
1063
|
-
this.state.previousBuf = buf.slice(pos);
|
|
1064
|
-
}
|
|
1065
|
-
if (this.state.wasRowDelimiter === true) {
|
|
1066
|
-
this.info.lines++;
|
|
1067
|
-
this.state.wasRowDelimiter = false;
|
|
1068
|
-
}
|
|
1069
|
-
},
|
|
1070
|
-
__onRecord: function(push) {
|
|
1071
|
-
const {
|
|
1072
|
-
columns,
|
|
1073
|
-
group_columns_by_name,
|
|
1074
|
-
encoding,
|
|
1075
|
-
info: info2,
|
|
1076
|
-
from,
|
|
1077
|
-
relax_column_count,
|
|
1078
|
-
relax_column_count_less,
|
|
1079
|
-
relax_column_count_more,
|
|
1080
|
-
raw,
|
|
1081
|
-
skip_records_with_empty_values
|
|
1082
|
-
} = this.options;
|
|
1083
|
-
const { enabled, record } = this.state;
|
|
1084
|
-
if (enabled === false) {
|
|
1085
|
-
return this.__resetRecord();
|
|
1086
|
-
}
|
|
1087
|
-
const recordLength = record.length;
|
|
1088
|
-
if (columns === true) {
|
|
1089
|
-
if (skip_records_with_empty_values === true && isRecordEmpty(record)) {
|
|
1090
|
-
this.__resetRecord();
|
|
1091
|
-
return;
|
|
1092
|
-
}
|
|
1093
|
-
return this.__firstLineToColumns(record);
|
|
1094
|
-
}
|
|
1095
|
-
if (columns === false && this.info.records === 0) {
|
|
1096
|
-
this.state.expectedRecordLength = recordLength;
|
|
1097
|
-
}
|
|
1098
|
-
if (recordLength !== this.state.expectedRecordLength) {
|
|
1099
|
-
const err = columns === false ? new CsvError(
|
|
1100
|
-
"CSV_RECORD_INCONSISTENT_FIELDS_LENGTH",
|
|
1101
|
-
[
|
|
1102
|
-
"Invalid Record Length:",
|
|
1103
|
-
`expect ${this.state.expectedRecordLength},`,
|
|
1104
|
-
`got ${recordLength} on line ${this.info.lines}`
|
|
1105
|
-
],
|
|
1106
|
-
this.options,
|
|
1107
|
-
this.__infoField(),
|
|
1108
|
-
{
|
|
1109
|
-
record
|
|
1110
|
-
}
|
|
1111
|
-
) : new CsvError(
|
|
1112
|
-
"CSV_RECORD_INCONSISTENT_COLUMNS",
|
|
1113
|
-
[
|
|
1114
|
-
"Invalid Record Length:",
|
|
1115
|
-
`columns length is ${columns.length},`,
|
|
1116
|
-
// rename columns
|
|
1117
|
-
`got ${recordLength} on line ${this.info.lines}`
|
|
1118
|
-
],
|
|
1119
|
-
this.options,
|
|
1120
|
-
this.__infoField(),
|
|
1121
|
-
{
|
|
1122
|
-
record
|
|
1123
|
-
}
|
|
1124
|
-
);
|
|
1125
|
-
if (relax_column_count === true || relax_column_count_less === true && recordLength < this.state.expectedRecordLength || relax_column_count_more === true && recordLength > this.state.expectedRecordLength) {
|
|
1126
|
-
this.info.invalid_field_length++;
|
|
1127
|
-
this.state.error = err;
|
|
1128
|
-
} else {
|
|
1129
|
-
const finalErr = this.__error(err);
|
|
1130
|
-
if (finalErr) return finalErr;
|
|
1131
|
-
}
|
|
1132
|
-
}
|
|
1133
|
-
if (skip_records_with_empty_values === true && isRecordEmpty(record)) {
|
|
1134
|
-
this.__resetRecord();
|
|
1135
|
-
return;
|
|
1136
|
-
}
|
|
1137
|
-
if (this.state.recordHasError === true) {
|
|
1138
|
-
this.__resetRecord();
|
|
1139
|
-
this.state.recordHasError = false;
|
|
1140
|
-
return;
|
|
1141
|
-
}
|
|
1142
|
-
this.info.records++;
|
|
1143
|
-
if (from === 1 || this.info.records >= from) {
|
|
1144
|
-
const { objname } = this.options;
|
|
1145
|
-
if (columns !== false) {
|
|
1146
|
-
const obj = {};
|
|
1147
|
-
for (let i = 0, l = record.length; i < l; i++) {
|
|
1148
|
-
if (columns[i] === void 0 || columns[i].disabled) continue;
|
|
1149
|
-
if (group_columns_by_name === true && obj[columns[i].name] !== void 0) {
|
|
1150
|
-
if (Array.isArray(obj[columns[i].name])) {
|
|
1151
|
-
obj[columns[i].name] = obj[columns[i].name].concat(record[i]);
|
|
1152
|
-
} else {
|
|
1153
|
-
obj[columns[i].name] = [obj[columns[i].name], record[i]];
|
|
1154
|
-
}
|
|
1155
|
-
} else {
|
|
1156
|
-
obj[columns[i].name] = record[i];
|
|
1157
|
-
}
|
|
1158
|
-
}
|
|
1159
|
-
if (raw === true || info2 === true) {
|
|
1160
|
-
const extRecord = Object.assign(
|
|
1161
|
-
{ record: obj },
|
|
1162
|
-
raw === true ? { raw: this.state.rawBuffer.toString(encoding) } : {},
|
|
1163
|
-
info2 === true ? { info: this.__infoRecord() } : {}
|
|
1164
|
-
);
|
|
1165
|
-
const err = this.__push(
|
|
1166
|
-
objname === void 0 ? extRecord : [obj[objname], extRecord],
|
|
1167
|
-
push
|
|
1168
|
-
);
|
|
1169
|
-
if (err) {
|
|
1170
|
-
return err;
|
|
1171
|
-
}
|
|
1172
|
-
} else {
|
|
1173
|
-
const err = this.__push(
|
|
1174
|
-
objname === void 0 ? obj : [obj[objname], obj],
|
|
1175
|
-
push
|
|
1176
|
-
);
|
|
1177
|
-
if (err) {
|
|
1178
|
-
return err;
|
|
1179
|
-
}
|
|
1180
|
-
}
|
|
1181
|
-
} else {
|
|
1182
|
-
if (raw === true || info2 === true) {
|
|
1183
|
-
const extRecord = Object.assign(
|
|
1184
|
-
{ record },
|
|
1185
|
-
raw === true ? { raw: this.state.rawBuffer.toString(encoding) } : {},
|
|
1186
|
-
info2 === true ? { info: this.__infoRecord() } : {}
|
|
1187
|
-
);
|
|
1188
|
-
const err = this.__push(
|
|
1189
|
-
objname === void 0 ? extRecord : [record[objname], extRecord],
|
|
1190
|
-
push
|
|
1191
|
-
);
|
|
1192
|
-
if (err) {
|
|
1193
|
-
return err;
|
|
1194
|
-
}
|
|
1195
|
-
} else {
|
|
1196
|
-
const err = this.__push(
|
|
1197
|
-
objname === void 0 ? record : [record[objname], record],
|
|
1198
|
-
push
|
|
1199
|
-
);
|
|
1200
|
-
if (err) {
|
|
1201
|
-
return err;
|
|
1202
|
-
}
|
|
1203
|
-
}
|
|
1204
|
-
}
|
|
1205
|
-
}
|
|
1206
|
-
this.__resetRecord();
|
|
1207
|
-
},
|
|
1208
|
-
__firstLineToColumns: function(record) {
|
|
1209
|
-
const { firstLineToHeaders } = this.state;
|
|
1210
|
-
try {
|
|
1211
|
-
const headers = firstLineToHeaders === void 0 ? record : firstLineToHeaders.call(null, record);
|
|
1212
|
-
if (!Array.isArray(headers)) {
|
|
1213
|
-
return this.__error(
|
|
1214
|
-
new CsvError(
|
|
1215
|
-
"CSV_INVALID_COLUMN_MAPPING",
|
|
1216
|
-
[
|
|
1217
|
-
"Invalid Column Mapping:",
|
|
1218
|
-
"expect an array from column function,",
|
|
1219
|
-
`got ${JSON.stringify(headers)}`
|
|
1220
|
-
],
|
|
1221
|
-
this.options,
|
|
1222
|
-
this.__infoField(),
|
|
1223
|
-
{
|
|
1224
|
-
headers
|
|
1225
|
-
}
|
|
1226
|
-
)
|
|
1227
|
-
);
|
|
1228
|
-
}
|
|
1229
|
-
const normalizedHeaders = normalize_columns_array(headers);
|
|
1230
|
-
this.state.expectedRecordLength = normalizedHeaders.length;
|
|
1231
|
-
this.options.columns = normalizedHeaders;
|
|
1232
|
-
this.__resetRecord();
|
|
1233
|
-
return;
|
|
1234
|
-
} catch (err) {
|
|
1235
|
-
return err;
|
|
1236
|
-
}
|
|
1237
|
-
},
|
|
1238
|
-
__resetRecord: function() {
|
|
1239
|
-
if (this.options.raw === true) {
|
|
1240
|
-
this.state.rawBuffer.reset();
|
|
1241
|
-
}
|
|
1242
|
-
this.state.error = void 0;
|
|
1243
|
-
this.state.record = [];
|
|
1244
|
-
this.state.record_length = 0;
|
|
1245
|
-
},
|
|
1246
|
-
__onField: function() {
|
|
1247
|
-
const { cast, encoding, rtrim, max_record_size } = this.options;
|
|
1248
|
-
const { enabled, wasQuoting } = this.state;
|
|
1249
|
-
if (enabled === false) {
|
|
1250
|
-
return this.__resetField();
|
|
1251
|
-
}
|
|
1252
|
-
let field = this.state.field.toString(encoding);
|
|
1253
|
-
if (rtrim === true && wasQuoting === false) {
|
|
1254
|
-
field = field.trimRight();
|
|
1255
|
-
}
|
|
1256
|
-
if (cast === true) {
|
|
1257
|
-
const [err, f] = this.__cast(field);
|
|
1258
|
-
if (err !== void 0) return err;
|
|
1259
|
-
field = f;
|
|
1260
|
-
}
|
|
1261
|
-
this.state.record.push(field);
|
|
1262
|
-
if (max_record_size !== 0 && typeof field === "string") {
|
|
1263
|
-
this.state.record_length += field.length;
|
|
1264
|
-
}
|
|
1265
|
-
this.__resetField();
|
|
1266
|
-
},
|
|
1267
|
-
__resetField: function() {
|
|
1268
|
-
this.state.field.reset();
|
|
1269
|
-
this.state.wasQuoting = false;
|
|
1270
|
-
},
|
|
1271
|
-
__push: function(record, push) {
|
|
1272
|
-
const { on_record } = this.options;
|
|
1273
|
-
if (on_record !== void 0) {
|
|
1274
|
-
const info2 = this.__infoRecord();
|
|
1275
|
-
try {
|
|
1276
|
-
record = on_record.call(null, record, info2);
|
|
1277
|
-
} catch (err) {
|
|
1278
|
-
return err;
|
|
1279
|
-
}
|
|
1280
|
-
if (record === void 0 || record === null) {
|
|
1281
|
-
return;
|
|
1282
|
-
}
|
|
1283
|
-
}
|
|
1284
|
-
push(record);
|
|
1285
|
-
},
|
|
1286
|
-
// Return a tuple with the error and the casted value
|
|
1287
|
-
__cast: function(field) {
|
|
1288
|
-
const { columns, relax_column_count } = this.options;
|
|
1289
|
-
const isColumns = Array.isArray(columns);
|
|
1290
|
-
if (isColumns === true && relax_column_count && this.options.columns.length <= this.state.record.length) {
|
|
1291
|
-
return [void 0, void 0];
|
|
1292
|
-
}
|
|
1293
|
-
if (this.state.castField !== null) {
|
|
1294
|
-
try {
|
|
1295
|
-
const info2 = this.__infoField();
|
|
1296
|
-
return [void 0, this.state.castField.call(null, field, info2)];
|
|
1297
|
-
} catch (err) {
|
|
1298
|
-
return [err];
|
|
1299
|
-
}
|
|
1300
|
-
}
|
|
1301
|
-
if (this.__isFloat(field)) {
|
|
1302
|
-
return [void 0, parseFloat(field)];
|
|
1303
|
-
} else if (this.options.cast_date !== false) {
|
|
1304
|
-
const info2 = this.__infoField();
|
|
1305
|
-
return [void 0, this.options.cast_date.call(null, field, info2)];
|
|
1306
|
-
}
|
|
1307
|
-
return [void 0, field];
|
|
1308
|
-
},
|
|
1309
|
-
// Helper to test if a character is a space or a line delimiter
|
|
1310
|
-
__isCharTrimable: function(buf, pos) {
|
|
1311
|
-
const isTrim = (buf2, pos2) => {
|
|
1312
|
-
const { timchars } = this.state;
|
|
1313
|
-
loop1: for (let i = 0; i < timchars.length; i++) {
|
|
1314
|
-
const timchar = timchars[i];
|
|
1315
|
-
for (let j = 0; j < timchar.length; j++) {
|
|
1316
|
-
if (timchar[j] !== buf2[pos2 + j]) continue loop1;
|
|
1317
|
-
}
|
|
1318
|
-
return timchar.length;
|
|
1319
|
-
}
|
|
1320
|
-
return 0;
|
|
1321
|
-
};
|
|
1322
|
-
return isTrim(buf, pos);
|
|
1323
|
-
},
|
|
1324
|
-
// Keep it in case we implement the `cast_int` option
|
|
1325
|
-
// __isInt(value){
|
|
1326
|
-
// // return Number.isInteger(parseInt(value))
|
|
1327
|
-
// // return !isNaN( parseInt( obj ) );
|
|
1328
|
-
// return /^(\-|\+)?[1-9][0-9]*$/.test(value)
|
|
1329
|
-
// }
|
|
1330
|
-
__isFloat: function(value) {
|
|
1331
|
-
return value - parseFloat(value) + 1 >= 0;
|
|
1332
|
-
},
|
|
1333
|
-
__compareBytes: function(sourceBuf, targetBuf, targetPos, firstByte) {
|
|
1334
|
-
if (sourceBuf[0] !== firstByte) return 0;
|
|
1335
|
-
const sourceLength = sourceBuf.length;
|
|
1336
|
-
for (let i = 1; i < sourceLength; i++) {
|
|
1337
|
-
if (sourceBuf[i] !== targetBuf[targetPos + i]) return 0;
|
|
1338
|
-
}
|
|
1339
|
-
return sourceLength;
|
|
1340
|
-
},
|
|
1341
|
-
__isDelimiter: function(buf, pos, chr) {
|
|
1342
|
-
const { delimiter, ignore_last_delimiters } = this.options;
|
|
1343
|
-
if (ignore_last_delimiters === true && this.state.record.length === this.options.columns.length - 1) {
|
|
1344
|
-
return 0;
|
|
1345
|
-
} else if (ignore_last_delimiters !== false && typeof ignore_last_delimiters === "number" && this.state.record.length === ignore_last_delimiters - 1) {
|
|
1346
|
-
return 0;
|
|
1347
|
-
}
|
|
1348
|
-
loop1: for (let i = 0; i < delimiter.length; i++) {
|
|
1349
|
-
const del = delimiter[i];
|
|
1350
|
-
if (del[0] === chr) {
|
|
1351
|
-
for (let j = 1; j < del.length; j++) {
|
|
1352
|
-
if (del[j] !== buf[pos + j]) continue loop1;
|
|
1353
|
-
}
|
|
1354
|
-
return del.length;
|
|
1355
|
-
}
|
|
1356
|
-
}
|
|
1357
|
-
return 0;
|
|
1358
|
-
},
|
|
1359
|
-
__isRecordDelimiter: function(chr, buf, pos) {
|
|
1360
|
-
const { record_delimiter } = this.options;
|
|
1361
|
-
const recordDelimiterLength = record_delimiter.length;
|
|
1362
|
-
loop1: for (let i = 0; i < recordDelimiterLength; i++) {
|
|
1363
|
-
const rd = record_delimiter[i];
|
|
1364
|
-
const rdLength = rd.length;
|
|
1365
|
-
if (rd[0] !== chr) {
|
|
1366
|
-
continue;
|
|
1367
|
-
}
|
|
1368
|
-
for (let j = 1; j < rdLength; j++) {
|
|
1369
|
-
if (rd[j] !== buf[pos + j]) {
|
|
1370
|
-
continue loop1;
|
|
1371
|
-
}
|
|
1372
|
-
}
|
|
1373
|
-
return rd.length;
|
|
1374
|
-
}
|
|
1375
|
-
return 0;
|
|
1376
|
-
},
|
|
1377
|
-
__isEscape: function(buf, pos, chr) {
|
|
1378
|
-
const { escape } = this.options;
|
|
1379
|
-
if (escape === null) return false;
|
|
1380
|
-
const l = escape.length;
|
|
1381
|
-
if (escape[0] === chr) {
|
|
1382
|
-
for (let i = 0; i < l; i++) {
|
|
1383
|
-
if (escape[i] !== buf[pos + i]) {
|
|
1384
|
-
return false;
|
|
1385
|
-
}
|
|
1386
|
-
}
|
|
1387
|
-
return true;
|
|
1388
|
-
}
|
|
1389
|
-
return false;
|
|
1390
|
-
},
|
|
1391
|
-
__isQuote: function(buf, pos) {
|
|
1392
|
-
const { quote } = this.options;
|
|
1393
|
-
if (quote === null) return false;
|
|
1394
|
-
const l = quote.length;
|
|
1395
|
-
for (let i = 0; i < l; i++) {
|
|
1396
|
-
if (quote[i] !== buf[pos + i]) {
|
|
1397
|
-
return false;
|
|
1398
|
-
}
|
|
1399
|
-
}
|
|
1400
|
-
return true;
|
|
1401
|
-
},
|
|
1402
|
-
__autoDiscoverRecordDelimiter: function(buf, pos) {
|
|
1403
|
-
const { encoding } = this.options;
|
|
1404
|
-
const rds = [
|
|
1405
|
-
// Important, the windows line ending must be before mac os 9
|
|
1406
|
-
Buffer.from("\r\n", encoding),
|
|
1407
|
-
Buffer.from("\n", encoding),
|
|
1408
|
-
Buffer.from("\r", encoding)
|
|
1409
|
-
];
|
|
1410
|
-
loop: for (let i = 0; i < rds.length; i++) {
|
|
1411
|
-
const l = rds[i].length;
|
|
1412
|
-
for (let j = 0; j < l; j++) {
|
|
1413
|
-
if (rds[i][j] !== buf[pos + j]) {
|
|
1414
|
-
continue loop;
|
|
1415
|
-
}
|
|
1416
|
-
}
|
|
1417
|
-
this.options.record_delimiter.push(rds[i]);
|
|
1418
|
-
this.state.recordDelimiterMaxLength = rds[i].length;
|
|
1419
|
-
return rds[i].length;
|
|
1420
|
-
}
|
|
1421
|
-
return 0;
|
|
1422
|
-
},
|
|
1423
|
-
__error: function(msg) {
|
|
1424
|
-
const { encoding, raw, skip_records_with_error } = this.options;
|
|
1425
|
-
const err = typeof msg === "string" ? new Error(msg) : msg;
|
|
1426
|
-
if (skip_records_with_error) {
|
|
1427
|
-
this.state.recordHasError = true;
|
|
1428
|
-
if (this.options.on_skip !== void 0) {
|
|
1429
|
-
this.options.on_skip(
|
|
1430
|
-
err,
|
|
1431
|
-
raw ? this.state.rawBuffer.toString(encoding) : void 0
|
|
1432
|
-
);
|
|
1433
|
-
}
|
|
1434
|
-
return void 0;
|
|
1435
|
-
} else {
|
|
1436
|
-
return err;
|
|
1437
|
-
}
|
|
1438
|
-
},
|
|
1439
|
-
__infoDataSet: function() {
|
|
1440
|
-
return {
|
|
1441
|
-
...this.info,
|
|
1442
|
-
columns: this.options.columns
|
|
1443
|
-
};
|
|
1444
|
-
},
|
|
1445
|
-
__infoRecord: function() {
|
|
1446
|
-
const { columns, raw, encoding } = this.options;
|
|
1447
|
-
return {
|
|
1448
|
-
...this.__infoDataSet(),
|
|
1449
|
-
error: this.state.error,
|
|
1450
|
-
header: columns === true,
|
|
1451
|
-
index: this.state.record.length,
|
|
1452
|
-
raw: raw ? this.state.rawBuffer.toString(encoding) : void 0
|
|
1453
|
-
};
|
|
1454
|
-
},
|
|
1455
|
-
__infoField: function() {
|
|
1456
|
-
const { columns } = this.options;
|
|
1457
|
-
const isColumns = Array.isArray(columns);
|
|
1458
|
-
return {
|
|
1459
|
-
...this.__infoRecord(),
|
|
1460
|
-
column: isColumns === true ? columns.length > this.state.record.length ? columns[this.state.record.length].name : null : this.state.record.length,
|
|
1461
|
-
quoting: this.state.wasQuoting
|
|
1462
|
-
};
|
|
1463
|
-
}
|
|
1464
|
-
};
|
|
1465
|
-
};
|
|
1466
|
-
|
|
1467
|
-
// ../../node_modules/csv-parse/lib/sync.js
|
|
1468
|
-
var parse = function(data, opts = {}) {
|
|
1469
|
-
if (typeof data === "string") {
|
|
1470
|
-
data = Buffer.from(data);
|
|
1471
|
-
}
|
|
1472
|
-
const records = opts && opts.objname ? {} : [];
|
|
1473
|
-
const parser = transform(opts);
|
|
1474
|
-
const push = (record) => {
|
|
1475
|
-
if (parser.options.objname === void 0) records.push(record);
|
|
1476
|
-
else {
|
|
1477
|
-
records[record[0]] = record[1];
|
|
1478
|
-
}
|
|
1479
|
-
};
|
|
1480
|
-
const close = () => {
|
|
1481
|
-
};
|
|
1482
|
-
const err1 = parser.parse(data, false, push, close);
|
|
1483
|
-
if (err1 !== void 0) throw err1;
|
|
1484
|
-
const err2 = parser.parse(void 0, true, push, close);
|
|
1485
|
-
if (err2 !== void 0) throw err2;
|
|
1486
|
-
return records;
|
|
1487
|
-
};
|
|
1488
|
-
export {
|
|
1489
|
-
CsvError,
|
|
1490
|
-
parse
|
|
1491
|
-
};
|
|
1492
|
-
//# sourceMappingURL=sync-FQGEFK5M.js.map
|