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