select-csv 1.1.20 → 1.1.21
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/LICENSE.txt +21 -0
- package/README.md +132 -426
- package/package.json +37 -34
- package/selectcsv.js +254 -1288
package/selectcsv.js
CHANGED
|
@@ -1,1288 +1,254 @@
|
|
|
1
|
-
const fs = require("fs");
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
let
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
linebreak,
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
x1;
|
|
256
|
-
let fd, bSize, bytesRead,
|
|
257
|
-
data2
|
|
258
|
-
if (csv) {
|
|
259
|
-
[fd, bSize] = csv
|
|
260
|
-
offsFd = data.length
|
|
261
|
-
bytesRead = 1
|
|
262
|
-
}
|
|
263
|
-
x1 = data.indexOf(linebreak, x0)
|
|
264
|
-
let m = 2,
|
|
265
|
-
n = 0;
|
|
266
|
-
let sbstr = sbstr2 = "",
|
|
267
|
-
rows = [],
|
|
268
|
-
cols = []
|
|
269
|
-
let q0, q1;
|
|
270
|
-
while (m) {
|
|
271
|
-
if (col) {
|
|
272
|
-
let lDlm = d.length;
|
|
273
|
-
if (oJson) {
|
|
274
|
-
let j;
|
|
275
|
-
cols = {}
|
|
276
|
-
if (quote) {
|
|
277
|
-
while (x1 != -1) {
|
|
278
|
-
sbstr = data.slice(x0, x1)
|
|
279
|
-
q1 = sbstr.indexOf('"');
|
|
280
|
-
if (q1 != -1) {
|
|
281
|
-
y0 = 0, y1 = sbstr.indexOf(d);
|
|
282
|
-
j = 0
|
|
283
|
-
while (y1 != -1) {
|
|
284
|
-
sbstr2 = sbstr.slice(y0, y1)
|
|
285
|
-
if (y1 < q1) {
|
|
286
|
-
cols[oJson[j]] = sbstr2
|
|
287
|
-
y0 = y1 + lDlm
|
|
288
|
-
y1 = sbstr.indexOf(d, y0);
|
|
289
|
-
} else {
|
|
290
|
-
z = y0;
|
|
291
|
-
q0 = q1 + lDlm
|
|
292
|
-
q1 = sbstr.indexOf('"', q0);
|
|
293
|
-
if (q1 != -1) {
|
|
294
|
-
y1 = sbstr.indexOf(d, q1 + lDlm);
|
|
295
|
-
q1 = sbstr.indexOf('"', y1 + lDlm);
|
|
296
|
-
if (q1 == -1) q1 = sbstr.length
|
|
297
|
-
} else {
|
|
298
|
-
y1 = sbstr.indexOf(d, q0);
|
|
299
|
-
}
|
|
300
|
-
}
|
|
301
|
-
j++;
|
|
302
|
-
}
|
|
303
|
-
} else {
|
|
304
|
-
y0 = 0, y1 = sbstr.indexOf(d);
|
|
305
|
-
j = 0
|
|
306
|
-
while (y1 != -1) {
|
|
307
|
-
sbstr2 = sbstr.slice(y0, y1)
|
|
308
|
-
cols[oJson[j]] = sbstr2
|
|
309
|
-
y0 = y1 + lDlm
|
|
310
|
-
y1 = sbstr.indexOf(d, y0);
|
|
311
|
-
j++;
|
|
312
|
-
}
|
|
313
|
-
}
|
|
314
|
-
if (y0) {
|
|
315
|
-
cols[oJson[j]] = sbstr.slice(y0)
|
|
316
|
-
rows.push(cols)
|
|
317
|
-
}
|
|
318
|
-
cols = {}
|
|
319
|
-
x0 = x1 + lLbr
|
|
320
|
-
x1 = data.indexOf(linebreak, x0);
|
|
321
|
-
n++;
|
|
322
|
-
}
|
|
323
|
-
} else {
|
|
324
|
-
while (x1 != -1) {
|
|
325
|
-
sbstr = data.slice(x0, x1)
|
|
326
|
-
y0 = 0, y1 = sbstr.indexOf(d);
|
|
327
|
-
j = 0
|
|
328
|
-
while (y1 != -1) {
|
|
329
|
-
sbstr2 = sbstr.slice(y0, y1)
|
|
330
|
-
cols[oJson[j]] = sbstr2
|
|
331
|
-
y0 = y1 + lDlm
|
|
332
|
-
y1 = sbstr.indexOf(d, y0);
|
|
333
|
-
j++;
|
|
334
|
-
}
|
|
335
|
-
if (y0) {
|
|
336
|
-
cols[oJson[j]] = sbstr.slice(y0)
|
|
337
|
-
rows.push(cols)
|
|
338
|
-
}
|
|
339
|
-
cols = {}
|
|
340
|
-
x0 = x1 + lLbr
|
|
341
|
-
x1 = data.indexOf(linebreak, x0);
|
|
342
|
-
n++;
|
|
343
|
-
}
|
|
344
|
-
}
|
|
345
|
-
} else {
|
|
346
|
-
if (quote) {
|
|
347
|
-
while (x1 != -1) {
|
|
348
|
-
sbstr = data.slice(x0, x1)
|
|
349
|
-
q1 = sbstr.indexOf('"');
|
|
350
|
-
if (q1 != -1) {
|
|
351
|
-
y0 = 0, y1 = sbstr.indexOf(d);
|
|
352
|
-
while (y1 != -1) {
|
|
353
|
-
sbstr2 = sbstr.slice(y0, y1)
|
|
354
|
-
if (y1 < q1) {
|
|
355
|
-
cols.push(sbstr2)
|
|
356
|
-
y0 = y1 + lDlm
|
|
357
|
-
y1 = sbstr.indexOf(d, y0);
|
|
358
|
-
} else {
|
|
359
|
-
z = y0;
|
|
360
|
-
q0 = q1 + lDlm
|
|
361
|
-
q1 = sbstr.indexOf('"', q0);
|
|
362
|
-
if (q1 != -1) {
|
|
363
|
-
y1 = sbstr.indexOf(d, q1 + lDlm);
|
|
364
|
-
q1 = sbstr.indexOf('"', y1 + lDlm);
|
|
365
|
-
if (q1 == -1) q1 = sbstr.length
|
|
366
|
-
} else {
|
|
367
|
-
y1 = sbstr.indexOf(d, q0);
|
|
368
|
-
}
|
|
369
|
-
}
|
|
370
|
-
}
|
|
371
|
-
} else {
|
|
372
|
-
y0 = 0, y1 = sbstr.indexOf(d);
|
|
373
|
-
while (y1 != -1) {
|
|
374
|
-
sbstr2 = sbstr.slice(y0, y1)
|
|
375
|
-
cols.push(sbstr2)
|
|
376
|
-
y0 = y1 + lDlm
|
|
377
|
-
y1 = sbstr.indexOf(d, y0);
|
|
378
|
-
}
|
|
379
|
-
}
|
|
380
|
-
if (y0) {
|
|
381
|
-
cols.push(sbstr.slice(y0))
|
|
382
|
-
rows.push(cols)
|
|
383
|
-
}
|
|
384
|
-
cols = []
|
|
385
|
-
x0 = x1 + lLbr
|
|
386
|
-
x1 = data.indexOf(linebreak, x0);
|
|
387
|
-
n++;
|
|
388
|
-
}
|
|
389
|
-
} else {
|
|
390
|
-
while (x1 != -1) {
|
|
391
|
-
sbstr = data.slice(x0, x1)
|
|
392
|
-
y0 = 0, y1 = sbstr.indexOf(d);
|
|
393
|
-
while (y1 != -1) {
|
|
394
|
-
sbstr2 = sbstr.slice(y0, y1)
|
|
395
|
-
cols.push(sbstr2)
|
|
396
|
-
y0 = y1 + lDlm
|
|
397
|
-
y1 = sbstr.indexOf(d, y0);
|
|
398
|
-
}
|
|
399
|
-
if (y0) {
|
|
400
|
-
cols.push(sbstr.slice(y0))
|
|
401
|
-
rows.push(cols)
|
|
402
|
-
}
|
|
403
|
-
cols = []
|
|
404
|
-
x0 = x1 + lLbr
|
|
405
|
-
x1 = data.indexOf(linebreak, x0);
|
|
406
|
-
n++;
|
|
407
|
-
}
|
|
408
|
-
}
|
|
409
|
-
}
|
|
410
|
-
} else {
|
|
411
|
-
while (x1 != -1) {
|
|
412
|
-
cols.push(data.slice(x0, x1))
|
|
413
|
-
rows.push(cols)
|
|
414
|
-
cols = []
|
|
415
|
-
x0 = x1 + lLbr
|
|
416
|
-
x1 = data.indexOf(linebreak, x0);
|
|
417
|
-
n++;
|
|
418
|
-
}
|
|
419
|
-
}
|
|
420
|
-
m--;
|
|
421
|
-
if (x1 == -1) {
|
|
422
|
-
if (m) {
|
|
423
|
-
if (csv) {
|
|
424
|
-
bytesRead = 1;
|
|
425
|
-
data = data.slice(x0)
|
|
426
|
-
xs += x0;
|
|
427
|
-
x0 = 0
|
|
428
|
-
while (bytesRead && x1 == -1) {
|
|
429
|
-
[bytesRead, data2] = getData(fd, bSize, offsFd)
|
|
430
|
-
offsFd += bytesRead;
|
|
431
|
-
data += data2;
|
|
432
|
-
x1 = data.indexOf(linebreak)
|
|
433
|
-
}
|
|
434
|
-
if (x1 == -1) x1 = data.length;
|
|
435
|
-
if (bytesRead) m++;
|
|
436
|
-
} else x1 = data.length;
|
|
437
|
-
} else x0 = x1 = data.length;
|
|
438
|
-
}
|
|
439
|
-
}
|
|
440
|
-
return {
|
|
441
|
-
get: {
|
|
442
|
-
rows: rows,
|
|
443
|
-
row_count: n
|
|
444
|
-
},
|
|
445
|
-
offs: [xs + x0, n],
|
|
446
|
-
row_count: n
|
|
447
|
-
};
|
|
448
|
-
}
|
|
449
|
-
|
|
450
|
-
function getRowsOffsFT(data, from, to, info, sO_R) {
|
|
451
|
-
let {
|
|
452
|
-
header,
|
|
453
|
-
linebreak,
|
|
454
|
-
quote,
|
|
455
|
-
col,
|
|
456
|
-
d,
|
|
457
|
-
csv,
|
|
458
|
-
oJson
|
|
459
|
-
} = info
|
|
460
|
-
let lLbr = linebreak.length,
|
|
461
|
-
offsFd,
|
|
462
|
-
xs = x0 = sO_R,
|
|
463
|
-
x1;
|
|
464
|
-
let fd, bSize, bytesRead,
|
|
465
|
-
data2
|
|
466
|
-
if (csv) {
|
|
467
|
-
[fd, bSize] = csv
|
|
468
|
-
offsFd = data.length
|
|
469
|
-
bytesRead = 1
|
|
470
|
-
}
|
|
471
|
-
x1 = data.indexOf(linebreak, x0)
|
|
472
|
-
let m = 2,
|
|
473
|
-
n = 0,
|
|
474
|
-
c = 0;
|
|
475
|
-
let sbstr = sbstr2 = "",
|
|
476
|
-
rows = [],
|
|
477
|
-
cols = []
|
|
478
|
-
let q0, q1;
|
|
479
|
-
while (m) {
|
|
480
|
-
if (col) {
|
|
481
|
-
let lDlm = d.length;
|
|
482
|
-
if (oJson) {
|
|
483
|
-
let j;
|
|
484
|
-
cols = {}
|
|
485
|
-
if (quote) {
|
|
486
|
-
while (x1 != -1) {
|
|
487
|
-
if (n >= from) {
|
|
488
|
-
if (n < to) {
|
|
489
|
-
sbstr = data.slice(x0, x1)
|
|
490
|
-
q1 = sbstr.indexOf('"');
|
|
491
|
-
if (q1 != -1) {
|
|
492
|
-
y0 = 0, y1 = sbstr.indexOf(d);
|
|
493
|
-
j = 0;
|
|
494
|
-
while (y1 != -1) {
|
|
495
|
-
sbstr2 = sbstr.slice(y0, y1)
|
|
496
|
-
if (y1 < q1) {
|
|
497
|
-
cols[oJson[j]] = sbstr2
|
|
498
|
-
y0 = y1 + lDlm
|
|
499
|
-
y1 = sbstr.indexOf(d, y0);
|
|
500
|
-
} else {
|
|
501
|
-
z = y0;
|
|
502
|
-
q0 = q1 + lDlm
|
|
503
|
-
q1 = sbstr.indexOf('"', q0);
|
|
504
|
-
if (q1 != -1) {
|
|
505
|
-
y1 = sbstr.indexOf(d, q1 + lDlm);
|
|
506
|
-
q1 = sbstr.indexOf('"', y1 + lDlm);
|
|
507
|
-
if (q1 == -1) q1 = sbstr.length
|
|
508
|
-
} else {
|
|
509
|
-
y1 = sbstr.indexOf(d, q0);
|
|
510
|
-
}
|
|
511
|
-
}
|
|
512
|
-
j++;
|
|
513
|
-
}
|
|
514
|
-
} else {
|
|
515
|
-
y0 = 0, y1 = sbstr.indexOf(d);
|
|
516
|
-
j = 0;
|
|
517
|
-
while (y1 != -1) {
|
|
518
|
-
sbstr2 = sbstr.slice(y0, y1)
|
|
519
|
-
cols[oJson[j]] = sbstr2
|
|
520
|
-
y0 = y1 + lDlm
|
|
521
|
-
y1 = sbstr.indexOf(d, y0);
|
|
522
|
-
j++;
|
|
523
|
-
}
|
|
524
|
-
}
|
|
525
|
-
if (y0) {
|
|
526
|
-
cols[oJson[j]] = sbstr.slice(y0)
|
|
527
|
-
rows.push(cols)
|
|
528
|
-
}
|
|
529
|
-
cols = {}
|
|
530
|
-
c++;
|
|
531
|
-
} else {
|
|
532
|
-
n++;
|
|
533
|
-
break
|
|
534
|
-
}
|
|
535
|
-
}
|
|
536
|
-
x0 = x1 + lLbr
|
|
537
|
-
x1 = data.indexOf(linebreak, x0);
|
|
538
|
-
n++;
|
|
539
|
-
}
|
|
540
|
-
} else {
|
|
541
|
-
while (x1 != -1) {
|
|
542
|
-
if (n < to) {
|
|
543
|
-
if (n >= from) {
|
|
544
|
-
sbstr = data.slice(x0, x1)
|
|
545
|
-
y0 = 0, y1 = sbstr.indexOf(d);
|
|
546
|
-
j = 0;
|
|
547
|
-
while (y1 != -1) {
|
|
548
|
-
sbstr2 = sbstr.slice(y0, y1)
|
|
549
|
-
cols[oJson[j]] = sbstr2
|
|
550
|
-
y0 = y1 + lDlm
|
|
551
|
-
y1 = sbstr.indexOf(d, y0);
|
|
552
|
-
j++;
|
|
553
|
-
}
|
|
554
|
-
if (y0) {
|
|
555
|
-
cols[oJson[j]] = sbstr.slice(y0)
|
|
556
|
-
rows.push(cols)
|
|
557
|
-
}
|
|
558
|
-
cols = {}
|
|
559
|
-
c++;
|
|
560
|
-
}
|
|
561
|
-
} else {
|
|
562
|
-
n++;
|
|
563
|
-
break
|
|
564
|
-
}
|
|
565
|
-
x0 = x1 + lLbr
|
|
566
|
-
x1 = data.indexOf(linebreak, x0);
|
|
567
|
-
n++;
|
|
568
|
-
}
|
|
569
|
-
}
|
|
570
|
-
} else {
|
|
571
|
-
if (quote) {
|
|
572
|
-
while (x1 != -1) {
|
|
573
|
-
if (n >= from) {
|
|
574
|
-
if (n < to) {
|
|
575
|
-
sbstr = data.slice(x0, x1)
|
|
576
|
-
q1 = sbstr.indexOf('"');
|
|
577
|
-
if (q1 != -1) {
|
|
578
|
-
y0 = 0, y1 = sbstr.indexOf(d);
|
|
579
|
-
while (y1 != -1) {
|
|
580
|
-
sbstr2 = sbstr.slice(y0, y1)
|
|
581
|
-
if (y1 < q1) {
|
|
582
|
-
cols.push(sbstr2)
|
|
583
|
-
y0 = y1 + lDlm
|
|
584
|
-
y1 = sbstr.indexOf(d, y0);
|
|
585
|
-
} else {
|
|
586
|
-
z = y0;
|
|
587
|
-
q0 = q1 + lDlm
|
|
588
|
-
q1 = sbstr.indexOf('"', q0);
|
|
589
|
-
if (q1 != -1) {
|
|
590
|
-
y1 = sbstr.indexOf(d, q1 + lDlm);
|
|
591
|
-
q1 = sbstr.indexOf('"', y1 + lDlm);
|
|
592
|
-
if (q1 == -1) q1 = sbstr.length
|
|
593
|
-
} else {
|
|
594
|
-
y1 = sbstr.indexOf(d, q0);
|
|
595
|
-
}
|
|
596
|
-
}
|
|
597
|
-
}
|
|
598
|
-
} else {
|
|
599
|
-
y0 = 0, y1 = sbstr.indexOf(d);
|
|
600
|
-
while (y1 != -1) {
|
|
601
|
-
sbstr2 = sbstr.slice(y0, y1)
|
|
602
|
-
cols.push(sbstr2)
|
|
603
|
-
y0 = y1 + lDlm
|
|
604
|
-
y1 = sbstr.indexOf(d, y0);
|
|
605
|
-
}
|
|
606
|
-
}
|
|
607
|
-
if (y0) {
|
|
608
|
-
cols.push(sbstr.slice(y0))
|
|
609
|
-
rows.push(cols)
|
|
610
|
-
}
|
|
611
|
-
cols = []
|
|
612
|
-
c++;
|
|
613
|
-
} else {
|
|
614
|
-
n++;
|
|
615
|
-
break
|
|
616
|
-
}
|
|
617
|
-
}
|
|
618
|
-
x0 = x1 + lLbr
|
|
619
|
-
x1 = data.indexOf(linebreak, x0);
|
|
620
|
-
n++;
|
|
621
|
-
}
|
|
622
|
-
} else {
|
|
623
|
-
while (x1 != -1) {
|
|
624
|
-
if (n < to) {
|
|
625
|
-
if (n >= from) {
|
|
626
|
-
sbstr = data.slice(x0, x1)
|
|
627
|
-
y0 = 0, y1 = sbstr.indexOf(d);
|
|
628
|
-
while (y1 != -1) {
|
|
629
|
-
sbstr2 = sbstr.slice(y0, y1)
|
|
630
|
-
cols.push(sbstr2)
|
|
631
|
-
y0 = y1 + lDlm
|
|
632
|
-
y1 = sbstr.indexOf(d, y0);
|
|
633
|
-
}
|
|
634
|
-
if (y0) {
|
|
635
|
-
cols.push(sbstr.slice(y0))
|
|
636
|
-
rows.push(cols)
|
|
637
|
-
}
|
|
638
|
-
cols = []
|
|
639
|
-
c++;
|
|
640
|
-
}
|
|
641
|
-
} else {
|
|
642
|
-
n++;
|
|
643
|
-
break
|
|
644
|
-
}
|
|
645
|
-
x0 = x1 + lLbr
|
|
646
|
-
x1 = data.indexOf(linebreak, x0);
|
|
647
|
-
n++;
|
|
648
|
-
}
|
|
649
|
-
}
|
|
650
|
-
}
|
|
651
|
-
} else {
|
|
652
|
-
while (x1 != -1) {
|
|
653
|
-
if (n < to) {
|
|
654
|
-
if (n >= from) {
|
|
655
|
-
cols.push(data.slice(x0, x1))
|
|
656
|
-
rows.push(cols)
|
|
657
|
-
cols = []
|
|
658
|
-
c++;
|
|
659
|
-
}
|
|
660
|
-
} else {
|
|
661
|
-
n++;
|
|
662
|
-
break
|
|
663
|
-
}
|
|
664
|
-
x0 = x1 + lLbr
|
|
665
|
-
x1 = data.indexOf(linebreak, x0);
|
|
666
|
-
n++;
|
|
667
|
-
}
|
|
668
|
-
}
|
|
669
|
-
if (n >= to) {
|
|
670
|
-
break
|
|
671
|
-
}
|
|
672
|
-
m--;
|
|
673
|
-
if (x1 == -1) {
|
|
674
|
-
if (m) {
|
|
675
|
-
if (csv) {
|
|
676
|
-
bytesRead = 1
|
|
677
|
-
data = data.slice(x0)
|
|
678
|
-
xs += x0;
|
|
679
|
-
x0 = 0
|
|
680
|
-
while (bytesRead && x1 == -1) {
|
|
681
|
-
[bytesRead, data2] = getData(fd, bSize, offsFd)
|
|
682
|
-
offsFd += bytesRead;
|
|
683
|
-
data += data2;
|
|
684
|
-
x1 = data.indexOf(linebreak)
|
|
685
|
-
}
|
|
686
|
-
if (x1 == -1) x1 = data.length;
|
|
687
|
-
if (bytesRead) m++;
|
|
688
|
-
} else x1 = data.length;
|
|
689
|
-
} else x0 = x1 = data.length;
|
|
690
|
-
}
|
|
691
|
-
}
|
|
692
|
-
return {
|
|
693
|
-
get: {
|
|
694
|
-
rows: rows,
|
|
695
|
-
row_count: c
|
|
696
|
-
},
|
|
697
|
-
offs: [xs + x0, n],
|
|
698
|
-
row_count: c
|
|
699
|
-
};
|
|
700
|
-
}
|
|
701
|
-
|
|
702
|
-
function getRowsOffsF(data, from, info, sO_R) {
|
|
703
|
-
let {
|
|
704
|
-
header,
|
|
705
|
-
linebreak,
|
|
706
|
-
quote,
|
|
707
|
-
col,
|
|
708
|
-
d,
|
|
709
|
-
csv,
|
|
710
|
-
oJson
|
|
711
|
-
} = info
|
|
712
|
-
let lLbr = linebreak.length,
|
|
713
|
-
offsFd,
|
|
714
|
-
xs = x0 = sO_R,
|
|
715
|
-
x1;
|
|
716
|
-
let fd, bSize, bytesRead,
|
|
717
|
-
data2
|
|
718
|
-
if (csv) {
|
|
719
|
-
[fd, bSize] = csv
|
|
720
|
-
offsFd = data.length
|
|
721
|
-
bytesRead = 1
|
|
722
|
-
}
|
|
723
|
-
x1 = data.indexOf(linebreak, x0)
|
|
724
|
-
let m = 2,
|
|
725
|
-
n = 0,
|
|
726
|
-
c = 0;
|
|
727
|
-
let sbstr = sbstr2 = "",
|
|
728
|
-
rows = [],
|
|
729
|
-
cols = []
|
|
730
|
-
let q0, q1;
|
|
731
|
-
while (m) {
|
|
732
|
-
if (col) {
|
|
733
|
-
let lDlm = d.length;
|
|
734
|
-
if (oJson) {
|
|
735
|
-
let j;
|
|
736
|
-
cols = {}
|
|
737
|
-
if (quote) {
|
|
738
|
-
while (x1 != -1) {
|
|
739
|
-
if (n >= from) {
|
|
740
|
-
sbstr = data.slice(x0, x1)
|
|
741
|
-
q1 = sbstr.indexOf('"');
|
|
742
|
-
if (q1 != -1) {
|
|
743
|
-
y0 = 0, y1 = sbstr.indexOf(d);
|
|
744
|
-
j = 0;
|
|
745
|
-
while (y1 != -1) {
|
|
746
|
-
sbstr2 = sbstr.slice(y0, y1)
|
|
747
|
-
if (y1 < q1) {
|
|
748
|
-
cols[oJson[j]] = sbstr2
|
|
749
|
-
y0 = y1 + lDlm
|
|
750
|
-
y1 = sbstr.indexOf(d, y0);
|
|
751
|
-
} else {
|
|
752
|
-
z = y0;
|
|
753
|
-
q0 = q1 + lDlm
|
|
754
|
-
q1 = sbstr.indexOf('"', q0);
|
|
755
|
-
if (q1 != -1) {
|
|
756
|
-
y1 = sbstr.indexOf(d, q1 + lDlm);
|
|
757
|
-
q1 = sbstr.indexOf('"', y1 + lDlm);
|
|
758
|
-
if (q1 == -1) q1 = sbstr.length
|
|
759
|
-
} else {
|
|
760
|
-
y1 = sbstr.indexOf(d, q0);
|
|
761
|
-
}
|
|
762
|
-
}
|
|
763
|
-
j++;
|
|
764
|
-
}
|
|
765
|
-
} else {
|
|
766
|
-
y0 = 0, y1 = sbstr.indexOf(d);
|
|
767
|
-
j = 0;
|
|
768
|
-
while (y1 != -1) {
|
|
769
|
-
sbstr2 = sbstr.slice(y0, y1)
|
|
770
|
-
cols[oJson[j]] = sbstr2
|
|
771
|
-
y0 = y1 + lDlm
|
|
772
|
-
y1 = sbstr.indexOf(d, y0);
|
|
773
|
-
j++;
|
|
774
|
-
}
|
|
775
|
-
}
|
|
776
|
-
if (y0) {
|
|
777
|
-
cols[oJson[j]] = sbstr.slice(y0)
|
|
778
|
-
rows.push(cols)
|
|
779
|
-
}
|
|
780
|
-
cols = {}
|
|
781
|
-
c++;
|
|
782
|
-
}
|
|
783
|
-
x0 = x1 + lLbr
|
|
784
|
-
x1 = data.indexOf(linebreak, x0);
|
|
785
|
-
n++;
|
|
786
|
-
}
|
|
787
|
-
} else {
|
|
788
|
-
while (x1 != -1) {
|
|
789
|
-
if (n >= from) {
|
|
790
|
-
sbstr = data.slice(x0, x1)
|
|
791
|
-
y0 = 0, y1 = sbstr.indexOf(d);
|
|
792
|
-
j = 0;
|
|
793
|
-
while (y1 != -1) {
|
|
794
|
-
sbstr2 = sbstr.slice(y0, y1)
|
|
795
|
-
cols[oJson[j]] = sbstr2
|
|
796
|
-
y0 = y1 + lDlm
|
|
797
|
-
y1 = sbstr.indexOf(d, y0);
|
|
798
|
-
j++;
|
|
799
|
-
}
|
|
800
|
-
if (y0) {
|
|
801
|
-
cols[oJson[j]] = sbstr.slice(y0)
|
|
802
|
-
rows.push(cols)
|
|
803
|
-
}
|
|
804
|
-
cols = {}
|
|
805
|
-
c++;
|
|
806
|
-
}
|
|
807
|
-
x0 = x1 + lLbr
|
|
808
|
-
x1 = data.indexOf(linebreak, x0);
|
|
809
|
-
n++;
|
|
810
|
-
}
|
|
811
|
-
}
|
|
812
|
-
} else {
|
|
813
|
-
if (quote) {
|
|
814
|
-
while (x1 != -1) {
|
|
815
|
-
if (n >= from) {
|
|
816
|
-
sbstr = data.slice(x0, x1)
|
|
817
|
-
q1 = sbstr.indexOf('"');
|
|
818
|
-
if (q1 != -1) {
|
|
819
|
-
y0 = 0, y1 = sbstr.indexOf(d);
|
|
820
|
-
while (y1 != -1) {
|
|
821
|
-
sbstr2 = sbstr.slice(y0, y1)
|
|
822
|
-
if (y1 < q1) {
|
|
823
|
-
cols.push(sbstr2)
|
|
824
|
-
y0 = y1 + lDlm
|
|
825
|
-
y1 = sbstr.indexOf(d, y0);
|
|
826
|
-
} else {
|
|
827
|
-
z = y0;
|
|
828
|
-
q0 = q1 + lDlm
|
|
829
|
-
q1 = sbstr.indexOf('"', q0);
|
|
830
|
-
if (q1 != -1) {
|
|
831
|
-
y1 = sbstr.indexOf(d, q1 + lDlm);
|
|
832
|
-
q1 = sbstr.indexOf('"', y1 + lDlm);
|
|
833
|
-
if (q1 == -1) q1 = sbstr.length
|
|
834
|
-
} else {
|
|
835
|
-
y1 = sbstr.indexOf(d, q0);
|
|
836
|
-
}
|
|
837
|
-
}
|
|
838
|
-
}
|
|
839
|
-
} else {
|
|
840
|
-
y0 = 0, y1 = sbstr.indexOf(d);
|
|
841
|
-
while (y1 != -1) {
|
|
842
|
-
sbstr2 = sbstr.slice(y0, y1)
|
|
843
|
-
cols.push(sbstr2)
|
|
844
|
-
y0 = y1 + lDlm
|
|
845
|
-
y1 = sbstr.indexOf(d, y0);
|
|
846
|
-
}
|
|
847
|
-
}
|
|
848
|
-
if (y0) {
|
|
849
|
-
cols.push(sbstr.slice(y0))
|
|
850
|
-
rows.push(cols)
|
|
851
|
-
}
|
|
852
|
-
cols = []
|
|
853
|
-
c++;
|
|
854
|
-
}
|
|
855
|
-
x0 = x1 + lLbr
|
|
856
|
-
x1 = data.indexOf(linebreak, x0);
|
|
857
|
-
n++;
|
|
858
|
-
}
|
|
859
|
-
} else {
|
|
860
|
-
while (x1 != -1) {
|
|
861
|
-
if (n >= from) {
|
|
862
|
-
sbstr = data.slice(x0, x1)
|
|
863
|
-
y0 = 0, y1 = sbstr.indexOf(d);
|
|
864
|
-
while (y1 != -1) {
|
|
865
|
-
sbstr2 = sbstr.slice(y0, y1)
|
|
866
|
-
cols.push(sbstr2)
|
|
867
|
-
y0 = y1 + lDlm
|
|
868
|
-
y1 = sbstr.indexOf(d, y0);
|
|
869
|
-
}
|
|
870
|
-
if (y0) {
|
|
871
|
-
cols.push(sbstr.slice(y0))
|
|
872
|
-
rows.push(cols)
|
|
873
|
-
}
|
|
874
|
-
cols = []
|
|
875
|
-
c++;
|
|
876
|
-
}
|
|
877
|
-
x0 = x1 + lLbr
|
|
878
|
-
x1 = data.indexOf(linebreak, x0);
|
|
879
|
-
n++;
|
|
880
|
-
}
|
|
881
|
-
}
|
|
882
|
-
}
|
|
883
|
-
} else {
|
|
884
|
-
while (x1 != -1) {
|
|
885
|
-
if (n >= from) {
|
|
886
|
-
cols.push(data.slice(x0, x1))
|
|
887
|
-
rows.push(cols)
|
|
888
|
-
cols = []
|
|
889
|
-
c++;
|
|
890
|
-
}
|
|
891
|
-
x0 = x1 + lLbr
|
|
892
|
-
x1 = data.indexOf(linebreak, x0);
|
|
893
|
-
n++;
|
|
894
|
-
}
|
|
895
|
-
}
|
|
896
|
-
m--;
|
|
897
|
-
if (x1 == -1) {
|
|
898
|
-
if (m) {
|
|
899
|
-
if (csv) {
|
|
900
|
-
bytesRead = 1;
|
|
901
|
-
data = data.slice(x0)
|
|
902
|
-
xs += x0;
|
|
903
|
-
x0 = 0
|
|
904
|
-
while (bytesRead && x1 == -1) {
|
|
905
|
-
[bytesRead, data2] = getData(fd, bSize, offsFd)
|
|
906
|
-
offsFd += bytesRead;
|
|
907
|
-
data += data2;
|
|
908
|
-
x1 = data.indexOf(linebreak)
|
|
909
|
-
}
|
|
910
|
-
if (x1 == -1) x1 = data.length;
|
|
911
|
-
if (bytesRead) m++;
|
|
912
|
-
} else x1 = data.length;
|
|
913
|
-
} else x0 = x1 = data.length;
|
|
914
|
-
}
|
|
915
|
-
}
|
|
916
|
-
return {
|
|
917
|
-
get: {
|
|
918
|
-
rows: rows,
|
|
919
|
-
row_count: c
|
|
920
|
-
},
|
|
921
|
-
offs: [xs + x0, n],
|
|
922
|
-
row_count: c
|
|
923
|
-
};
|
|
924
|
-
}
|
|
925
|
-
|
|
926
|
-
function get() {
|
|
927
|
-
let t = Date.now()
|
|
928
|
-
let obj;
|
|
929
|
-
obj = getAllRows(this.data, this.info, this.starOffsRow)
|
|
930
|
-
return {
|
|
931
|
-
time: Date.now() - t + ' ms',
|
|
932
|
-
header: this.option.header ? this.hdr : this.option.header,
|
|
933
|
-
...obj.get
|
|
934
|
-
}
|
|
935
|
-
}
|
|
936
|
-
|
|
937
|
-
function chunk(chunk) {
|
|
938
|
-
let t = Date.now()
|
|
939
|
-
let obj;
|
|
940
|
-
if (chunk && Number.isInteger(chunk)) {
|
|
941
|
-
if (chunk >= 1) obj = getRowsChunk(this.data, chunk, this.info, this.offs_n)
|
|
942
|
-
else Err("The 'chunk' parameter must be greater than or equal to 1")
|
|
943
|
-
} else Err("The 'chunk' parameter must be an integer")
|
|
944
|
-
this.offs_n = obj.offs
|
|
945
|
-
return {
|
|
946
|
-
time: Date.now() - t + ' ms',
|
|
947
|
-
header: this.option.header ? this.hdr : this.option.header,
|
|
948
|
-
...obj.get
|
|
949
|
-
}
|
|
950
|
-
}
|
|
951
|
-
|
|
952
|
-
function rowOffset(from, to) {
|
|
953
|
-
let t = Date.now()
|
|
954
|
-
let obj;
|
|
955
|
-
if (Number.isInteger(from)) {
|
|
956
|
-
if (from >= 0) {
|
|
957
|
-
if (to == undefined) {
|
|
958
|
-
obj = getRowsOffsF(this.data, from, this.info, this.starOffsRow)
|
|
959
|
-
} else {
|
|
960
|
-
if (Number.isInteger(to)) {
|
|
961
|
-
if (to > 0) obj = getRowsOffsFT(this.data, from, to, this.info, this.starOffsRow)
|
|
962
|
-
else Err("The second parameter must be greater than or equal to 1")
|
|
963
|
-
} else Err("The second parameter must be an integer")
|
|
964
|
-
}
|
|
965
|
-
} else Err("The first parameter must be greater than or equal to zero")
|
|
966
|
-
} else Err("The first parameter must be an integer")
|
|
967
|
-
if (obj.row_count) this.offs_n = obj.offs
|
|
968
|
-
return {
|
|
969
|
-
time: Date.now() - t + ' ms',
|
|
970
|
-
header: this.option.header ? this.hdr : this.option.header,
|
|
971
|
-
...obj.get
|
|
972
|
-
}
|
|
973
|
-
}
|
|
974
|
-
|
|
975
|
-
function setRowOffset(rowOffs) {
|
|
976
|
-
if (rowOffs != undefined) {
|
|
977
|
-
if (Number.isInteger(rowOffs)) {
|
|
978
|
-
if (rowOffs >= 0) {
|
|
979
|
-
let lLbr = this.info[1].length
|
|
980
|
-
n = 0,
|
|
981
|
-
x0 = 0
|
|
982
|
-
x1 = this.data.indexOf(this.info[1])
|
|
983
|
-
let offs = this.data.length
|
|
984
|
-
while (x1 != -1) {
|
|
985
|
-
if (n == rowOffs) {
|
|
986
|
-
if (this.option.header) {
|
|
987
|
-
offs = x1 + lLbr
|
|
988
|
-
} else offs = x0
|
|
989
|
-
this.offs_n = [offs, n]
|
|
990
|
-
return [offs, n]
|
|
991
|
-
}
|
|
992
|
-
x0 = x1 + lLbr
|
|
993
|
-
x1 = this.data.indexOf(this.info[1], x0)
|
|
994
|
-
n++;
|
|
995
|
-
}
|
|
996
|
-
} else Err("The 'rowOffs' parameter must be greater than or equal to zero")
|
|
997
|
-
} else Err("The 'rowOffs' parameter must be an integer")
|
|
998
|
-
}
|
|
999
|
-
return false
|
|
1000
|
-
}
|
|
1001
|
-
|
|
1002
|
-
function resetOption(opt) {
|
|
1003
|
-
let option = {
|
|
1004
|
-
'header': true,
|
|
1005
|
-
'quote': false,
|
|
1006
|
-
'linebreak': '\r\n',
|
|
1007
|
-
'delimiter': ",",
|
|
1008
|
-
'json': false,
|
|
1009
|
-
"bufferSize": 1024 * 1024
|
|
1010
|
-
};
|
|
1011
|
-
if (opt != undefined) {
|
|
1012
|
-
if (typeof opt == 'object') {
|
|
1013
|
-
if ('header' in opt)
|
|
1014
|
-
if (typeof opt.header === "boolean") {
|
|
1015
|
-
this.option.header = opt.header;
|
|
1016
|
-
} else Err("The 'header' key in The second parameter must be 'boolean'")
|
|
1017
|
-
if ('quote' in opt)
|
|
1018
|
-
if (typeof opt.quote === "boolean") {
|
|
1019
|
-
this.option.quote = opt.quote;
|
|
1020
|
-
} else Err("The 'quote' key in The second parameter must be 'boolean'")
|
|
1021
|
-
if ('linebreak' in opt)
|
|
1022
|
-
if (typeof opt.linebreak === "string") {
|
|
1023
|
-
this.option.linebreak = opt.linebreak;
|
|
1024
|
-
} else Err("The 'linebreak' key in The second parameter must be 'string'")
|
|
1025
|
-
if ('delimiter' in opt)
|
|
1026
|
-
if (typeof opt.delimiter === "string" || (typeof opt.delimiter === "boolean" && !opt.delimiter)) {
|
|
1027
|
-
this.option.delimiter = opt.delimiter;
|
|
1028
|
-
if (opt.delimiter === "boolean") {
|
|
1029
|
-
this.column = false;
|
|
1030
|
-
}
|
|
1031
|
-
} else Err("The 'delimiter' key in The second parameter must be 'string' or false 'boolean'")
|
|
1032
|
-
if ('json' in opt)
|
|
1033
|
-
if (typeof opt.json === "boolean") {
|
|
1034
|
-
option.json = opt.json;
|
|
1035
|
-
} else Err("The 'json' key in The second parameter must be 'boolean'")
|
|
1036
|
-
if ('bufferSize' in opt) {
|
|
1037
|
-
if (Number.isInteger(opt.bufferSize)) {
|
|
1038
|
-
if (opt.bufferSize >= 1024) option.bufferSize = opt.bufferSize;
|
|
1039
|
-
else Err("The 'bufferSize' parameter must be greater than or equal to 1024")
|
|
1040
|
-
} else Err("The 'rowOffs' parameter must be an integer")
|
|
1041
|
-
}
|
|
1042
|
-
} else Err("The second parameter must be an object")
|
|
1043
|
-
} else this.option = option;
|
|
1044
|
-
let gc;
|
|
1045
|
-
if (this.fd) gc = getColumns(this.data, this.option, this.csv)
|
|
1046
|
-
else gc = getColumns(this.data, this.option)
|
|
1047
|
-
this.hdr = gc[0]
|
|
1048
|
-
this.starOffsRow = gc[1]
|
|
1049
|
-
this.offs_n = [this.starOffsRow, 0]
|
|
1050
|
-
this.oJson = (this.option.header && this.option.json) ? this.hdr : false;
|
|
1051
|
-
this.info = {
|
|
1052
|
-
header: this.option.header,
|
|
1053
|
-
linebreak: this.option.linebreak,
|
|
1054
|
-
quote: this.option.quote,
|
|
1055
|
-
col: this.column,
|
|
1056
|
-
d: this.option.delimiter,
|
|
1057
|
-
csv: this.csv,
|
|
1058
|
-
oJson: this.oJson
|
|
1059
|
-
}
|
|
1060
|
-
}
|
|
1061
|
-
|
|
1062
|
-
function getInfo() {
|
|
1063
|
-
return {
|
|
1064
|
-
offset: this.offs_n[0],
|
|
1065
|
-
rowOffset: this.offs_n[1],
|
|
1066
|
-
option: this.option
|
|
1067
|
-
}
|
|
1068
|
-
}
|
|
1069
|
-
|
|
1070
|
-
function offs_nRowwwwwwww(data, option, csv) {
|
|
1071
|
-
let n = 0,
|
|
1072
|
-
offs = 0,
|
|
1073
|
-
x0 = 0,
|
|
1074
|
-
x1;
|
|
1075
|
-
if (option.header) {
|
|
1076
|
-
if (csv) {
|
|
1077
|
-
let [fd, bSize] = csv
|
|
1078
|
-
let offsFd = 0
|
|
1079
|
-
let bytesRead = 1,
|
|
1080
|
-
data2;
|
|
1081
|
-
x1 = data.indexOf(option.linebreak)
|
|
1082
|
-
while (bytesRead && x1 == -1) {
|
|
1083
|
-
[bytesRead, data2] = getData(fd, bSize, offsFd)
|
|
1084
|
-
offsFd += bytesRead;
|
|
1085
|
-
data += data2;
|
|
1086
|
-
x1 = data.indexOf(option.linebreak)
|
|
1087
|
-
}
|
|
1088
|
-
} else x1 = data.indexOf(option.linebreak)
|
|
1089
|
-
if (x1 == -1) x0 = x1 = data.length;
|
|
1090
|
-
else x0 = x1 + option.linebreak.length
|
|
1091
|
-
}
|
|
1092
|
-
return [x0, n];
|
|
1093
|
-
}
|
|
1094
|
-
|
|
1095
|
-
function getColumns(data, option, csv) {
|
|
1096
|
-
let cols = false,
|
|
1097
|
-
x0 = 0
|
|
1098
|
-
if (option.header) {
|
|
1099
|
-
cols = []
|
|
1100
|
-
let lDlm = option.delimiter.length,
|
|
1101
|
-
x1;
|
|
1102
|
-
if (csv) {
|
|
1103
|
-
let [fd, bSize] = csv
|
|
1104
|
-
let offsFd = 0
|
|
1105
|
-
let bytesRead = 1,
|
|
1106
|
-
data2
|
|
1107
|
-
x1 = data.indexOf(option.linebreak)
|
|
1108
|
-
while (bytesRead && x1 == -1) {
|
|
1109
|
-
[bytesRead, data2] = getData(fd, bSize, offsFd)
|
|
1110
|
-
offsFd += bytesRead;
|
|
1111
|
-
data += data2;
|
|
1112
|
-
x1 = data.indexOf(option.linebreak)
|
|
1113
|
-
}
|
|
1114
|
-
} else x1 = data.indexOf(option.linebreak)
|
|
1115
|
-
if (x1 != -1) {
|
|
1116
|
-
let sbstr2, sbstr = data.slice(x0, x1)
|
|
1117
|
-
let y0 = 0,
|
|
1118
|
-
y1 = sbstr.indexOf(option.delimiter);
|
|
1119
|
-
while (y1 != -1) {
|
|
1120
|
-
sbstr2 = sbstr.slice(y0, y1)
|
|
1121
|
-
cols.push(sbstr2)
|
|
1122
|
-
y0 = y1 + lDlm
|
|
1123
|
-
y1 = sbstr.indexOf(option.delimiter, y0);
|
|
1124
|
-
}
|
|
1125
|
-
if (y0)
|
|
1126
|
-
cols.push(sbstr.slice(y0))
|
|
1127
|
-
}
|
|
1128
|
-
if (x1 == -1) x0 = x1 = data.length;
|
|
1129
|
-
else x0 = x1 + option.linebreak.length
|
|
1130
|
-
}
|
|
1131
|
-
return [cols, x0];
|
|
1132
|
-
}
|
|
1133
|
-
|
|
1134
|
-
function parseCsv(file_path, opt) {
|
|
1135
|
-
let data, fd, column = true,
|
|
1136
|
-
option = {
|
|
1137
|
-
'header': true,
|
|
1138
|
-
'quote': false,
|
|
1139
|
-
'linebreak': '\r\n',
|
|
1140
|
-
'delimiter': ",",
|
|
1141
|
-
'json': false,
|
|
1142
|
-
"bufferSize": 1024 * 1024
|
|
1143
|
-
};
|
|
1144
|
-
if (file_path != undefined) {
|
|
1145
|
-
if (fs.existsSync(file_path)) {
|
|
1146
|
-
fd = fs.openSync(file_path, 'r');
|
|
1147
|
-
let [bytesRead, data2] = getData(fd, option.bufferSize, 0)
|
|
1148
|
-
data = data2
|
|
1149
|
-
} else Err(`File "${file_path}" does not exists`);
|
|
1150
|
-
} else Err("The first parameter 'file_path' must be string")
|
|
1151
|
-
if (opt != undefined) {
|
|
1152
|
-
if (typeof opt == 'object') {
|
|
1153
|
-
if ('header' in opt)
|
|
1154
|
-
if (typeof opt.header === "boolean") {
|
|
1155
|
-
option.header = opt.header;
|
|
1156
|
-
} else Err("The 'header' key in The second parameter must be 'boolean'")
|
|
1157
|
-
if ('quote' in opt)
|
|
1158
|
-
if (typeof opt.quote === "boolean") {
|
|
1159
|
-
option.quote = opt.quote;
|
|
1160
|
-
} else Err("The 'quote' key in The second parameter must be 'boolean'")
|
|
1161
|
-
if ('linebreak' in opt)
|
|
1162
|
-
if (typeof opt.linebreak === "string") {
|
|
1163
|
-
option.linebreak = opt.linebreak;
|
|
1164
|
-
} else Err("The 'linebreak' key in The second parameter must be 'string'")
|
|
1165
|
-
if ('delimiter' in opt)
|
|
1166
|
-
if (typeof opt.delimiter === "string" || (typeof opt.delimiter === "boolean" && !opt.delimiter)) {
|
|
1167
|
-
option.delimiter = opt.delimiter;
|
|
1168
|
-
if (opt.delimiter === "boolean") {
|
|
1169
|
-
column = false;
|
|
1170
|
-
}
|
|
1171
|
-
} else Err("The 'delimiter' key in The second parameter must be 'string' or false 'boolean'")
|
|
1172
|
-
if ('json' in opt)
|
|
1173
|
-
if (typeof opt.json === "boolean") {
|
|
1174
|
-
option.json = opt.json;
|
|
1175
|
-
} else Err("The 'json' key in The second parameter must be 'boolean'")
|
|
1176
|
-
if ('bufferSize' in opt) {
|
|
1177
|
-
if (Number.isInteger(opt.bufferSize)) {
|
|
1178
|
-
if (opt.bufferSize >= 1024) option.bufferSize = opt.bufferSize;
|
|
1179
|
-
else Err("The 'bufferSize' parameter must be greater than or equal to 1024")
|
|
1180
|
-
} else Err("The 'rowOffs' parameter must be an integer")
|
|
1181
|
-
}
|
|
1182
|
-
} else Err("The second parameter must be an object")
|
|
1183
|
-
}
|
|
1184
|
-
return new class {
|
|
1185
|
-
constructor() {
|
|
1186
|
-
let gc;
|
|
1187
|
-
this.option = option
|
|
1188
|
-
this.data = data;
|
|
1189
|
-
this.column = column;
|
|
1190
|
-
this.csv = [fd, option.bufferSize];
|
|
1191
|
-
gc = getColumns(this.data, this.option, this.csv)
|
|
1192
|
-
this.hdr = gc[0]
|
|
1193
|
-
this.starOffsRow = gc[1]
|
|
1194
|
-
this.offs_n = [this.starOffsRow, 0]
|
|
1195
|
-
this.oJson = (this.option.header && this.option.json) ? this.hdr : false;
|
|
1196
|
-
this.info = {
|
|
1197
|
-
header: this.option.header,
|
|
1198
|
-
linebreak: this.option.linebreak,
|
|
1199
|
-
quote: this.option.quote,
|
|
1200
|
-
col: this.column,
|
|
1201
|
-
d: this.option.delimiter,
|
|
1202
|
-
csv: this.csv,
|
|
1203
|
-
oJson: this.oJson
|
|
1204
|
-
}
|
|
1205
|
-
}
|
|
1206
|
-
get = get
|
|
1207
|
-
chunk = chunk
|
|
1208
|
-
setRowOffset = setRowOffset
|
|
1209
|
-
rowOffset = rowOffset
|
|
1210
|
-
getInfo = getInfo
|
|
1211
|
-
header = () => this.hdr;
|
|
1212
|
-
};
|
|
1213
|
-
}
|
|
1214
|
-
|
|
1215
|
-
function parseText(text, opt) {
|
|
1216
|
-
let data, column = true,
|
|
1217
|
-
option = {
|
|
1218
|
-
'header': true,
|
|
1219
|
-
'quote': false,
|
|
1220
|
-
'linebreak': '\r\n',
|
|
1221
|
-
'delimiter': ",",
|
|
1222
|
-
'json': false
|
|
1223
|
-
};
|
|
1224
|
-
|
|
1225
|
-
if (text != undefined || typeof text != 'string') {
|
|
1226
|
-
data = text
|
|
1227
|
-
} else Err("The first parameter must be a file path string")
|
|
1228
|
-
if (opt != undefined) {
|
|
1229
|
-
if (typeof opt == 'object') {
|
|
1230
|
-
if ('header' in opt)
|
|
1231
|
-
if (typeof opt.header === "boolean") {
|
|
1232
|
-
option.header = opt.header;
|
|
1233
|
-
} else Err("The 'header' key in The second parameter must be 'boolean'")
|
|
1234
|
-
if ('quote' in opt)
|
|
1235
|
-
if (typeof opt.quote === "boolean") {
|
|
1236
|
-
option.quote = opt.quote;
|
|
1237
|
-
} else Err("The 'quote' key in The second parameter must be 'boolean'")
|
|
1238
|
-
if ('linebreak' in opt)
|
|
1239
|
-
if (typeof opt.linebreak === "string") {
|
|
1240
|
-
option.linebreak = opt.linebreak;
|
|
1241
|
-
} else Err("The 'linebreak' key in The second parameter must be 'string'")
|
|
1242
|
-
if ('delimiter' in opt)
|
|
1243
|
-
if (typeof opt.delimiter === "string" || (typeof opt.delimiter === "boolean" && !opt.delimiter)) {
|
|
1244
|
-
option.delimiter = opt.delimiter;
|
|
1245
|
-
if (opt.delimiter === "boolean") {
|
|
1246
|
-
this.column = false;
|
|
1247
|
-
}
|
|
1248
|
-
} else Err("The 'delimiter' key in The second parameter must be 'string' or false 'boolean'")
|
|
1249
|
-
if ('json' in opt)
|
|
1250
|
-
if (typeof opt.json === "boolean") {
|
|
1251
|
-
option.json = opt.json;
|
|
1252
|
-
} else Err("The 'json' key in The second parameter must be 'boolean'")
|
|
1253
|
-
} else Err("The second parameter must be an object")
|
|
1254
|
-
}
|
|
1255
|
-
return new class {
|
|
1256
|
-
constructor() {
|
|
1257
|
-
let gc;
|
|
1258
|
-
this.option = option
|
|
1259
|
-
this.data = data;
|
|
1260
|
-
this.column = column;
|
|
1261
|
-
this.csv = false;
|
|
1262
|
-
gc = getColumns(this.data, this.option)
|
|
1263
|
-
this.hdr = gc[0]
|
|
1264
|
-
this.starOffsRow = gc[1]
|
|
1265
|
-
this.offs_n = [this.starOffsRow, 0]
|
|
1266
|
-
this.oJson = (this.option.header && this.option.json) ? this.hdr : false;
|
|
1267
|
-
this.info = {
|
|
1268
|
-
header: this.option.header,
|
|
1269
|
-
linebreak: this.option.linebreak,
|
|
1270
|
-
quote: this.option.quote,
|
|
1271
|
-
col: this.column,
|
|
1272
|
-
d: this.option.delimiter,
|
|
1273
|
-
oJson: this.oJson
|
|
1274
|
-
}
|
|
1275
|
-
}
|
|
1276
|
-
get = get
|
|
1277
|
-
chunk = chunk
|
|
1278
|
-
setRowOffset = setRowOffset
|
|
1279
|
-
rowOffset = rowOffset
|
|
1280
|
-
resetOption = resetOption
|
|
1281
|
-
getInfo = getInfo
|
|
1282
|
-
header = () => this.hdr;
|
|
1283
|
-
};
|
|
1284
|
-
}
|
|
1285
|
-
module.exports = {
|
|
1286
|
-
parseText,
|
|
1287
|
-
parseCsv
|
|
1288
|
-
}
|
|
1
|
+
const fs = require("fs");
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Custom Error Handler
|
|
5
|
+
*/
|
|
6
|
+
function throwErr(msg) {
|
|
7
|
+
console.error(`[select-csv Error]: ${msg}`);
|
|
8
|
+
const err = new Error("");
|
|
9
|
+
const stack = err.stack.split('\n').slice(2).join('\n');
|
|
10
|
+
console.error(stack);
|
|
11
|
+
process.exit(1);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
class CsvParser {
|
|
15
|
+
constructor(data, option, isCsv, column, fd = null) {
|
|
16
|
+
this.data = data;
|
|
17
|
+
this.option = option;
|
|
18
|
+
this.isCsv = isCsv; // Boolean: true if file, false if text
|
|
19
|
+
this.column = column;
|
|
20
|
+
this.fd = fd;
|
|
21
|
+
this.bufferSize = option.bufferSize || 1024 * 1024;
|
|
22
|
+
|
|
23
|
+
// Initialize Header and Offsets
|
|
24
|
+
const gc = this._getColumns();
|
|
25
|
+
this.hdr = gc.columns;
|
|
26
|
+
this.startOffsRow = gc.offset;
|
|
27
|
+
|
|
28
|
+
// Internal state: [byteOffset, rowNumber]
|
|
29
|
+
this.currentPos = [this.startOffsRow, 0];
|
|
30
|
+
|
|
31
|
+
// JSON mapping
|
|
32
|
+
this.jsonKeys = (this.option.header && this.option.json) ? this.hdr : null;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Reads a chunk from file
|
|
37
|
+
*/
|
|
38
|
+
_readSync(offs) {
|
|
39
|
+
const buffer = Buffer.alloc(this.bufferSize);
|
|
40
|
+
const bytesRead = fs.readSync(this.fd, buffer, 0, this.bufferSize, offs);
|
|
41
|
+
return bytesRead > 0 ? [bytesRead, buffer.toString()] : [0, ''];
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Internal method to extract columns/header
|
|
46
|
+
*/
|
|
47
|
+
_getColumns() {
|
|
48
|
+
let columns = this.option.header ? [] : false;
|
|
49
|
+
let offset = 0;
|
|
50
|
+
let workingData = this.data;
|
|
51
|
+
|
|
52
|
+
if (this.option.header) {
|
|
53
|
+
let lineBreakPos = workingData.indexOf(this.option.linebreak);
|
|
54
|
+
|
|
55
|
+
// If file and linebreak not in initial buffer, read more
|
|
56
|
+
if (this.isCsv && lineBreakPos === -1) {
|
|
57
|
+
let fileOffs = 0;
|
|
58
|
+
let bytesRead = 1;
|
|
59
|
+
while (bytesRead > 0 && lineBreakPos === -1) {
|
|
60
|
+
const [read, moreData] = this._readSync(fileOffs);
|
|
61
|
+
bytesRead = read;
|
|
62
|
+
fileOffs += bytesRead;
|
|
63
|
+
workingData += moreData;
|
|
64
|
+
lineBreakPos = workingData.indexOf(this.option.linebreak);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
if (lineBreakPos !== -1) {
|
|
69
|
+
const headerLine = workingData.slice(0, lineBreakPos);
|
|
70
|
+
if (this.option.delimiter) {
|
|
71
|
+
columns = headerLine.split(this.option.delimiter);
|
|
72
|
+
} else {
|
|
73
|
+
columns = [headerLine];
|
|
74
|
+
}
|
|
75
|
+
offset = lineBreakPos + this.option.linebreak.length;
|
|
76
|
+
} else {
|
|
77
|
+
offset = workingData.length;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return { columns, offset };
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Main Parsing Logic
|
|
85
|
+
*/
|
|
86
|
+
_parseRows(chunkSize, fromRow = null, toRow = null) {
|
|
87
|
+
let rows = [];
|
|
88
|
+
let count = 0;
|
|
89
|
+
let currentIdx = 0; // Local index in current data string
|
|
90
|
+
let byteOffset = this.isCsv ? this.currentPos[0] : 0;
|
|
91
|
+
let rowTracker = this.isCsv ? this.currentPos[1] : 0;
|
|
92
|
+
|
|
93
|
+
let workingData = "";
|
|
94
|
+
|
|
95
|
+
// Setup initial data
|
|
96
|
+
if (this.isCsv) {
|
|
97
|
+
const [read, d] = this._readSync(byteOffset);
|
|
98
|
+
workingData = d;
|
|
99
|
+
} else {
|
|
100
|
+
workingData = this.data;
|
|
101
|
+
currentIdx = fromRow !== null ? 0 : this.currentPos[0];
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const { linebreak, delimiter, quote } = this.option;
|
|
105
|
+
const lbLen = linebreak.length;
|
|
106
|
+
|
|
107
|
+
// Target range logic
|
|
108
|
+
const isTargeted = fromRow !== null;
|
|
109
|
+
let n = isTargeted ? 0 : rowTracker;
|
|
110
|
+
|
|
111
|
+
while (true) {
|
|
112
|
+
let lbIdx = workingData.indexOf(linebreak, currentIdx);
|
|
113
|
+
|
|
114
|
+
// Handle file streaming if linebreak not found
|
|
115
|
+
if (lbIdx === -1 && this.isCsv) {
|
|
116
|
+
let [read, more] = this._readSync(byteOffset + workingData.length);
|
|
117
|
+
if (read > 0) {
|
|
118
|
+
workingData += more;
|
|
119
|
+
continue;
|
|
120
|
+
} else {
|
|
121
|
+
lbIdx = workingData.length; // End of file
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
if (lbIdx === -1) break;
|
|
126
|
+
|
|
127
|
+
// Check if we should process this row
|
|
128
|
+
const shouldProcess = isTargeted ? (n >= fromRow && n < (toRow || Infinity)) : true;
|
|
129
|
+
|
|
130
|
+
if (shouldProcess) {
|
|
131
|
+
let line = workingData.slice(currentIdx, lbIdx);
|
|
132
|
+
let rowData;
|
|
133
|
+
|
|
134
|
+
if (delimiter) {
|
|
135
|
+
// Basic split or logic can be expanded here for complex quotes
|
|
136
|
+
rowData = line.split(delimiter);
|
|
137
|
+
|
|
138
|
+
// JSON Transform
|
|
139
|
+
if (this.jsonKeys) {
|
|
140
|
+
let obj = {};
|
|
141
|
+
this.jsonKeys.forEach((key, i) => obj[key] = rowData[i] || "");
|
|
142
|
+
rowData = obj;
|
|
143
|
+
}
|
|
144
|
+
} else {
|
|
145
|
+
rowData = [line];
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
rows.push(rowData);
|
|
149
|
+
count++;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
currentIdx = lbIdx + lbLen;
|
|
153
|
+
n++;
|
|
154
|
+
|
|
155
|
+
// Break conditions
|
|
156
|
+
if (chunkSize && count >= chunkSize) break;
|
|
157
|
+
if (toRow && n >= toRow) break;
|
|
158
|
+
if (lbIdx === workingData.length) break;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// Update state
|
|
162
|
+
if (!isTargeted) {
|
|
163
|
+
this.currentPos = [byteOffset + currentIdx, n];
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
return { rows, row_count: count };
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
get() {
|
|
170
|
+
const t = Date.now();
|
|
171
|
+
const result = this._parseRows(null, 0);
|
|
172
|
+
return {
|
|
173
|
+
time: `${Date.now() - t} ms`,
|
|
174
|
+
header: this.option.header ? this.hdr : false,
|
|
175
|
+
...result
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
chunk(size) {
|
|
180
|
+
if (!Number.isInteger(size) || size < 1) throwErr("Chunk size must be an integer >= 1");
|
|
181
|
+
const t = Date.now();
|
|
182
|
+
const result = this._parseRows(size);
|
|
183
|
+
return {
|
|
184
|
+
time: `${Date.now() - t} ms`,
|
|
185
|
+
header: this.option.header ? this.hdr : false,
|
|
186
|
+
...result
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
rowOffset(from, to) {
|
|
191
|
+
if (!Number.isInteger(from) || from < 0) throwErr("Offset 'from' must be integer >= 0");
|
|
192
|
+
const t = Date.now();
|
|
193
|
+
const result = this._parseRows(null, from, to);
|
|
194
|
+
return {
|
|
195
|
+
time: `${Date.now() - t} ms`,
|
|
196
|
+
header: this.option.header ? this.hdr : false,
|
|
197
|
+
...result
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
getInfo() {
|
|
202
|
+
return {
|
|
203
|
+
offset: this.currentPos[0],
|
|
204
|
+
rowOffset: this.currentPos[1],
|
|
205
|
+
option: this.option
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
header() {
|
|
210
|
+
return this.hdr;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Factory functions
|
|
216
|
+
*/
|
|
217
|
+
function parseCsv(file_path, opt = {}) {
|
|
218
|
+
if (!file_path || typeof file_path !== 'string') throwErr("File path must be a string");
|
|
219
|
+
if (!fs.existsSync(file_path)) throwErr(`File "${file_path}" not found`);
|
|
220
|
+
|
|
221
|
+
const defaultOpt = {
|
|
222
|
+
header: true,
|
|
223
|
+
quote: false,
|
|
224
|
+
linebreak: '\r\n',
|
|
225
|
+
delimiter: ",",
|
|
226
|
+
json: false,
|
|
227
|
+
bufferSize: 1024 * 1024
|
|
228
|
+
};
|
|
229
|
+
const finalOpt = { ...defaultOpt, ...opt };
|
|
230
|
+
const fd = fs.openSync(file_path, 'r');
|
|
231
|
+
|
|
232
|
+
// Initial read for header
|
|
233
|
+
const buffer = Buffer.alloc(finalOpt.bufferSize);
|
|
234
|
+
fs.readSync(fd, buffer, 0, finalOpt.bufferSize, 0);
|
|
235
|
+
|
|
236
|
+
return new CsvParser(buffer.toString(), finalOpt, true, !!finalOpt.delimiter, fd);
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
function parseText(text, opt = {}) {
|
|
240
|
+
if (typeof text !== 'string') throwErr("Input text must be a string");
|
|
241
|
+
|
|
242
|
+
const defaultOpt = {
|
|
243
|
+
header: true,
|
|
244
|
+
quote: false,
|
|
245
|
+
linebreak: '\r\n',
|
|
246
|
+
delimiter: ",",
|
|
247
|
+
json: false
|
|
248
|
+
};
|
|
249
|
+
const finalOpt = { ...defaultOpt, ...opt };
|
|
250
|
+
|
|
251
|
+
return new CsvParser(text, finalOpt, false, !!finalOpt.delimiter);
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
module.exports = { parseCsv, parseText };
|