select-csv 1.1.7 → 1.1.9
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 +57 -14
- package/package.json +1 -1
- package/selectcsv.js +1 -1
package/README.md
CHANGED
|
@@ -13,7 +13,7 @@ It is the fastest, simplest and most powerful package of all existing libraries
|
|
|
13
13
|
- Uses row offset (get rows from line x to line x+n)
|
|
14
14
|
- Returns rows (with columns) or lines (without columns)
|
|
15
15
|
- No external dependencies
|
|
16
|
-
- Flexible with lots of options (header, quote, line break, delimiter)
|
|
16
|
+
- Flexible with lots of options (header, quote, line break, delimiter, bufferSize in CSV file, Rows in an array or json array)
|
|
17
17
|
- One of the only parsers that correctly handles line-breaks and quotations
|
|
18
18
|
|
|
19
19
|
select-csv has **no dependencies** .
|
|
@@ -65,7 +65,7 @@ parse = parseText(
|
|
|
65
65
|
const result = parse.get(); //Return all rows
|
|
66
66
|
/*
|
|
67
67
|
{
|
|
68
|
-
time:1,
|
|
68
|
+
time:1 ms,
|
|
69
69
|
header:["Index","User Id","First Name","Last Name","Sex"],
|
|
70
70
|
rows:[
|
|
71
71
|
["1","5f10e9D33fC5f2b","Sara","Mcguire","Female"],
|
|
@@ -232,6 +232,7 @@ result = parse.chunk(1) // Get rows from last offset saved
|
|
|
232
232
|
'quote': false,
|
|
233
233
|
'linebreak': '\r\n',
|
|
234
234
|
'delimiter': ",",
|
|
235
|
+
'json': false,
|
|
235
236
|
'bufferSize':1024*1024
|
|
236
237
|
}
|
|
237
238
|
// delimiter: (String: get rows containing columns, false: get lines without columns)
|
|
@@ -241,11 +242,12 @@ result = parse.chunk(1) // Get rows from last offset saved
|
|
|
241
242
|
* If you want to use specific option :
|
|
242
243
|
```js
|
|
243
244
|
var option = {
|
|
244
|
-
'header': false,
|
|
245
|
-
'quote': true,
|
|
245
|
+
'header': false, /* or true */
|
|
246
|
+
'quote': true, /* or false */
|
|
246
247
|
'linebreak': '\n', /* '\n' or '\r' or any other string */
|
|
247
|
-
'delimiter': ","
|
|
248
|
-
'
|
|
248
|
+
'delimiter': "," /* ';' or any other string or false */
|
|
249
|
+
'json': false /* or true */
|
|
250
|
+
'bufferSize':2000 /* It only works with a CSV file */
|
|
249
251
|
}
|
|
250
252
|
|
|
251
253
|
var parse;
|
|
@@ -260,9 +262,6 @@ parse = parseText(
|
|
|
260
262
|
3,DcEFDB2D2e62bF9,Gwendolyn,Sheppard,Male`
|
|
261
263
|
, option);
|
|
262
264
|
|
|
263
|
-
option = {
|
|
264
|
-
'header': false,
|
|
265
|
-
}
|
|
266
265
|
|
|
267
266
|
result = parse.rowOffset(2)
|
|
268
267
|
/*
|
|
@@ -305,6 +304,50 @@ const option = { // Just an exapmle
|
|
|
305
304
|
parse.resetOption(option); // All saved values are erased and the object is restared again
|
|
306
305
|
|
|
307
306
|
|
|
307
|
+
```
|
|
308
|
+
|
|
309
|
+
* If you want to rows as json array :
|
|
310
|
+
```js
|
|
311
|
+
// 'header' and 'json' must be true if you want to get rows as a json array
|
|
312
|
+
const option = {
|
|
313
|
+
'header': true,
|
|
314
|
+
'json': true
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
|
|
318
|
+
const result = parse.chunk(3)
|
|
319
|
+
/*
|
|
320
|
+
{
|
|
321
|
+
time: 0 ms,
|
|
322
|
+
header: [ 'Index', 'User Id', 'First Name', 'Last Name', 'Sex' ],
|
|
323
|
+
rows: [
|
|
324
|
+
{
|
|
325
|
+
Index: '1',
|
|
326
|
+
'User Id': '5f10e9D33fC5f2b',
|
|
327
|
+
'First Name': 'Sara',
|
|
328
|
+
'Last Name': 'Mcguire',
|
|
329
|
+
Sex: 'Female'
|
|
330
|
+
},
|
|
331
|
+
{
|
|
332
|
+
Index: '2',
|
|
333
|
+
'User Id': '751cD1cbF77e005',
|
|
334
|
+
'First Name': 'Alisha',
|
|
335
|
+
'Last Name': 'Hebert',
|
|
336
|
+
Sex: 'Male'
|
|
337
|
+
},
|
|
338
|
+
{
|
|
339
|
+
Index: '3',
|
|
340
|
+
'User Id': 'DcEFDB2D2e62bF9',
|
|
341
|
+
'First Name': 'Gwendolyn',
|
|
342
|
+
'Last Name': 'Sheppard',
|
|
343
|
+
Sex: 'Male'
|
|
344
|
+
}
|
|
345
|
+
],
|
|
346
|
+
row_count: 3
|
|
347
|
+
}
|
|
348
|
+
*/
|
|
349
|
+
|
|
350
|
+
|
|
308
351
|
```
|
|
309
352
|
|
|
310
353
|
* If you want to get information of your object :
|
|
@@ -337,7 +380,7 @@ var result;
|
|
|
337
380
|
result = parse.chunk(100000)
|
|
338
381
|
/*
|
|
339
382
|
{
|
|
340
|
-
time: 222,
|
|
383
|
+
time: 222 ms,
|
|
341
384
|
header: false,
|
|
342
385
|
rows: [
|
|
343
386
|
[ '198801', '1', '103', '100', '000000190', '0', '35843', '34353' ],
|
|
@@ -367,7 +410,7 @@ result = parse.chunk(100000)
|
|
|
367
410
|
result = parse.chunk(3) // Return row 100001,100002 and 100003 (Get rows from last offset saved)
|
|
368
411
|
/*
|
|
369
412
|
{
|
|
370
|
-
time: 1,
|
|
413
|
+
time: 1 ms,
|
|
371
414
|
header: false,
|
|
372
415
|
rows: [
|
|
373
416
|
[ '198801', '1', '326', '500', '841330000', '90', '81', '246' ],
|
|
@@ -383,7 +426,7 @@ const to = from + 5;
|
|
|
383
426
|
result = parse.rowOffset(from,to)
|
|
384
427
|
/*
|
|
385
428
|
{
|
|
386
|
-
time: 3743,
|
|
429
|
+
time: 3743 ms,
|
|
387
430
|
header: false,
|
|
388
431
|
rows: [
|
|
389
432
|
[
|
|
@@ -406,7 +449,7 @@ const to = from + 4;
|
|
|
406
449
|
result = parse.rowOffset(from,to)
|
|
407
450
|
/*
|
|
408
451
|
{
|
|
409
|
-
time: 44126,
|
|
452
|
+
time: 44126 ms,
|
|
410
453
|
header: false,
|
|
411
454
|
rows: [
|
|
412
455
|
[ '201412', '1', '125', '400', '283525000', '0', '160000', '6492' ],
|
|
@@ -421,7 +464,7 @@ result = parse.rowOffset(from,to)
|
|
|
421
464
|
result = parse.chunk(3) // Get rows from last offset saved ( row to,to+1 and to+2 )
|
|
422
465
|
/*
|
|
423
466
|
{
|
|
424
|
-
time: 29,
|
|
467
|
+
time: 29 ms,
|
|
425
468
|
header: false,
|
|
426
469
|
rows: [
|
|
427
470
|
[ '201412', '1', '125', '400', '400932000', '0', '18', '526' ],
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "select-csv",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.9",
|
|
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",
|
package/selectcsv.js
CHANGED
|
@@ -1 +1 @@
|
|
|
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,x,y,b,p]=i,c=u.length;if(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]),d&&!r[0]){if(x1=e.indexOf(u),p)for(l=1;l&&-1==x1;)[l,a]=getData(f,h,n),n+=l,e+=a,x1=e.indexOf(u);s=-1==x1?e.length:x1+c}x1=e.indexOf(u,s);let w,m,g=2,O=0,k=sbstr2="",T=[],E=[];for(;g;){if(y){let i=b.length;if(x)for(;-1!=x1;){if(k=e.slice(s,x1),m=k.indexOf('"'),-1!=m)for(y0=0,y1=k.indexOf(b);-1!=y1;)sbstr2=k.slice(y0,y1),y1<m?(E.push(sbstr2),y0=y1+i,y1=k.indexOf(b,y0)):(z=y0,w=m+i,m=k.indexOf('"',w),-1!=m?(y1=k.indexOf(b,m+i),m=k.indexOf('"',y1+i),-1==m&&(m=k.length)):y1=k.indexOf(b,w));else for(y0=0,y1=k.indexOf(b);-1!=y1;)sbstr2=k.slice(y0,y1),E.push(sbstr2),y0=y1+i,y1=k.indexOf(b,y0);if(E.push(k.slice(y0)),T.push(E),E=[],O++,O>=t)break;s=x1+c,x1=e.indexOf(u,s)}else for(;-1!=x1;){for(k=e.slice(s,x1),y0=0,y1=k.indexOf(b);-1!=y1;)sbstr2=k.slice(y0,y1),E.push(sbstr2),y0=y1+i,y1=k.indexOf(b,y0);if(E.push(k.slice(y0)),T.push(E),E=[],O++,O>=t)break;s=x1+c,x1=e.indexOf(u,s)}}else for(;-1!=x1&&(E.push(e.slice(s,x1)),T.push(E),E=[],O++,!(O>=t));)s=x1+c,x1=e.indexOf(u,s);if(O>=t){s=x1+c;break}if(g--,-1==x1)if(g)if(p){for(l=1,e=e.slice(s),o+=s,s=0;l&&-1==x1;)[l,a]=getData(f,h,n),n+=l,e+=a,x1=e.indexOf(u);-1==x1&&(x1=e.length),l&&g++}else x1=e.length;else s=x1}return{get:{rows:T,row_count:O},offs:[o+s,O+r[1]],row_count:O}}function getAllRows(e,t){let i,r,n,o,s,f,[h,l,a,d,u,x]=t,y=l.length,b=x0=0;if(x&&([n,o]=x,i=e.length,s=1),h){if(r=e.indexOf(l),x)for(;s&&-1==r;)[s,f]=getData(n,o,i),i+=s,r=(e+=f).indexOf(l);x0=-1==r?e.length:r+y}r=e.indexOf(l,x0);let p,c,w=2,m=0,g=sbstr2="",O=[],k=[];for(;w;){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+y,r=e.indexOf(l,x0),m++}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+y,r=e.indexOf(l,x0),m++}}else for(;-1!=r;)k.push(e.slice(x0,r)),O.push(k),k=[],x0=r+y,r=e.indexOf(l,x0),m++;if(w--,-1==r)if(w)if(x){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&&w++}else r=e.length;else x0=r}return{get:{rows:O,row_count:m},offs:[b+x0,m],row_count:m}}function getRowsOffsFT(e,t,i,r){let n,o,s,f,h,l,[a,d,u,x,y,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 w,m,g=2,O=0,k=0,T=sbstr2="",E=[],q=[];for(;g;){if(x){let r=y.length;if(u)for(;-1!=o;){if(O>=t){if(!(O<i)){O++;break}if(T=e.slice(x0,o),m=T.indexOf('"'),-1!=m)for(y0=0,y1=T.indexOf(y);-1!=y1;)sbstr2=T.slice(y0,y1),y1<m?(q.push(sbstr2),y0=y1+r,y1=T.indexOf(y,y0)):(z=y0,w=m+r,m=T.indexOf('"',w),-1!=m?(y1=T.indexOf(y,m+r),m=T.indexOf('"',y1+r),-1==m&&(m=T.length)):y1=T.indexOf(y,w));else for(y0=0,y1=T.indexOf(y);-1!=y1;)sbstr2=T.slice(y0,y1),q.push(sbstr2),y0=y1+r,y1=T.indexOf(y,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(y);-1!=y1;)sbstr2=T.slice(y0,y1),q.push(sbstr2),y0=y1+r,y1=T.indexOf(y,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,x,y]=i,b=a.length,p=x0=0;if(y&&([o,s]=y,r=e.length,f=1),l){if(n=e.indexOf(a),y)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,w,m=2,g=0,O=0,k=sbstr2="",T=[],E=[];for(;m;){if(u){let i=x.length;if(d)for(;-1!=n;){if(g>=t){if(k=e.slice(x0,n),w=k.indexOf('"'),-1!=w)for(y0=0,y1=k.indexOf(x);-1!=y1;)sbstr2=k.slice(y0,y1),y1<w?(E.push(sbstr2),y0=y1+i,y1=k.indexOf(x,y0)):(z=y0,c=w+i,w=k.indexOf('"',c),-1!=w?(y1=k.indexOf(x,w+i),w=k.indexOf('"',y1+i),-1==w&&(w=k.length)):y1=k.indexOf(x,c));else for(y0=0,y1=k.indexOf(x);-1!=y1;)sbstr2=k.slice(y0,y1),E.push(sbstr2),y0=y1+i,y1=k.indexOf(x,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(x);-1!=y1;)sbstr2=k.slice(y0,y1),E.push(sbstr2),y0=y1+i,y1=k.indexOf(x,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(m--,-1==n)if(m)if(y){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&&m++}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}}
|
|
1
|
+
const fs=require("fs");function getData(e,i,t){let r,s=Buffer.alloc(i);return 0!==(r=fs.readSync(e,s,0,i,t))?[r,String(s)]:[0,""]}function getRowsChunk(e,i,t,r){let s,n,o,f,h,l,y,{header:a,linebreak:d,quote:x,col:u,d:b,csv:c,oJson:O}=t,p=d.length;c?([f,h]=c,s=n=r[0],o=0,[l,y]=getData(f,h,s),s+=l,e=y):(n=0,o=r[0]),x1=e.indexOf(d,o);let w,m,g=2,k=0,T=sbstr2="",q=[],E=[];for(;g;){if(u){let t=b.length;if(O){let r;if(E={},x)for(;-1!=x1;){if(T=e.slice(o,x1),m=T.indexOf('"'),r=0,-1!=m)for(y0=0,y1=T.indexOf(b);-1!=y1;)sbstr2=T.slice(y0,y1),y1<m?(E[O[r]]=sbstr2,y0=y1+t,y1=T.indexOf(b,y0)):(z=y0,w=m+t,m=T.indexOf('"',w),-1!=m?(y1=T.indexOf(b,m+t),m=T.indexOf('"',y1+t),-1==m&&(m=T.length)):y1=T.indexOf(b,w)),r++;else for(y0=0,y1=T.indexOf(b);-1!=y1;)sbstr2=T.slice(y0,y1),E[O[r]]=sbstr2,y0=y1+t,y1=T.indexOf(b,y0),r++;if(E[O[r]]=T.slice(y0),q.push(E),E={},k++,k>=i)break;o=x1+p,x1=e.indexOf(d,o)}else for(;-1!=x1;){for(T=e.slice(o,x1),y0=0,y1=T.indexOf(b),r=0;-1!=y1;)sbstr2=T.slice(y0,y1),E[O[r]]=sbstr2,y0=y1+t,y1=T.indexOf(b,y0),r++;if(E[O[r]]=T.slice(y0),q.push(E),E={},k++,k>=i)break;o=x1+p,x1=e.indexOf(d,o)}}else if(x)for(;-1!=x1;){if(T=e.slice(o,x1),m=T.indexOf('"'),-1!=m)for(y0=0,y1=T.indexOf(b);-1!=y1;)sbstr2=T.slice(y0,y1),y1<m?(E.push(sbstr2),y0=y1+t,y1=T.indexOf(b,y0)):(z=y0,w=m+t,m=T.indexOf('"',w),-1!=m?(y1=T.indexOf(b,m+t),m=T.indexOf('"',y1+t),-1==m&&(m=T.length)):y1=T.indexOf(b,w));else for(y0=0,y1=T.indexOf(b);-1!=y1;)sbstr2=T.slice(y0,y1),E.push(sbstr2),y0=y1+t,y1=T.indexOf(b,y0);if(E.push(T.slice(y0)),q.push(E),E=[],k++,k>=i)break;o=x1+p,x1=e.indexOf(d,o)}else for(;-1!=x1;){for(T=e.slice(o,x1),y0=0,y1=T.indexOf(b);-1!=y1;)sbstr2=T.slice(y0,y1),E.push(sbstr2),y0=y1+t,y1=T.indexOf(b,y0);if(E.push(T.slice(y0)),q.push(E),E=[],k++,k>=i)break;o=x1+p,x1=e.indexOf(d,o)}}else for(;-1!=x1&&(E.push(e.slice(o,x1)),q.push(E),E=[],k++,!(k>=i));)o=x1+p,x1=e.indexOf(d,o);if(k>=i){o=x1+p;break}if(g--,-1==x1)if(g)if(c){for(l=1,e=e.slice(o),n+=o,o=0;l&&-1==x1;)[l,y]=getData(f,h,s),s+=l,e+=y,x1=e.indexOf(d);-1==x1&&(x1=e.length),l&&g++}else x1=e.length;else o=x1}return{get:{rows:q,row_count:k},offs:[n+o,k+r[1]],row_count:k}}function getAllRows(e,i,t){let r,s,n,o,f,h,{header:l,linebreak:y,quote:a,col:d,d:x,csv:u,oJson:b}=i,c=y.length,O=x0=t;u&&([n,o]=u,r=e.length,f=1),s=e.indexOf(y,x0);let p,w,m=2,g=0,k=sbstr2="",T=[],q=[];for(;m;){if(d){let i=x.length;if(b){let t;if(q={},a)for(;-1!=s;){if(k=e.slice(x0,s),w=k.indexOf('"'),-1!=w)for(y0=0,y1=k.indexOf(x),t=0;-1!=y1;)sbstr2=k.slice(y0,y1),y1<w?(q[b[t]]=sbstr2,y0=y1+i,y1=k.indexOf(x,y0)):(z=y0,p=w+i,w=k.indexOf('"',p),-1!=w?(y1=k.indexOf(x,w+i),w=k.indexOf('"',y1+i),-1==w&&(w=k.length)):y1=k.indexOf(x,p)),t++;else for(y0=0,y1=k.indexOf(x),t=0;-1!=y1;)sbstr2=k.slice(y0,y1),q[b[t]]=sbstr2,y0=y1+i,y1=k.indexOf(x,y0),t++;q[b[t]]=k.slice(y0),T.push(q),q={},x0=s+c,s=e.indexOf(y,x0),g++}else for(;-1!=s;){for(k=e.slice(x0,s),y0=0,y1=k.indexOf(x),t=0;-1!=y1;)sbstr2=k.slice(y0,y1),q[b[t]]=sbstr2,y0=y1+i,y1=k.indexOf(x,y0),t++;q[b[t]]=k.slice(y0),T.push(q),q={},x0=s+c,s=e.indexOf(y,x0),g++}}else if(a)for(;-1!=s;){if(k=e.slice(x0,s),w=k.indexOf('"'),-1!=w)for(y0=0,y1=k.indexOf(x);-1!=y1;)sbstr2=k.slice(y0,y1),y1<w?(q.push(sbstr2),y0=y1+i,y1=k.indexOf(x,y0)):(z=y0,p=w+i,w=k.indexOf('"',p),-1!=w?(y1=k.indexOf(x,w+i),w=k.indexOf('"',y1+i),-1==w&&(w=k.length)):y1=k.indexOf(x,p));else for(y0=0,y1=k.indexOf(x);-1!=y1;)sbstr2=k.slice(y0,y1),q.push(sbstr2),y0=y1+i,y1=k.indexOf(x,y0);q.push(k.slice(y0)),T.push(q),q=[],x0=s+c,s=e.indexOf(y,x0),g++}else for(;-1!=s;){for(k=e.slice(x0,s),y0=0,y1=k.indexOf(x);-1!=y1;)sbstr2=k.slice(y0,y1),q.push(sbstr2),y0=y1+i,y1=k.indexOf(x,y0);q.push(k.slice(y0)),T.push(q),q=[],x0=s+c,s=e.indexOf(y,x0),g++}}else for(;-1!=s;)q.push(e.slice(x0,s)),T.push(q),q=[],x0=s+c,s=e.indexOf(y,x0),g++;if(m--,-1==s)if(m)if(u){for(f=1,e=e.slice(x0),O+=x0,x0=0;f&&-1==s;)[f,h]=getData(n,o,r),r+=f,s=(e+=h).indexOf(y);-1==s&&(s=e.length),f&&m++}else s=e.length;else x0=s}return{get:{rows:T,row_count:g},offs:[O+x0,g],row_count:g}}function getRowsOffsFT(e,i,t,r,s){let n,o,f,h,l,y,{header:a,linebreak:d,quote:x,col:u,d:b,csv:c,oJson:O}=r,p=d.length,w=x0=s;c&&([f,h]=c,n=e.length,l=1),o=e.indexOf(d,x0);let m,g,k=2,T=0,q=0,E=sbstr2="",j=[],R=[];for(;k;){if(u){let r=b.length;if(O){let s;if(R={},x)for(;-1!=o;){if(T>=i){if(!(T<t)){T++;break}if(E=e.slice(x0,o),g=E.indexOf('"'),-1!=g)for(y0=0,y1=E.indexOf(b),s=0;-1!=y1;)sbstr2=E.slice(y0,y1),y1<g?(R[O[s]]=sbstr2,y0=y1+r,y1=E.indexOf(b,y0)):(z=y0,m=g+r,g=E.indexOf('"',m),-1!=g?(y1=E.indexOf(b,g+r),g=E.indexOf('"',y1+r),-1==g&&(g=E.length)):y1=E.indexOf(b,m)),s++;else for(y0=0,y1=E.indexOf(b),s=0;-1!=y1;)sbstr2=E.slice(y0,y1),R[O[s]]=sbstr2,y0=y1+r,y1=E.indexOf(b,y0),s++;R[O[s]]=E.slice(y0),j.push(R),R={},q++}x0=o+p,o=e.indexOf(d,x0),T++}else for(;-1!=o;){if(!(T<t)){T++;break}if(T>=i){for(E=e.slice(x0,o),y0=0,y1=E.indexOf(b),s=0;-1!=y1;)sbstr2=E.slice(y0,y1),R[O[s]]=sbstr2,y0=y1+r,y1=E.indexOf(b,y0),s++;R[O[s]]=E.slice(y0),j.push(R),R={},q++}x0=o+p,o=e.indexOf(d,x0),T++}}else if(x)for(;-1!=o;){if(T>=i){if(!(T<t)){T++;break}if(E=e.slice(x0,o),g=E.indexOf('"'),-1!=g)for(y0=0,y1=E.indexOf(b);-1!=y1;)sbstr2=E.slice(y0,y1),y1<g?(R.push(sbstr2),y0=y1+r,y1=E.indexOf(b,y0)):(z=y0,m=g+r,g=E.indexOf('"',m),-1!=g?(y1=E.indexOf(b,g+r),g=E.indexOf('"',y1+r),-1==g&&(g=E.length)):y1=E.indexOf(b,m));else for(y0=0,y1=E.indexOf(b);-1!=y1;)sbstr2=E.slice(y0,y1),R.push(sbstr2),y0=y1+r,y1=E.indexOf(b,y0);R.push(E.slice(y0)),j.push(R),R=[],q++}x0=o+p,o=e.indexOf(d,x0),T++}else for(;-1!=o;){if(!(T<t)){T++;break}if(T>=i){for(E=e.slice(x0,o),y0=0,y1=E.indexOf(b);-1!=y1;)sbstr2=E.slice(y0,y1),R.push(sbstr2),y0=y1+r,y1=E.indexOf(b,y0);R.push(E.slice(y0)),j.push(R),R=[],q++}x0=o+p,o=e.indexOf(d,x0),T++}}else for(;-1!=o;){if(!(T<t)){T++;break}T>=i&&(R.push(e.slice(x0,o)),j.push(R),R=[],q++),x0=o+p,o=e.indexOf(d,x0),T++}if(T>=t)break;if(k--,-1==o)if(k)if(c){for(l=1,e=e.slice(x0),w+=x0,x0=0;l&&-1==o;)[l,y]=getData(f,h,n),n+=l,o=(e+=y).indexOf(d);-1==o&&(o=e.length),l&&k++}else o=e.length;else x0=o}return{get:{rows:j,row_count:q},offs:[w+x0,T],row_count:q}}function getRowsOffsF(e,i,t,r){let s,n,o,f,h,l,{header:y,linebreak:a,quote:d,col:x,d:u,csv:b,oJson:c}=t,O=a.length,p=x0=r;b&&([o,f]=b,s=e.length,h=1),n=e.indexOf(a,x0);let w,m,g=2,k=0,T=0,q=sbstr2="",E=[],j=[];for(;g;){if(x){let t=u.length;if(c){let r;if(j={},d)for(;-1!=n;){if(k>=i){if(q=e.slice(x0,n),m=q.indexOf('"'),-1!=m)for(y0=0,y1=q.indexOf(u),r=0;-1!=y1;)sbstr2=q.slice(y0,y1),y1<m?(j[c[r]]=sbstr2,y0=y1+t,y1=q.indexOf(u,y0)):(z=y0,w=m+t,m=q.indexOf('"',w),-1!=m?(y1=q.indexOf(u,m+t),m=q.indexOf('"',y1+t),-1==m&&(m=q.length)):y1=q.indexOf(u,w)),r++;else for(y0=0,y1=q.indexOf(u),r=0;-1!=y1;)sbstr2=q.slice(y0,y1),j[c[r]]=sbstr2,y0=y1+t,y1=q.indexOf(u,y0),r++;j[c[r]]=q.slice(y0),E.push(j),j={},T++}x0=n+O,n=e.indexOf(a,x0),k++}else for(;-1!=n;){if(k>=i){for(q=e.slice(x0,n),y0=0,y1=q.indexOf(u),r=0;-1!=y1;)sbstr2=q.slice(y0,y1),j[c[r]]=sbstr2,y0=y1+t,y1=q.indexOf(u,y0),r++;j[c[r]]=q.slice(y0),E.push(j),j={},T++}x0=n+O,n=e.indexOf(a,x0),k++}}else if(d)for(;-1!=n;){if(k>=i){if(q=e.slice(x0,n),m=q.indexOf('"'),-1!=m)for(y0=0,y1=q.indexOf(u);-1!=y1;)sbstr2=q.slice(y0,y1),y1<m?(j.push(sbstr2),y0=y1+t,y1=q.indexOf(u,y0)):(z=y0,w=m+t,m=q.indexOf('"',w),-1!=m?(y1=q.indexOf(u,m+t),m=q.indexOf('"',y1+t),-1==m&&(m=q.length)):y1=q.indexOf(u,w));else for(y0=0,y1=q.indexOf(u);-1!=y1;)sbstr2=q.slice(y0,y1),j.push(sbstr2),y0=y1+t,y1=q.indexOf(u,y0);j.push(q.slice(y0)),E.push(j),j=[],T++}x0=n+O,n=e.indexOf(a,x0),k++}else for(;-1!=n;){if(k>=i){for(q=e.slice(x0,n),y0=0,y1=q.indexOf(u);-1!=y1;)sbstr2=q.slice(y0,y1),j.push(sbstr2),y0=y1+t,y1=q.indexOf(u,y0);j.push(q.slice(y0)),E.push(j),j=[],T++}x0=n+O,n=e.indexOf(a,x0),k++}}else for(;-1!=n;)k>=i&&(j.push(e.slice(x0,n)),E.push(j),j=[],T++),x0=n+O,n=e.indexOf(a,x0),k++;if(g--,-1==n)if(g)if(b){for(h=1,e=e.slice(x0),p+=x0,x0=0;h&&-1==n;)[h,l]=getData(o,f,s),s+=h,n=(e+=l).indexOf(a);-1==n&&(n=e.length),h&&g++}else n=e.length;else x0=n}return{get:{rows:E,row_count:T},offs:[p+x0,k],row_count:T}}function get(){let e,i=Date.now();return e=getAllRows(this.data,this.info,this.starOffsRow),{time:Date.now()-i+" ms",header:this.option.header?this.header:this.option.header,...e.get}}function chunk(e){let i,t=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 i=getRowsChunk(this.data,e,this.info,this.offs_n),this.offs_n=i.offs,{time:Date.now()-t+" ms",header:this.option.header?this.header:this.option.header,...i.get}}function rowOffset(e,i){let t,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==i)t=getRowsOffsF(this.data,e,this.info,this.starOffsRow);else{if(!Number.isInteger(i))throw new Error("The second parameter must be an integer");if(!(i>0))throw new Error("The second parameter must be greater than or equal to 1");t=getRowsOffsFT(this.data,e,i,this.info,this.starOffsRow)}return t.row_count&&(this.offs_n=t.offs),{time:Date.now()-r+" ms",header:this.option.header?this.header:this.option.header,...t.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 i=this.info[1].length;n=0,x0=0,x1=this.data.indexOf(this.info[1]);let t=this.data.length;for(;-1!=x1;){if(n==e)return t=this.option.header?x1+i:x0,this.offs_n=[t,n],[t,n];x0=x1+i,x1=this.data.indexOf(this.info[1],x0),n++}}}return!1}function resetOption(e){let i,t={header:!0,quote:!1,linebreak:"\r\n",delimiter:",",json:!1,bufferSize:1048576};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)}if("json"in e){if("boolean"!=typeof e.json)throw new Error("The 'json' key in The second parameter must be 'boolean'");t.json=e.json}if("bufferSize"in e){if(!Number.isInteger(e.bufferSize))throw new Error("The 'rowOffs' parameter must be an integer");if(!(e.bufferSize>=1024))throw new Error("The 'bufferSize' parameter must be greater than or equal to 1024");t.bufferSize=e.bufferSize}}else this.option=t;i=this.fd?getColumns(this.data,this.option,this.csv):getColumns(this.data,this.option),this.header=i[0],this.starOffsRow=i[1],this.offs_n=[this.starOffsRow,0],this.oJson=!(!this.option.header||!this.option.json)&&this.header,this.info={header:this.option.header,linebreak:this.option.linebreak,quote:this.option.quote,col:this.column,d:this.option.delimiter,csv:this.csv,oJson:this.oJson}}function getInfo(){return{offset:this.offs_n[0],rowOffset:this.offs_n[1],option:this.option}}function offs_nRowwwwwwww(e,i,t){let r,s=0;if(i.header){if(t){let s,[n,o]=t,f=0,h=1;for(r=e.indexOf(i.linebreak);h&&-1==r;)[h,s]=getData(n,o,f),f+=h,r=(e+=s).indexOf(i.linebreak)}else r=e.indexOf(i.linebreak);s=-1==r?r=e.length:r+i.linebreak.length}return[s,0]}function getColumns(e,i,t){let r=!1,s=0;if(i.header){r=[];let n,o=i.delimiter.length;if(t){let r,[s,o]=t,f=0,h=1;for(n=e.indexOf(i.linebreak);h&&-1==n;)[h,r]=getData(s,o,f),f+=h,n=(e+=r).indexOf(i.linebreak)}else n=e.indexOf(i.linebreak);if(-1!=n){let t,f=e.slice(s,n),h=0,l=f.indexOf(i.delimiter);for(;-1!=l;)t=f.slice(h,l),r.push(t),h=l+o,l=f.indexOf(i.delimiter,h);r.push(f.slice(h))}s=-1==n?n=e.length:n+i.linebreak.length}return[r,s]}function parseCsv(e,i){let t,r,s=!0,n={header:!0,quote:!1,linebreak:"\r\n",delimiter:",",json:!1,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[i,s]=getData(r,n.bufferSize,0);t=s}if(null!=i){if("object"!=typeof i)throw new Error("The second parameter must be an object");if("header"in i){if("boolean"!=typeof i.header)throw new Error("The 'header' key in The second parameter must be 'boolean'");n.header=i.header}if("quote"in i){if("boolean"!=typeof i.quote)throw new Error("The 'quote' key in The second parameter must be 'boolean'");n.quote=i.quote}if("linebreak"in i){if("string"!=typeof i.linebreak)throw new Error("The 'linebreak' key in The second parameter must be 'string'");n.linebreak=i.linebreak}if("delimiter"in i){if("string"!=typeof i.delimiter&&("boolean"!=typeof i.delimiter||i.delimiter))throw new Error("The 'delimiter' key in The second parameter must be 'string' or false 'boolean'");n.delimiter=i.delimiter,"boolean"===i.delimiter&&(s=!1)}if("json"in i){if("boolean"!=typeof i.json)throw new Error("The 'json' key in The second parameter must be 'boolean'");n.json=i.json}if("bufferSize"in i){if(!Number.isInteger(i.bufferSize))throw new Error("The 'rowOffs' parameter must be an integer");if(!(i.bufferSize>=1024))throw new Error("The 'bufferSize' parameter must be greater than or equal to 1024");n.bufferSize=i.bufferSize}}return new class{constructor(){let e;this.option=n,this.data=t,this.column=s,this.csv=[r,n.bufferSize],e=getColumns(this.data,this.option,this.csv),this.header=e[0],this.starOffsRow=e[1],this.offs_n=[this.starOffsRow,0],this.oJson=!(!this.option.header||!this.option.json)&&this.header,this.info={header:this.option.header,linebreak:this.option.linebreak,quote:this.option.quote,col:this.column,d:this.option.delimiter,csv:this.csv,oJson:this.oJson}}get=get;chunk=chunk;setRowOffset=setRowOffset;rowOffset=rowOffset;getInfo=getInfo}}function parseText(e,i){let t,r={header:!0,quote:!1,linebreak:"\r\n",delimiter:",",json:!1};if(null==e&&"string"==typeof e)throw new Error("The first parameter must be a file path string");if(t=e,null!=i){if("object"!=typeof i)throw new Error("The second parameter must be an object");if("header"in i){if("boolean"!=typeof i.header)throw new Error("The 'header' key in The second parameter must be 'boolean'");r.header=i.header}if("quote"in i){if("boolean"!=typeof i.quote)throw new Error("The 'quote' key in The second parameter must be 'boolean'");r.quote=i.quote}if("linebreak"in i){if("string"!=typeof i.linebreak)throw new Error("The 'linebreak' key in The second parameter must be 'string'");r.linebreak=i.linebreak}if("delimiter"in i){if("string"!=typeof i.delimiter&&("boolean"!=typeof i.delimiter||i.delimiter))throw new Error("The 'delimiter' key in The second parameter must be 'string' or false 'boolean'");r.delimiter=i.delimiter,"boolean"===i.delimiter&&(this.column=!1)}if("json"in i){if("boolean"!=typeof i.json)throw new Error("The 'json' key in The second parameter must be 'boolean'");r.json=i.json}}return new class{constructor(){let e;this.option=r,this.data=t,this.column=true,this.csv=!1,e=getColumns(this.data,this.option),this.header=e[0],this.starOffsRow=e[1],this.offs_n=[this.starOffsRow,0],this.oJson=!(!this.option.header||!this.option.json)&&this.header,this.info={header:this.option.header,linebreak:this.option.linebreak,quote:this.option.quote,col:this.column,d:this.option.delimiter,oJson:this.oJson}}get=get;chunk=chunk;setRowOffset=setRowOffset;rowOffset=rowOffset;resetOption=resetOption;getInfo=getInfo}}module.exports={parseText:parseText,parseCsv:parseCsv};
|