select-csv 1.0.3 → 1.1.4
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 +145 -21
- package/package.json +5 -2
- package/selectcsv.js +1 -1
package/README.md
CHANGED
|
@@ -65,9 +65,9 @@ parse = parseText(
|
|
|
65
65
|
const result = parse.get(); //Return all rows
|
|
66
66
|
/*
|
|
67
67
|
{
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
68
|
+
time:1,
|
|
69
|
+
header:["Index","User Id","First Name","Last Name","Sex"],
|
|
70
|
+
rows:[
|
|
71
71
|
["1","5f10e9D33fC5f2b","Sara","Mcguire","Female"],
|
|
72
72
|
["2","751cD1cbF77e005","Alisha","Hebert","Male"],
|
|
73
73
|
["3","DcEFDB2D2e62bF9","Gwendolyn","Sheppard","Male"],
|
|
@@ -79,7 +79,7 @@ const result = parse.get(); //Return all rows
|
|
|
79
79
|
["9","F563CcbFBfEcf5a","Emma","Robinson","Female"],
|
|
80
80
|
["10","f2dceFc00F62542","Pedro","Cordova","Male"]
|
|
81
81
|
],
|
|
82
|
-
|
|
82
|
+
row_count:10
|
|
83
83
|
}
|
|
84
84
|
*/
|
|
85
85
|
|
|
@@ -93,7 +93,7 @@ result = parse.chunk(c)
|
|
|
93
93
|
//The 'c' parameter must be an integer and greater than or equal to 1
|
|
94
94
|
|
|
95
95
|
//Examples:
|
|
96
|
-
result = parse.chunk(2) //Return row 0 and 1
|
|
96
|
+
result = parse.chunk(2) //Return row 0 and 1
|
|
97
97
|
/*
|
|
98
98
|
{
|
|
99
99
|
"Time": 0,
|
|
@@ -102,11 +102,11 @@ result = parse.chunk(2) //Return row 0 and 1
|
|
|
102
102
|
[ "1", "5f10e9D33fC5f2b", "Sara", "Mcguire", "Female" ],
|
|
103
103
|
[ "2", "751cD1cbF77e005", "Alisha", "Hebert", "Male" ]
|
|
104
104
|
],
|
|
105
|
-
"
|
|
105
|
+
"row_count:": 2
|
|
106
106
|
}
|
|
107
107
|
*/
|
|
108
108
|
|
|
109
|
-
result = parse.chunk(3) //Return row 2,3 and 4
|
|
109
|
+
result = parse.chunk(3) //Return row 2,3 and 4 (Get rows from last offset saved)
|
|
110
110
|
/*
|
|
111
111
|
{
|
|
112
112
|
"Time": 0,
|
|
@@ -116,11 +116,11 @@ result = parse.chunk(3) //Return row 2,3 and 4
|
|
|
116
116
|
[ "4", "C88661E02EEDA9e", "Kristine", "Mccann", "Female" ],
|
|
117
117
|
[ "5", "fafF1aBDebaB2a6", "Bobby", "Pittman", "Female" ]
|
|
118
118
|
],
|
|
119
|
-
"
|
|
119
|
+
"row_count:": 3
|
|
120
120
|
}
|
|
121
121
|
*/
|
|
122
122
|
|
|
123
|
-
result = parse.chunk(1) //Return row 5
|
|
123
|
+
result = parse.chunk(1) //Return row 5 (Get rows from last offset saved)
|
|
124
124
|
/*
|
|
125
125
|
{
|
|
126
126
|
"Time": 0,
|
|
@@ -128,7 +128,7 @@ result = parse.chunk(1) //Return row 5
|
|
|
128
128
|
"Rows": [
|
|
129
129
|
[ "6", "BdDb6C8Af309202", "Calvin", "Ramsey", "Female" ]
|
|
130
130
|
],
|
|
131
|
-
"
|
|
131
|
+
"row_count:": 1
|
|
132
132
|
}
|
|
133
133
|
*/
|
|
134
134
|
|
|
@@ -158,7 +158,7 @@ result = parse.rowOffset(6) //Returns all rows from the sixth row to the last ro
|
|
|
158
158
|
[ "9", "F563CcbFBfEcf5a", "Emma", "Robinson", "Female" ],
|
|
159
159
|
[ "10", "f2dceFc00F62542", "Pedro", "Cordova", "Male" ]
|
|
160
160
|
],
|
|
161
|
-
"
|
|
161
|
+
"row_count:": 4
|
|
162
162
|
}
|
|
163
163
|
*/
|
|
164
164
|
|
|
@@ -172,7 +172,7 @@ result = parse.rowOffset(5,8) //Returns all rows from 5th to 8th row
|
|
|
172
172
|
[ "7", "FCdfFf08196f633", "Collin", "Allison", "Male" ],
|
|
173
173
|
[ "8", "356279dAa0F7CbD", "Nicholas", "Branch", "Male" ]
|
|
174
174
|
],
|
|
175
|
-
"
|
|
175
|
+
"row_count:": 3
|
|
176
176
|
}
|
|
177
177
|
*/
|
|
178
178
|
|
|
@@ -192,7 +192,7 @@ result = parse.setRowOffset(5)
|
|
|
192
192
|
/*
|
|
193
193
|
[236,5]
|
|
194
194
|
*/
|
|
195
|
-
result = parse.chunk(1)
|
|
195
|
+
result = parse.chunk(1) // Get rows from last offset saved
|
|
196
196
|
/*
|
|
197
197
|
{
|
|
198
198
|
"Time": 0,
|
|
@@ -200,7 +200,7 @@ result = parse.chunk(1)
|
|
|
200
200
|
"Rows": [
|
|
201
201
|
[ "6", "BdDb6C8Af309202", "Calvin", "Ramsey", "Female" ]
|
|
202
202
|
],
|
|
203
|
-
"
|
|
203
|
+
"row_count:": 1
|
|
204
204
|
}
|
|
205
205
|
*/
|
|
206
206
|
|
|
@@ -209,7 +209,7 @@ result = parse.setRowOffset(20)
|
|
|
209
209
|
/*
|
|
210
210
|
false
|
|
211
211
|
*/
|
|
212
|
-
result = parse.chunk(1)
|
|
212
|
+
result = parse.chunk(1) // Get rows from last offset saved
|
|
213
213
|
/*
|
|
214
214
|
{
|
|
215
215
|
"Time": 0,
|
|
@@ -217,7 +217,7 @@ result = parse.chunk(1)
|
|
|
217
217
|
"Rows": [
|
|
218
218
|
[ "7", "FCdfFf08196f633", "Collin", "Allison", "Male" ]
|
|
219
219
|
],
|
|
220
|
-
"
|
|
220
|
+
"row_count:": 1
|
|
221
221
|
}
|
|
222
222
|
*/
|
|
223
223
|
|
|
@@ -231,9 +231,11 @@ result = parse.chunk(1)
|
|
|
231
231
|
'header': true,
|
|
232
232
|
'quote': false,
|
|
233
233
|
'linebreak': '\r\n',
|
|
234
|
-
'delimiter': ","
|
|
234
|
+
'delimiter': ",",
|
|
235
|
+
'bufferSize':1024*1024
|
|
235
236
|
}
|
|
236
237
|
// delimiter: (String: get rows containing columns, false: get lines without columns)
|
|
238
|
+
//bufferSize: It only works with a CSV file, which is the maximum number of characters that can be read at a time, the minimum value is 1024
|
|
237
239
|
```
|
|
238
240
|
|
|
239
241
|
* If you want to use specific option :
|
|
@@ -243,6 +245,7 @@ var option = {
|
|
|
243
245
|
'quote': true, /* or false */
|
|
244
246
|
'linebreak': '\n', /* '\n' or '\r' or any other string */
|
|
245
247
|
'delimiter': "," /* ';' or any other string or false */
|
|
248
|
+
'bufferSize':2000 /* It only works with a CSV file */
|
|
246
249
|
}
|
|
247
250
|
|
|
248
251
|
var parse;
|
|
@@ -269,7 +272,7 @@ result = parse.rowOffset(2)
|
|
|
269
272
|
"Rows": [
|
|
270
273
|
[ "2", "751cD1cbF77e005", "Alisha", "Hebert", "Male" ]
|
|
271
274
|
],
|
|
272
|
-
"
|
|
275
|
+
"row_count:": 1
|
|
273
276
|
}
|
|
274
277
|
*/
|
|
275
278
|
|
|
@@ -285,13 +288,13 @@ option = {
|
|
|
285
288
|
"Rows": [
|
|
286
289
|
[ "2,751cD1cbF77e005,Alisha,Hebert,Male" ] // No columns, just string (all line)
|
|
287
290
|
],
|
|
288
|
-
"
|
|
291
|
+
"row_count:": 1
|
|
289
292
|
}
|
|
290
293
|
*/
|
|
291
294
|
|
|
292
295
|
```
|
|
293
296
|
|
|
294
|
-
* If you want reset option after multiple uses of your code :
|
|
297
|
+
* If you want to reset option after multiple uses of your code :
|
|
295
298
|
```js
|
|
296
299
|
const option = { // Just an exapmle
|
|
297
300
|
'header': false,
|
|
@@ -304,7 +307,7 @@ parse.resetOption(option); // All saved values are erased and the object is rest
|
|
|
304
307
|
|
|
305
308
|
```
|
|
306
309
|
|
|
307
|
-
* If you want get information of your object :
|
|
310
|
+
* If you want to get information of your object :
|
|
308
311
|
```js
|
|
309
312
|
|
|
310
313
|
const result = parse.getInfo();
|
|
@@ -321,5 +324,126 @@ const result = parse.getInfo();
|
|
|
321
324
|
}
|
|
322
325
|
*/
|
|
323
326
|
|
|
327
|
+
```
|
|
328
|
+
|
|
329
|
+
* Examples of parsing a large CSV file:
|
|
330
|
+
(https://www.kaggle.com/datasets/zanjibar/100-million-data-csv)
|
|
331
|
+
|
|
332
|
+
|
|
333
|
+
```js
|
|
334
|
+
|
|
335
|
+
const parse = parseCsv('100-million-data.csv',{"header": false});
|
|
336
|
+
var result;
|
|
337
|
+
result = parse.chunk(100000)
|
|
338
|
+
/*
|
|
339
|
+
{
|
|
340
|
+
time: 222,
|
|
341
|
+
header: false,
|
|
342
|
+
rows: [
|
|
343
|
+
[ '198801', '1', '103', '100', '000000190', '0', '35843', '34353' ],
|
|
344
|
+
[ '198801', '1', '103', '100', '120991000', '0', '1590', '4154' ],
|
|
345
|
+
[ '198801', '1', '103', '100', '210390900', '0', '4500', '2565' ],
|
|
346
|
+
.
|
|
347
|
+
.
|
|
348
|
+
.
|
|
349
|
+
[ '198801', '1', '103', '100', '391590000', '0', '95000', '7850' ],
|
|
350
|
+
[ '198801', '1', '103', '100', '391620000', '0', '1000', '404' ],
|
|
351
|
+
[ '198801', '1', '103', '100', '391723000', '0', '545', '479' ],
|
|
352
|
+
[ '198801', '1', '103', '100', '391732100', '0', '24', '393' ],
|
|
353
|
+
[ '198801', '1', '103', '100', '391732900', '0', '60', '758' ],
|
|
354
|
+
[ '198801', '1', '103', '100', '391810100', '0', '1935', '1042' ],
|
|
355
|
+
[ '198801', '1', '103', '100', '391910200', '0', '510', '1303' ],
|
|
356
|
+
[ '198801', '1', '103', '100', '391910300', '0', '133', '379' ],
|
|
357
|
+
[ '198801', '1', '103', '100', '391990300', '0', '450', '1668' ],
|
|
358
|
+
[ '198801', '1', '103', '100', '391990500', '0', '942', '1721' ],
|
|
359
|
+
[ '198801', '1', '103', '100', '391990900', '0', '40', '235' ],
|
|
360
|
+
[ '198801', '1', '103', '100', '392030000', '0', '406', '652' ],
|
|
361
|
+
... 99900 more items
|
|
362
|
+
],
|
|
363
|
+
row_count: 100000
|
|
364
|
+
}
|
|
365
|
+
*/
|
|
324
366
|
|
|
367
|
+
result = parse.chunk(3) // Return row 100001,100002 and 100003 (Get rows from last offset saved)
|
|
368
|
+
/*
|
|
369
|
+
{
|
|
370
|
+
time: 1,
|
|
371
|
+
header: false,
|
|
372
|
+
rows: [
|
|
373
|
+
[ '198801', '1', '326', '500', '841330000', '90', '81', '246' ],
|
|
374
|
+
[ '198801', '1', '326', '500', '841510000', '0', '35', '1366' ],
|
|
375
|
+
[ '198801', '1', '326', '500', '841582100', '0', '6', '334' ]
|
|
376
|
+
],
|
|
377
|
+
row_count: 3
|
|
378
|
+
}
|
|
379
|
+
*/
|
|
380
|
+
|
|
381
|
+
const from = 1000*1000*30;
|
|
382
|
+
const to = from + 5;
|
|
383
|
+
result = parse.rowOffset(from,to)
|
|
384
|
+
/*
|
|
385
|
+
{
|
|
386
|
+
time: 3743,
|
|
387
|
+
header: false,
|
|
388
|
+
rows: [
|
|
389
|
+
[
|
|
390
|
+
'199804', '2',
|
|
391
|
+
'213', '502',
|
|
392
|
+
'848130000', '16035',
|
|
393
|
+
'746', '8380'
|
|
394
|
+
],
|
|
395
|
+
[ '199804', '2', '213', '502', '848140000', '168', '152', '1891' ],
|
|
396
|
+
[ '199804', '2', '213', '502', '848180010', '77', '404', '1366' ],
|
|
397
|
+
[ '199804', '2', '213', '502', '848190000', '0', '131', '570' ],
|
|
398
|
+
[ '199804', '2', '213', '502', '848230000', '300', '4', '882' ]
|
|
399
|
+
],
|
|
400
|
+
row_count: 5
|
|
401
|
+
}
|
|
402
|
+
*/
|
|
403
|
+
|
|
404
|
+
const from = 1000*1000*90;
|
|
405
|
+
const to = from + 4;
|
|
406
|
+
result = parse.rowOffset(from,to)
|
|
407
|
+
/*
|
|
408
|
+
{
|
|
409
|
+
time: 44126,
|
|
410
|
+
header: false,
|
|
411
|
+
rows: [
|
|
412
|
+
[ '201412', '1', '125', '400', '283525000', '0', '160000', '6492' ],
|
|
413
|
+
[ '201412', '1', '125', '400', '390740100', '0', '17500', '5579' ],
|
|
414
|
+
[ '201412', '1', '125', '400', '390950000', '0', '36000', '21423' ],
|
|
415
|
+
[ '201412', '1', '125', '400', '392329000', '0', '520', '1413' ]
|
|
416
|
+
],
|
|
417
|
+
row_count: 4
|
|
418
|
+
}
|
|
419
|
+
*/
|
|
420
|
+
|
|
421
|
+
result = parse.chunk(3) // Get rows from last offset saved ( row to,to+1 and to+2 )
|
|
422
|
+
/*
|
|
423
|
+
{
|
|
424
|
+
time: 29,
|
|
425
|
+
header: false,
|
|
426
|
+
rows: [
|
|
427
|
+
[ '201412', '1', '125', '400', '400932000', '0', '18', '526' ],
|
|
428
|
+
[ '201412', '1', '125', '400', '401110000', '173', '1735', '1197' ],
|
|
429
|
+
[ '201412', '1', '125', '400', '401120000', '133', '1707', '1099' ]
|
|
430
|
+
],
|
|
431
|
+
row_count: 3
|
|
432
|
+
}
|
|
433
|
+
*/
|
|
434
|
+
|
|
435
|
+
result = parse.getInfo() // Get all the information
|
|
436
|
+
/*
|
|
437
|
+
{
|
|
438
|
+
offset: 3599945660,
|
|
439
|
+
rowOffset: 90000008,
|
|
440
|
+
option: {
|
|
441
|
+
header: false,
|
|
442
|
+
quote: false,
|
|
443
|
+
linebreak: '\r\n',
|
|
444
|
+
delimiter: ',',
|
|
445
|
+
bufferSize: 1048576
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
*/
|
|
325
449
|
```
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "select-csv",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.4",
|
|
4
4
|
"description": "fastest, simplest and most powerful package of all existing libraries in npmjs. It converts .csv files into an array and even into lines. It contains two important functions parseCsv that handles a csv file, you only need a link to the file. And parseText deals with text, and they both have the same roles and and methods",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"csv",
|
|
@@ -29,5 +29,8 @@
|
|
|
29
29
|
},
|
|
30
30
|
"license": "CC-BY-ND",
|
|
31
31
|
"main": "selectcsv.js",
|
|
32
|
-
"scripts": {}
|
|
32
|
+
"scripts": {},
|
|
33
|
+
"dependencies": {
|
|
34
|
+
"select-csv": "^1.0.3"
|
|
35
|
+
}
|
|
33
36
|
}
|
package/selectcsv.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
const fs=require("fs");function getRowsChunk(e,t,i,r){let n,o,[s,f,h,l,a]=i,d=f.length,y=r[0],u=e.indexOf(f,y),p=2,b=0,c=sbstr2="",m=[],w=[];for(;p;){if(l){let i=a.length;if(h)for(;-1!=u;){if(c=e.slice(y,u),o=c.indexOf('"'),-1!=o)for(y0=0,y1=c.indexOf(a);-1!=y1;)sbstr2=c.slice(y0,y1),y1<o?(w.push(sbstr2),y0=y1+i,y1=c.indexOf(a,y0)):(z=y0,n=o+i,o=c.indexOf('"',n),-1!=o?(y1=c.indexOf(a,o+i),o=c.indexOf('"',y1+i),-1==o&&(o=c.length)):y1=c.indexOf(a,n));else for(y0=0,y1=c.indexOf(a);-1!=y1;)sbstr2=c.slice(y0,y1),w.push(sbstr2),y0=y1+i,y1=c.indexOf(a,y0);if(w.push(c.slice(y0)),m.push(w),w=[],b++,b>=t)break;y=u+d,u=e.indexOf(f,y)}else for(;-1!=u;){for(c=e.slice(y,u),y0=0,y1=c.indexOf(a);-1!=y1;)sbstr2=c.slice(y0,y1),w.push(sbstr2),y0=y1+i,y1=c.indexOf(a,y0);if(w.push(c.slice(y0)),m.push(w),w=[],b++,b>=t)break;y=u+d,u=e.indexOf(f,y)}}else for(;-1!=u&&(w.push(e.slice(y,u)),m.push(w),w=[],b++,!(b>=t));)y=u+d,u=e.indexOf(f,y);if(b>=t){y=u+d;break}p--,-1==u&&(p?u=e.length:y=u)}return{get:{Rows:m,Row_count:b},offs:[y,b+r[1]],Row_count:b}}function getAllRows(e,t,i){let r,n,[o,s,f,h,l]=t,a=s.length,d=i[0],y=e.indexOf(s,d),u=2,p=0,b=sbstr2="",c=[],m=[];for(;u;){if(h){let t=l.length;if(f)for(;-1!=y;){if(b=e.slice(d,y),n=b.indexOf('"'),-1!=n)for(y0=0,y1=b.indexOf(l);-1!=y1;)sbstr2=b.slice(y0,y1),y1<n?(m.push(sbstr2),y0=y1+t,y1=b.indexOf(l,y0)):(z=y0,r=n+t,n=b.indexOf('"',r),-1!=n?(y1=b.indexOf(l,n+t),n=b.indexOf('"',y1+t),-1==n&&(n=b.length)):y1=b.indexOf(l,r));else for(y0=0,y1=b.indexOf(l);-1!=y1;)sbstr2=b.slice(y0,y1),m.push(sbstr2),y0=y1+t,y1=b.indexOf(l,y0);m.push(b.slice(y0)),c.push(m),m=[],d=y+a,y=e.indexOf(s,d),p++}else for(;-1!=y;){for(b=e.slice(d,y),y0=0,y1=b.indexOf(l);-1!=y1;)sbstr2=b.slice(y0,y1),m.push(sbstr2),y0=y1+t,y1=b.indexOf(l,y0);m.push(b.slice(y0)),c.push(m),m=[],d=y+a,y=e.indexOf(s,d),p++}}else for(;-1!=y;)m.push(e.slice(d,y)),c.push(m),m=[],d=y+a,y=e.indexOf(s,d),p++;u--,-1==y&&(u?y=e.length:d=y)}return{get:{Rows:c,Row_count:p},offs:[d,p],Row_count:p}}function getRowsOffsFT(e,t,i,r){let n,[o,s,f,h,l]=r,a=s.length,d=0;o&&(n=e.indexOf(s),d=-1==n?e.length:n+a),n=e.indexOf(s,d);let y,u,p=2,b=0,c=0,m=sbstr2="",w=[],x=[];for(;p;){if(h){let r=l.length;if(f)for(;-1!=n;){if(b>=t){if(!(b<i)){b++;break}if(m=e.slice(d,n),u=m.indexOf('"'),-1!=u)for(y0=0,y1=m.indexOf(l);-1!=y1;)sbstr2=m.slice(y0,y1),y1<u?(x.push(sbstr2),y0=y1+r,y1=m.indexOf(l,y0)):(z=y0,y=u+r,u=m.indexOf('"',y),-1!=u?(y1=m.indexOf(l,u+r),u=m.indexOf('"',y1+r),-1==u&&(u=m.length)):y1=m.indexOf(l,y));else for(y0=0,y1=m.indexOf(l);-1!=y1;)sbstr2=m.slice(y0,y1),x.push(sbstr2),y0=y1+r,y1=m.indexOf(l,y0);x.push(m.slice(y0)),w.push(x),x=[],c++}d=n+a,n=e.indexOf(s,d),b++}else for(;-1!=n;){if(!(b<i)){b++;break}if(b>=t){for(m=e.slice(d,n),y0=0,y1=m.indexOf(l);-1!=y1;)sbstr2=m.slice(y0,y1),x.push(sbstr2),y0=y1+r,y1=m.indexOf(l,y0);x.push(m.slice(y0)),w.push(x),x=[],c++}d=n+a,n=e.indexOf(s,d),b++}}else for(;-1!=n;){if(!(b<i)){b++;break}b>=t&&(x.push(e.slice(d,n)),w.push(x),x=[],c++),d=n+a,n=e.indexOf(s,d),b++}if(b>=i){d=n+a;break}p--,-1==n&&(p?n=e.length:d=n)}return{get:{Rows:w,Row_count:c},offs:[d,b],Row_count:c}}function getRowsOffsF(e,t,i){let r,[n,o,s,f,h]=i,l=o.length,a=0;n&&(r=e.indexOf(o),a=-1==r?e.length:r+l),r=e.indexOf(o,a);let d,y,u=2,p=0,b=0,c=sbstr2="",m=[],w=[];for(;u;){if(f){let i=h.length;if(s)for(;-1!=r;){if(p>=t){if(c=e.slice(a,r),y=c.indexOf('"'),-1!=y)for(y0=0,y1=c.indexOf(h);-1!=y1;)sbstr2=c.slice(y0,y1),y1<y?(w.push(sbstr2),y0=y1+i,y1=c.indexOf(h,y0)):(z=y0,d=y+i,y=c.indexOf('"',d),-1!=y?(y1=c.indexOf(h,y+i),y=c.indexOf('"',y1+i),-1==y&&(y=c.length)):y1=c.indexOf(h,d));else for(y0=0,y1=c.indexOf(h);-1!=y1;)sbstr2=c.slice(y0,y1),w.push(sbstr2),y0=y1+i,y1=c.indexOf(h,y0);w.push(c.slice(y0)),m.push(w),w=[],b++}a=r+l,r=e.indexOf(o,a),p++}else for(;-1!=r;){if(p>=t){for(c=e.slice(a,r),y0=0,y1=c.indexOf(h);-1!=y1;)sbstr2=c.slice(y0,y1),w.push(sbstr2),y0=y1+i,y1=c.indexOf(h,y0);w.push(c.slice(y0)),m.push(w),w=[],b++}a=r+l,r=e.indexOf(o,a),p++}}else for(;-1!=r;)p>=t&&(w.push(e.slice(a,r)),m.push(w),w=[],b++),a=r+l,r=e.indexOf(o,a),p++;u--,-1==r&&(u?r=e.length:a=r)}return{get:{Rows:m,Row_count:b},offs:[r,p],Row_count:b}}function get(){let e,t=Date.now(),i=offs_nRow(this.data,this.option);return e=getAllRows(this.data,this.info,i),{Time:Date.now()-t,Header:this.option.header?this.header:this.option.header,...e.get}}function chunk(e){let t,i=Date.now();if(!e||!Number.isInteger(e))throw new Error("The 'chunk' parameter must be an integer");if(!(e>=1))throw new Error("The 'chunk' parameter must be greater than or equal to 1");return t=getRowsChunk(this.data,e,this.info,this.offs_n),this.offs_n=t.offs,{Time:Date.now()-i,Header:this.option.header?this.header:this.option.header,...t.get}}function rowOffset(e,t){let i,r=Date.now();if(!Number.isInteger(e))throw new Error("The first parameter must be an integer");if(!(e>0))throw new Error("The first parameter must be greater than or equal to zero");if(null==t)i=getRowsOffsF(this.data,e,this.info);else{if(!Number.isInteger(t))throw new Error("The second parameter must be an integer");if(!(t>0))throw new Error("The second parameter must be greater than or equal to 1");i=getRowsOffsFT(this.data,e,t,this.info)}return i.Row_count&&(this.offs_n=i.offs),{Time:Date.now()-r,Header:this.option.header?this.header:this.option.header,...i.get}}function setRowOffset(e){if(null!=e){if(!Number.isInteger(e))throw new Error("The 'rowOffs' parameter must be an integer");if(!(e>=0))throw new Error("The 'rowOffs' parameter must be greater than or equal to zero");{let t=this.info[1].length;n=0,x0=0,x1=this.data.indexOf(this.info[1]);let i=this.data.length;for(;-1!=x1;){if(n==e)return i=this.option.header?x1+t:x0,this.offs_n=[i,n],[i,n];x0=x1+t,x1=this.data.indexOf(this.info[1],x0),n++}}}return!1}function resetOption(e){if(null!=e){if("object"!=typeof e)throw new Error("The second parameter must be an object");if("header"in e){if("boolean"!=typeof e.header)throw new Error("The 'header' key in The second parameter must be 'boolean'");this.option.header=e.header}if("quote"in e){if("boolean"!=typeof e.quote)throw new Error("The 'quote' key in The second parameter must be 'boolean'");this.option.quote=e.quote}if("linebreak"in e){if("string"!=typeof e.linebreak)throw new Error("The 'linebreak' key in The second parameter must be 'string'");this.option.linebreak=e.linebreak}if("delimiter"in e){if("string"!=typeof e.delimiter&&("boolean"!=typeof e.delimiter||e.delimiter))throw new Error("The 'delimiter' key in The second parameter must be 'string' or false 'boolean'");this.option.delimiter=e.delimiter,"boolean"===e.delimiter&&(this.column=!1)}}else this.option={header:!0,quote:!1,linebreak:"\r\n",delimiter:","};this.offs_n=offs_nRow(this.data,this.option),this.header=getColumns(this.data,this.option),this.info=[this.option.header,this.option.linebreak,this.option.quote,this.column,this.option.delimiter]}function getInfo(){return{offset:this.offs_n[0],rowOffset:this.offs_n[1],option:this.option}}function offs_nRow(e,t){let i,r=0,n=0;return t.header&&(i=e.indexOf(t.linebreak),n=-1==i?i=e.length:i+t.linebreak.length,r++),[n,r]}function getColumns(e,t){let i=!1;if(t.header){i=[];let r=t.delimiter.length;if(x0=0,x1=e.indexOf(t.linebreak),-1!=x1){let n,o=e.slice(x0,x1),s=0,f=o.indexOf(t.delimiter);for(;-1!=f;)n=o.slice(s,f),i.push(n),s=f+r,f=o.indexOf(t.delimiter,s);i.push(o.slice(s))}}return i}function parseCsv(e,t){let i,r=!0,n={header:!0,quote:!1,linebreak:"\r\n",delimiter:","};if(null==e)throw new Error("The first parameter 'file_path' must be string");if(!fs.existsSync(e))throw new Error(`File "${e}" does not exists`);if(i=fs.readFileSync(e,"utf8"),null!=t){if("object"!=typeof t)throw new Error("The second parameter must be an object");if("header"in t){if("boolean"!=typeof t.header)throw new Error("The 'header' key in The second parameter must be 'boolean'");n.header=t.header}if("quote"in t){if("boolean"!=typeof t.quote)throw new Error("The 'quote' key in The second parameter must be 'boolean'");n.quote=t.quote}if("linebreak"in t){if("string"!=typeof t.linebreak)throw new Error("The 'linebreak' key in The second parameter must be 'string'");n.linebreak=t.linebreak}if("delimiter"in t){if("string"!=typeof t.delimiter&&("boolean"!=typeof t.delimiter||t.delimiter))throw new Error("The 'delimiter' key in The second parameter must be 'string' or false 'boolean'");n.delimiter=t.delimiter,"boolean"===t.delimiter&&(r=!1)}}return new class{constructor(){this.option=n,this.data=i,this.column=r,this.offs_n=offs_nRow(this.data,this.option),this.header=getColumns(this.data,this.option),this.info=[this.option.header,this.option.linebreak,this.option.quote,this.column,this.option.delimiter]}get=get;chunk=chunk;setRowOffset=setRowOffset;rowOffset=rowOffset;getInfo=getInfo}}function parseText(e,t){let i,r={header:!0,quote:!1,linebreak:"\r\n",delimiter:","};if(null==e&&"string"==typeof e)throw new Error("The first parameter must be a file path string");if(i=e,null!=t){if("object"!=typeof t)throw new Error("The second parameter must be an object");if("header"in t){if("boolean"!=typeof t.header)throw new Error("The 'header' key in The second parameter must be 'boolean'");r.header=t.header}if("quote"in t){if("boolean"!=typeof t.quote)throw new Error("The 'quote' key in The second parameter must be 'boolean'");r.quote=t.quote}if("linebreak"in t){if("string"!=typeof t.linebreak)throw new Error("The 'linebreak' key in The second parameter must be 'string'");r.linebreak=t.linebreak}if("delimiter"in t){if("string"!=typeof t.delimiter&&("boolean"!=typeof t.delimiter||t.delimiter))throw new Error("The 'delimiter' key in The second parameter must be 'string' or false 'boolean'");r.delimiter=t.delimiter,"boolean"===t.delimiter&&(this.column=!1)}}return new class{constructor(){this.option=r,this.data=i,this.column=true,this.offs_n=offs_nRow(this.data,this.option),this.header=getColumns(this.data,this.option),this.info=[this.option.header,this.option.linebreak,this.option.quote,this.column,this.option.delimiter]}get=get;chunk=chunk;setRowOffset=setRowOffset;rowOffset=rowOffset;resetOption=resetOption;getInfo=getInfo}}module.exports={parseText:parseText,parseCsv:parseCsv};
|
|
1
|
+
const fs=require("fs");function getData(e,t,i){let r,n=Buffer.alloc(t);return 0!==(r=fs.readSync(e,n,0,t,i))?[r,String(n)]:[0,""]}function getRowsChunk(e,t,i,r){let n,o,s,f,h,l,a,[d,u,y,x,b,p]=i,c=u.length;p?([f,h]=p,n=o=r[0],s=0,[l,a]=getData(f,h,n),n+=l,e=a):(o=0,s=r[0]);let m,w,g=e.indexOf(u,s),O=2,k=0,T=sbstr2="",E=[],q=[];for(;O;){if(x){let i=b.length;if(y)for(;-1!=g;){if(T=e.slice(s,g),w=T.indexOf('"'),-1!=w)for(y0=0,y1=T.indexOf(b);-1!=y1;)sbstr2=T.slice(y0,y1),y1<w?(q.push(sbstr2),y0=y1+i,y1=T.indexOf(b,y0)):(z=y0,m=w+i,w=T.indexOf('"',m),-1!=w?(y1=T.indexOf(b,w+i),w=T.indexOf('"',y1+i),-1==w&&(w=T.length)):y1=T.indexOf(b,m));else for(y0=0,y1=T.indexOf(b);-1!=y1;)sbstr2=T.slice(y0,y1),q.push(sbstr2),y0=y1+i,y1=T.indexOf(b,y0);if(q.push(T.slice(y0)),E.push(q),q=[],k++,k>=t)break;s=g+c,g=e.indexOf(u,s)}else for(;-1!=g;){for(T=e.slice(s,g),y0=0,y1=T.indexOf(b);-1!=y1;)sbstr2=T.slice(y0,y1),q.push(sbstr2),y0=y1+i,y1=T.indexOf(b,y0);if(q.push(T.slice(y0)),E.push(q),q=[],k++,k>=t)break;s=g+c,g=e.indexOf(u,s)}}else for(;-1!=g&&(q.push(e.slice(s,g)),E.push(q),q=[],k++,!(k>=t));)s=g+c,g=e.indexOf(u,s);if(k>=t){s=g+c;break}if(O--,-1==g)if(O)if(p){for(l=1,e=e.slice(s),o+=s,s=0;l&&-1==g;)[l,a]=getData(f,h,n),n+=l,g=(e+=a).indexOf(u);-1==g&&(g=e.length),l&&O++}else g=e.length;else s=g}return{get:{rows:E,row_count:k},offs:[o+s,k+r[1]],row_count:k}}function getAllRows(e,t){let i,r,n,o,s,f,[h,l,a,d,u,y]=t,x=l.length,b=x0=0;if(y&&([n,o]=y,i=e.length,s=1),h){if(r=e.indexOf(l),y)for(;s&&-1==r;)[s,f]=getData(n,o,i),i+=s,r=(e+=f).indexOf(l);x0=-1==r?e.length:r+x}r=e.indexOf(l,x0);let p,c,m=2,w=0,g=sbstr2="",O=[],k=[];for(;m;){if(d){let t=u.length;if(a)for(;-1!=r;){if(g=e.slice(x0,r),c=g.indexOf('"'),-1!=c)for(y0=0,y1=g.indexOf(u);-1!=y1;)sbstr2=g.slice(y0,y1),y1<c?(k.push(sbstr2),y0=y1+t,y1=g.indexOf(u,y0)):(z=y0,p=c+t,c=g.indexOf('"',p),-1!=c?(y1=g.indexOf(u,c+t),c=g.indexOf('"',y1+t),-1==c&&(c=g.length)):y1=g.indexOf(u,p));else for(y0=0,y1=g.indexOf(u);-1!=y1;)sbstr2=g.slice(y0,y1),k.push(sbstr2),y0=y1+t,y1=g.indexOf(u,y0);k.push(g.slice(y0)),O.push(k),k=[],x0=r+x,r=e.indexOf(l,x0),w++}else for(;-1!=r;){for(g=e.slice(x0,r),y0=0,y1=g.indexOf(u);-1!=y1;)sbstr2=g.slice(y0,y1),k.push(sbstr2),y0=y1+t,y1=g.indexOf(u,y0);k.push(g.slice(y0)),O.push(k),k=[],x0=r+x,r=e.indexOf(l,x0),w++}}else for(;-1!=r;)k.push(e.slice(x0,r)),O.push(k),k=[],x0=r+x,r=e.indexOf(l,x0),w++;if(m--,-1==r)if(m)if(y){for(s=1,e=e.slice(x0),b+=x0,x0=0;s&&-1==r;)[s,f]=getData(n,o,i),i+=s,r=(e+=f).indexOf(l);-1==r&&(r=e.length),s&&m++}else r=e.length;else x0=r}return{get:{rows:O,row_count:w},offs:[b+x0,w],row_count:w}}function getRowsOffsFT(e,t,i,r){let n,o,s,f,h,l,[a,d,u,y,x,b]=r,p=d.length,c=x0=0;if(b&&([s,f]=b,n=e.length,h=1),a){if(o=e.indexOf(d),b)for(;h&&-1==o;)[h,l]=getData(s,f,n),n+=h,o=(e+=l).indexOf(d);x0=-1==o?e.length:o+p}o=e.indexOf(d,x0);let m,w,g=2,O=0,k=0,T=sbstr2="",E=[],q=[];for(;g;){if(y){let r=x.length;if(u)for(;-1!=o;){if(O>=t){if(!(O<i)){O++;break}if(T=e.slice(x0,o),w=T.indexOf('"'),-1!=w)for(y0=0,y1=T.indexOf(x);-1!=y1;)sbstr2=T.slice(y0,y1),y1<w?(q.push(sbstr2),y0=y1+r,y1=T.indexOf(x,y0)):(z=y0,m=w+r,w=T.indexOf('"',m),-1!=w?(y1=T.indexOf(x,w+r),w=T.indexOf('"',y1+r),-1==w&&(w=T.length)):y1=T.indexOf(x,m));else for(y0=0,y1=T.indexOf(x);-1!=y1;)sbstr2=T.slice(y0,y1),q.push(sbstr2),y0=y1+r,y1=T.indexOf(x,y0);q.push(T.slice(y0)),E.push(q),q=[],k++}x0=o+p,o=e.indexOf(d,x0),O++}else for(;-1!=o;){if(!(O<i)){O++;break}if(O>=t){for(T=e.slice(x0,o),y0=0,y1=T.indexOf(x);-1!=y1;)sbstr2=T.slice(y0,y1),q.push(sbstr2),y0=y1+r,y1=T.indexOf(x,y0);q.push(T.slice(y0)),E.push(q),q=[],k++}x0=o+p,o=e.indexOf(d,x0),O++}}else for(;-1!=o;){if(!(O<i)){O++;break}O>=t&&(q.push(e.slice(x0,o)),E.push(q),q=[],k++),x0=o+p,o=e.indexOf(d,x0),O++}if(O>=i)break;if(g--,-1==o)if(g)if(b){for(h=1,e=e.slice(x0),c+=x0,x0=0;h&&-1==o;)[h,l]=getData(s,f,n),n+=h,o=(e+=l).indexOf(d);-1==o&&(o=e.length),h&&g++}else o=e.length;else x0=o}return{get:{rows:E,row_count:k},offs:[c+x0,O],row_count:k}}function getRowsOffsF(e,t,i){let r,n,o,s,f,h,[l,a,d,u,y,x]=i,b=a.length,p=x0=0;if(x&&([o,s]=x,r=e.length,f=1),l){if(n=e.indexOf(a),x)for(;f&&-1==n;)[f,h]=getData(o,s,r),r+=f,n=(e+=h).indexOf(a);x0=-1==n?e.length:n+b}n=e.indexOf(a,x0);let c,m,w=2,g=0,O=0,k=sbstr2="",T=[],E=[];for(;w;){if(u){let i=y.length;if(d)for(;-1!=n;){if(g>=t){if(k=e.slice(x0,n),m=k.indexOf('"'),-1!=m)for(y0=0,y1=k.indexOf(y);-1!=y1;)sbstr2=k.slice(y0,y1),y1<m?(E.push(sbstr2),y0=y1+i,y1=k.indexOf(y,y0)):(z=y0,c=m+i,m=k.indexOf('"',c),-1!=m?(y1=k.indexOf(y,m+i),m=k.indexOf('"',y1+i),-1==m&&(m=k.length)):y1=k.indexOf(y,c));else for(y0=0,y1=k.indexOf(y);-1!=y1;)sbstr2=k.slice(y0,y1),E.push(sbstr2),y0=y1+i,y1=k.indexOf(y,y0);E.push(k.slice(y0)),T.push(E),E=[],O++}x0=n+b,n=e.indexOf(a,x0),g++}else for(;-1!=n;){if(g>=t){for(k=e.slice(x0,n),y0=0,y1=k.indexOf(y);-1!=y1;)sbstr2=k.slice(y0,y1),E.push(sbstr2),y0=y1+i,y1=k.indexOf(y,y0);E.push(k.slice(y0)),T.push(E),E=[],O++}x0=n+b,n=e.indexOf(a,x0),g++}}else for(;-1!=n;)g>=t&&(E.push(e.slice(x0,n)),T.push(E),E=[],O++),x0=n+b,n=e.indexOf(a,x0),g++;if(w--,-1==n)if(w)if(x){for(f=1,e=e.slice(x0),p+=x0,x0=0;f&&-1==n;)[f,h]=getData(o,s,r),r+=f,n=(e+=h).indexOf(a);-1==n&&(n=e.length),f&&w++}else n=e.length;else x0=n}return{get:{rows:T,row_count:O},offs:[p+x0,g],row_count:O}}function get(){let e,t=Date.now();return e=getAllRows(this.data,this.info),{time:Date.now()-t,header:this.option.header?this.header:this.option.header,...e.get}}function chunk(e){let t,i=Date.now();if(!e||!Number.isInteger(e))throw new Error("The 'chunk' parameter must be an integer");if(!(e>=1))throw new Error("The 'chunk' parameter must be greater than or equal to 1");return t=getRowsChunk(this.data,e,this.info,this.offs_n),this.offs_n=t.offs,{time:Date.now()-i,header:this.option.header?this.header:this.option.header,...t.get}}function rowOffset(e,t){let i,r=Date.now();if(!Number.isInteger(e))throw new Error("The first parameter must be an integer");if(!(e>0))throw new Error("The first parameter must be greater than or equal to zero");if(null==t)i=getRowsOffsF(this.data,e,this.info);else{if(!Number.isInteger(t))throw new Error("The second parameter must be an integer");if(!(t>0))throw new Error("The second parameter must be greater than or equal to 1");i=getRowsOffsFT(this.data,e,t,this.info)}return i.row_count&&(this.offs_n=i.offs),{time:Date.now()-r,header:this.option.header?this.header:this.option.header,...i.get}}function setRowOffset(e){if(null!=e){if(!Number.isInteger(e))throw new Error("The 'rowOffs' parameter must be an integer");if(!(e>=0))throw new Error("The 'rowOffs' parameter must be greater than or equal to zero");{let t=this.info[1].length;n=0,x0=0,x1=this.data.indexOf(this.info[1]);let i=this.data.length;for(;-1!=x1;){if(n==e)return i=this.option.header?x1+t:x0,this.offs_n=[i,n],[i,n];x0=x1+t,x1=this.data.indexOf(this.info[1],x0),n++}}}return!1}function resetOption(e){if(null!=e){if("object"!=typeof e)throw new Error("The second parameter must be an object");if("header"in e){if("boolean"!=typeof e.header)throw new Error("The 'header' key in The second parameter must be 'boolean'");this.option.header=e.header}if("quote"in e){if("boolean"!=typeof e.quote)throw new Error("The 'quote' key in The second parameter must be 'boolean'");this.option.quote=e.quote}if("linebreak"in e){if("string"!=typeof e.linebreak)throw new Error("The 'linebreak' key in The second parameter must be 'string'");this.option.linebreak=e.linebreak}if("delimiter"in e){if("string"!=typeof e.delimiter&&("boolean"!=typeof e.delimiter||e.delimiter))throw new Error("The 'delimiter' key in The second parameter must be 'string' or false 'boolean'");this.option.delimiter=e.delimiter,"boolean"===e.delimiter&&(this.column=!1)}}else this.option={header:!0,quote:!1,linebreak:"\r\n",delimiter:","};this.offs_n=[0,0],this.header=getColumns(this.data,this.option),this.info=[this.option.header,this.option.linebreak,this.option.quote,this.column,this.option.delimiter]}function getInfo(){return{offset:this.offs_n[0],rowOffset:this.offs_n[1],option:this.option}}function offs_nRowwwwwwww(e,t,i){let r,n=0,o=0;if(t.header){if(i){let n,[o,s]=i,f=0,h=1;for(r=e.indexOf(t.linebreak);h&&-1==r;)[h,n]=getData(o,s,f),f+=h,r=(e+=n).indexOf(t.linebreak)}else r=e.indexOf(t.linebreak);o=-1==r?r=e.length:r+t.linebreak.length,n++}return[o,n]}function getColumns(e,t,i){let r=!1;if(t.header){r=[];let n,o=t.delimiter.length,s=0;if(i){let r,[o,s]=i,f=0,h=1;for(n=e.indexOf(t.linebreak);h&&-1==n;)[h,r]=getData(o,s,f),f+=h,n=(e+=r).indexOf(t.linebreak)}else n=e.indexOf(t.linebreak);if(-1!=n){let i,f=e.slice(s,n),h=0,l=f.indexOf(t.delimiter);for(;-1!=l;)i=f.slice(h,l),r.push(i),h=l+o,l=f.indexOf(t.delimiter,h);r.push(f.slice(h))}}return r}function parseCsv(e,t){let i,r,n=!0,o={header:!0,quote:!1,linebreak:"\r\n",delimiter:",",bufferSize:1048576};if(null==e)throw new Error("The first parameter 'file_path' must be string");if(!fs.existsSync(e))throw new Error(`File "${e}" does not exists`);{r=fs.openSync(e,"r");let[t,n]=getData(r,o.bufferSize,0);i=n}if(null!=t){if("object"!=typeof t)throw new Error("The second parameter must be an object");if("header"in t){if("boolean"!=typeof t.header)throw new Error("The 'header' key in The second parameter must be 'boolean'");o.header=t.header}if("quote"in t){if("boolean"!=typeof t.quote)throw new Error("The 'quote' key in The second parameter must be 'boolean'");o.quote=t.quote}if("linebreak"in t){if("string"!=typeof t.linebreak)throw new Error("The 'linebreak' key in The second parameter must be 'string'");o.linebreak=t.linebreak}if("delimiter"in t){if("string"!=typeof t.delimiter&&("boolean"!=typeof t.delimiter||t.delimiter))throw new Error("The 'delimiter' key in The second parameter must be 'string' or false 'boolean'");o.delimiter=t.delimiter,"boolean"===t.delimiter&&(n=!1)}if("bufferSize"in t){if(!Number.isInteger(t.bufferSize))throw new Error("The 'rowOffs' parameter must be an integer");if(!(t.bufferSize>=1024))throw new Error("The 'bufferSize' parameter must be greater than or equal to 1024");o.bufferSize=t.bufferSize}}return new class{constructor(){this.option=o,this.data=i,this.column=n,this.offs_n=[0,0],this.header=getColumns(this.data,this.option,[r,o.bufferSize]),this.info=[this.option.header,this.option.linebreak,this.option.quote,this.column,this.option.delimiter,[r,o.bufferSize]]}get=get;chunk=chunk;setRowOffset=setRowOffset;rowOffset=rowOffset;getInfo=getInfo}}function parseText(e,t){let i,r={header:!0,quote:!1,linebreak:"\r\n",delimiter:","};if(null==e&&"string"==typeof e)throw new Error("The first parameter must be a file path string");if(i=e,null!=t){if("object"!=typeof t)throw new Error("The second parameter must be an object");if("header"in t){if("boolean"!=typeof t.header)throw new Error("The 'header' key in The second parameter must be 'boolean'");r.header=t.header}if("quote"in t){if("boolean"!=typeof t.quote)throw new Error("The 'quote' key in The second parameter must be 'boolean'");r.quote=t.quote}if("linebreak"in t){if("string"!=typeof t.linebreak)throw new Error("The 'linebreak' key in The second parameter must be 'string'");r.linebreak=t.linebreak}if("delimiter"in t){if("string"!=typeof t.delimiter&&("boolean"!=typeof t.delimiter||t.delimiter))throw new Error("The 'delimiter' key in The second parameter must be 'string' or false 'boolean'");r.delimiter=t.delimiter,"boolean"===t.delimiter&&(this.column=!1)}}return new class{constructor(){this.option=r,this.data=i,this.column=true,this.offs_n=[0,0],this.header=getColumns(this.data,this.option),this.info=[this.option.header,this.option.linebreak,this.option.quote,this.column,this.option.delimiter]}get=get;chunk=chunk;setRowOffset=setRowOffset;rowOffset=rowOffset;resetOption=resetOption;getInfo=getInfo}}module.exports={parseText:parseText,parseCsv:parseCsv};
|