select-csv 1.1.5 → 1.1.7
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 +45 -34
- package/package.json +8 -8
- package/selectcsv.js +1 -1
package/README.md
CHANGED
|
@@ -64,23 +64,23 @@ parse = parseText(
|
|
|
64
64
|
```js
|
|
65
65
|
const result = parse.get(); //Return all rows
|
|
66
66
|
/*
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
67
|
+
{
|
|
68
|
+
time:1,
|
|
69
|
+
header:["Index","User Id","First Name","Last Name","Sex"],
|
|
70
|
+
rows:[
|
|
71
|
+
["1","5f10e9D33fC5f2b","Sara","Mcguire","Female"],
|
|
72
|
+
["2","751cD1cbF77e005","Alisha","Hebert","Male"],
|
|
73
|
+
["3","DcEFDB2D2e62bF9","Gwendolyn","Sheppard","Male"],
|
|
74
|
+
["4","C88661E02EEDA9e","Kristine","Mccann","Female"],
|
|
75
|
+
["5","fafF1aBDebaB2a6","Bobby","Pittman","Female"],
|
|
76
|
+
["6","BdDb6C8Af309202","Calvin","Ramsey","Female"],
|
|
77
|
+
["7","FCdfFf08196f633","Collin","Allison","Male"],
|
|
78
|
+
["8","356279dAa0F7CbD","Nicholas","Branch","Male"],
|
|
79
|
+
["9","F563CcbFBfEcf5a","Emma","Robinson","Female"],
|
|
80
|
+
["10","f2dceFc00F62542","Pedro","Cordova","Male"]
|
|
81
|
+
],
|
|
82
|
+
row_count:10
|
|
83
|
+
}
|
|
84
84
|
*/
|
|
85
85
|
|
|
86
86
|
```
|
|
@@ -228,23 +228,23 @@ result = parse.chunk(1) // Get rows from last offset saved
|
|
|
228
228
|
|
|
229
229
|
```js
|
|
230
230
|
{
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
231
|
+
'header': true,
|
|
232
|
+
'quote': false,
|
|
233
|
+
'linebreak': '\r\n',
|
|
234
|
+
'delimiter': ",",
|
|
235
235
|
'bufferSize':1024*1024
|
|
236
236
|
}
|
|
237
|
-
|
|
237
|
+
// delimiter: (String: get rows containing columns, false: get lines without columns)
|
|
238
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
|
|
239
239
|
```
|
|
240
240
|
|
|
241
241
|
* If you want to use specific option :
|
|
242
242
|
```js
|
|
243
243
|
var option = {
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
244
|
+
'header': false, /* or true */
|
|
245
|
+
'quote': true, /* or false */
|
|
246
|
+
'linebreak': '\n', /* '\n' or '\r' or any other string */
|
|
247
|
+
'delimiter': "," /* ';' or any other string or false */
|
|
248
248
|
'bufferSize':2000 /* It only works with a CSV file */
|
|
249
249
|
}
|
|
250
250
|
|
|
@@ -261,7 +261,7 @@ parse = parseText(
|
|
|
261
261
|
, option);
|
|
262
262
|
|
|
263
263
|
option = {
|
|
264
|
-
|
|
264
|
+
'header': false,
|
|
265
265
|
}
|
|
266
266
|
|
|
267
267
|
result = parse.rowOffset(2)
|
|
@@ -277,10 +277,10 @@ result = parse.rowOffset(2)
|
|
|
277
277
|
*/
|
|
278
278
|
|
|
279
279
|
option = {
|
|
280
|
-
|
|
281
|
-
|
|
280
|
+
'header': true,
|
|
281
|
+
'delimiter': false
|
|
282
282
|
}
|
|
283
|
-
|
|
283
|
+
// delimiter: (String: get rows containing columns, false: get lines without columns)
|
|
284
284
|
/*
|
|
285
285
|
{
|
|
286
286
|
"Time": 0,
|
|
@@ -297,9 +297,9 @@ option = {
|
|
|
297
297
|
* If you want to reset option after multiple uses of your code :
|
|
298
298
|
```js
|
|
299
299
|
const option = { // Just an exapmle
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
300
|
+
'header': false,
|
|
301
|
+
'quote': true,
|
|
302
|
+
'linebreak': '\n'
|
|
303
303
|
}
|
|
304
304
|
|
|
305
305
|
parse.resetOption(option); // All saved values are erased and the object is restared again
|
|
@@ -446,4 +446,15 @@ result = parse.getInfo() // Get all the information
|
|
|
446
446
|
}
|
|
447
447
|
}
|
|
448
448
|
*/
|
|
449
|
-
```
|
|
449
|
+
```
|
|
450
|
+
|
|
451
|
+
## Request Features or Report Bugs
|
|
452
|
+
|
|
453
|
+
Feature requests and bug reports are very welcome: https://github.com/housseynCheriet/select-csv/issues
|
|
454
|
+
|
|
455
|
+
A couple of requests from me when you raise an issue on GitHub.
|
|
456
|
+
|
|
457
|
+
* **Requesting a feature:** Please try to provide the context of why you want the feature. Such as,
|
|
458
|
+
in what situation the feature could help you and how, or how the lack of the feature is causing an inconvenience to you.
|
|
459
|
+
I can't start thinking of introducing it until I understand how it helps you 🙂
|
|
460
|
+
* **Reporting a bug:** If you could provide a runnable code snippet that reproduces the bug, it would be very helpful!
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "select-csv",
|
|
3
|
-
"version": "1.1.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "1.1.7",
|
|
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",
|
|
7
7
|
"parser",
|
|
@@ -10,23 +10,23 @@
|
|
|
10
10
|
"delimited",
|
|
11
11
|
"text",
|
|
12
12
|
"data",
|
|
13
|
-
"auto-detect",
|
|
14
13
|
"comma",
|
|
15
14
|
"tab",
|
|
16
15
|
"pipe",
|
|
17
16
|
"file",
|
|
18
17
|
"filereader",
|
|
19
18
|
"stream",
|
|
20
|
-
"
|
|
21
|
-
"
|
|
22
|
-
"thread",
|
|
23
|
-
"threading",
|
|
24
|
-
"multi-threaded"
|
|
19
|
+
"chunk",
|
|
20
|
+
"line"
|
|
25
21
|
],
|
|
26
22
|
"author": {
|
|
27
23
|
"name": "Housseyn Cheriet",
|
|
28
24
|
"url": "https://github.com/housseynCheriet"
|
|
29
25
|
},
|
|
26
|
+
"repository": {
|
|
27
|
+
"type": "git",
|
|
28
|
+
"url": "https://github.com/housseynCheriet/select-csv"
|
|
29
|
+
},
|
|
30
30
|
"license": "CC-BY-ND",
|
|
31
31
|
"main": "selectcsv.js",
|
|
32
32
|
"scripts": {},
|
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,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};
|
|
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}}
|