select-csv 1.0.1
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 +330 -0
- package/package.json +40 -0
- package/selectcsv.js +1 -0
package/README.md
ADDED
|
@@ -0,0 +1,330 @@
|
|
|
1
|
+
Parse CSV with JavaScript
|
|
2
|
+
========================================
|
|
3
|
+
|
|
4
|
+
It is the 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, and it comes with these features:
|
|
5
|
+
|
|
6
|
+
- Package with small content (< 30 KB)
|
|
7
|
+
- Easy to use
|
|
8
|
+
- Parse CSV files directly (local)
|
|
9
|
+
- Fast mode
|
|
10
|
+
- Stream large files
|
|
11
|
+
- It is a synchronous package
|
|
12
|
+
- Uses chunks
|
|
13
|
+
- Uses row offset (get rows from line x to line x+n)
|
|
14
|
+
- Returns rows (with columns) or lines (without columns)
|
|
15
|
+
- No external dependencies
|
|
16
|
+
- Flexible with lots of options (header, quote, line break, delimiter)
|
|
17
|
+
- One of the only parsers that correctly handles line-breaks and quotations
|
|
18
|
+
|
|
19
|
+
select-csv has **no dependencies** .
|
|
20
|
+
|
|
21
|
+
Install:
|
|
22
|
+
-------
|
|
23
|
+
|
|
24
|
+
select-csv is available on [npm](https://www.npmjs.com/package/papaparse). It
|
|
25
|
+
can be installed with the following command:
|
|
26
|
+
|
|
27
|
+
npm install select-csv
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
Usage:
|
|
32
|
+
-------
|
|
33
|
+
|
|
34
|
+
Here there are clearly different examples
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
```js
|
|
38
|
+
const {parseCsv,parseText} = require("select-csv");
|
|
39
|
+
|
|
40
|
+
var parse;
|
|
41
|
+
|
|
42
|
+
// First create object from .csv file
|
|
43
|
+
parse = parseCsv('file_path.csv');
|
|
44
|
+
|
|
45
|
+
// Or if you just want create object from text
|
|
46
|
+
parse = parseText(
|
|
47
|
+
`Index,User Id,First Name,Last Name,Sex
|
|
48
|
+
1,5f10e9D33fC5f2b,Sara,Mcguire,Female
|
|
49
|
+
2,751cD1cbF77e005,Alisha,Hebert,Male
|
|
50
|
+
3,DcEFDB2D2e62bF9,Gwendolyn,Sheppard,Male
|
|
51
|
+
4,C88661E02EEDA9e,Kristine,Mccann,Female
|
|
52
|
+
5,fafF1aBDebaB2a6,Bobby,Pittman,Female
|
|
53
|
+
6,BdDb6C8Af309202,Calvin,Ramsey,Female
|
|
54
|
+
7,FCdfFf08196f633,Collin,Allison,Male
|
|
55
|
+
8,356279dAa0F7CbD,Nicholas,Branch,Male
|
|
56
|
+
9,F563CcbFBfEcf5a,Emma,Robinson,Female
|
|
57
|
+
10,f2dceFc00F62542,Pedro,Cordova,Male`
|
|
58
|
+
);
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
* If you want to get all rows :
|
|
64
|
+
```js
|
|
65
|
+
const result = parse.get(); //Return all rows
|
|
66
|
+
/*
|
|
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
|
+
*/
|
|
85
|
+
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
* If you want to get a chunks of rows :
|
|
89
|
+
|
|
90
|
+
```js
|
|
91
|
+
var result;
|
|
92
|
+
result = parse.chunk(c)
|
|
93
|
+
//The 'c' parameter must be an integer and greater than or equal to 1
|
|
94
|
+
|
|
95
|
+
//Examples:
|
|
96
|
+
result = parse.chunk(2) //Return row 0 and 1
|
|
97
|
+
/*
|
|
98
|
+
{
|
|
99
|
+
"Time": 0,
|
|
100
|
+
"Header": [ "Index", "User Id", "First Name", "Last Name", "Sex" ],
|
|
101
|
+
"Rows": [
|
|
102
|
+
[ "1", "5f10e9D33fC5f2b", "Sara", "Mcguire", "Female" ],
|
|
103
|
+
[ "2", "751cD1cbF77e005", "Alisha", "Hebert", "Male" ]
|
|
104
|
+
],
|
|
105
|
+
"Row_count": 2
|
|
106
|
+
}
|
|
107
|
+
*/
|
|
108
|
+
|
|
109
|
+
result = parse.chunk(3) //Return row 2,3 and 4
|
|
110
|
+
/*
|
|
111
|
+
{
|
|
112
|
+
"Time": 0,
|
|
113
|
+
"Header": [ "Index", "User Id", "First Name", "Last Name", "Sex" ],
|
|
114
|
+
"Rows": [
|
|
115
|
+
[ "3", "DcEFDB2D2e62bF9", "Gwendolyn", "Sheppard", "Male" ],
|
|
116
|
+
[ "4", "C88661E02EEDA9e", "Kristine", "Mccann", "Female" ],
|
|
117
|
+
[ "5", "fafF1aBDebaB2a6", "Bobby", "Pittman", "Female" ]
|
|
118
|
+
],
|
|
119
|
+
"Row_count": 3
|
|
120
|
+
}
|
|
121
|
+
*/
|
|
122
|
+
|
|
123
|
+
result = parse.chunk(1) //Return row 5
|
|
124
|
+
/*
|
|
125
|
+
{
|
|
126
|
+
"Time": 0,
|
|
127
|
+
"Header": [ "Index", "User Id", "First Name", "Last Name", "Sex" ],
|
|
128
|
+
"Rows": [
|
|
129
|
+
[ "6", "BdDb6C8Af309202", "Calvin", "Ramsey", "Female" ]
|
|
130
|
+
],
|
|
131
|
+
"Row_count": 1
|
|
132
|
+
}
|
|
133
|
+
*/
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
* If you want to get specific rows :
|
|
139
|
+
|
|
140
|
+
```js
|
|
141
|
+
var result
|
|
142
|
+
result = parse.rowOffset(from)
|
|
143
|
+
// The 'from' parameter must be an integer and greater than or equal to 0
|
|
144
|
+
|
|
145
|
+
// Or
|
|
146
|
+
result = parse.rowOffset(from,to)
|
|
147
|
+
// The 'to' parameter must be an integer and greater than or equal to 1
|
|
148
|
+
|
|
149
|
+
//Examples:
|
|
150
|
+
result = parse.rowOffset(6) //Returns all rows from the sixth row to the last row
|
|
151
|
+
/*
|
|
152
|
+
{
|
|
153
|
+
"Time": 0,
|
|
154
|
+
"Header": [ "Index", "User Id", "First Name", "Last Name", "Sex" ],
|
|
155
|
+
"Rows": [
|
|
156
|
+
[ "7", "FCdfFf08196f633", "Collin", "Allison", "Male" ],
|
|
157
|
+
[ "8", "356279dAa0F7CbD", "Nicholas", "Branch", "Male" ],
|
|
158
|
+
[ "9", "F563CcbFBfEcf5a", "Emma", "Robinson", "Female" ],
|
|
159
|
+
[ "10", "f2dceFc00F62542", "Pedro", "Cordova", "Male" ]
|
|
160
|
+
],
|
|
161
|
+
"Row_count": 4
|
|
162
|
+
}
|
|
163
|
+
*/
|
|
164
|
+
|
|
165
|
+
result = parse.rowOffset(5,14) //Returns all rows from 5th to 14th row
|
|
166
|
+
/*
|
|
167
|
+
{
|
|
168
|
+
"Time": 1,
|
|
169
|
+
"Header": [ "Index", "User Id", "First Name", "Last Name", "Sex" ],
|
|
170
|
+
"Rows": [
|
|
171
|
+
[ "6", "BdDb6C8Af309202", "Calvin", "Ramsey", "Female" ],
|
|
172
|
+
[ "7", "FCdfFf08196f633", "Collin", "Allison", "Male" ],
|
|
173
|
+
[ "8", "356279dAa0F7CbD", "Nicholas", "Branch", "Male" ]
|
|
174
|
+
],
|
|
175
|
+
"Row_count": 3
|
|
176
|
+
}
|
|
177
|
+
*/
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
* If you want to change the row offset :
|
|
184
|
+
|
|
185
|
+
```js
|
|
186
|
+
parse.setRowOffset(offs)
|
|
187
|
+
|
|
188
|
+
// The 'offs' parameter must be an integer and greater than or equal to 0.
|
|
189
|
+
|
|
190
|
+
// If the offset exists, return [offset,row_number].
|
|
191
|
+
result = parse.setRowOffset(5)
|
|
192
|
+
/*
|
|
193
|
+
[236,5]
|
|
194
|
+
*/
|
|
195
|
+
result = parse.chunk(1)
|
|
196
|
+
/*
|
|
197
|
+
{
|
|
198
|
+
"Time": 0,
|
|
199
|
+
"Header": [ "Index", "User Id", "First Name", "Last Name", "Sex" ],
|
|
200
|
+
"Rows": [
|
|
201
|
+
[ "6", "BdDb6C8Af309202", "Calvin", "Ramsey", "Female" ]
|
|
202
|
+
],
|
|
203
|
+
"Row_count": 1
|
|
204
|
+
}
|
|
205
|
+
*/
|
|
206
|
+
|
|
207
|
+
// If not , returns false and the offset not changed.
|
|
208
|
+
result = parse.setRowOffset(20)
|
|
209
|
+
/*
|
|
210
|
+
false
|
|
211
|
+
*/
|
|
212
|
+
result = parse.chunk(1)
|
|
213
|
+
/*
|
|
214
|
+
{
|
|
215
|
+
"Time": 0,
|
|
216
|
+
"Header": [ "Index", "User Id", "First Name", "Last Name", "Sex" ],
|
|
217
|
+
"Rows": [
|
|
218
|
+
[ "7", "FCdfFf08196f633", "Collin", "Allison", "Male" ]
|
|
219
|
+
],
|
|
220
|
+
"Row_count": 1
|
|
221
|
+
}
|
|
222
|
+
*/
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
* The default object option :
|
|
228
|
+
|
|
229
|
+
```js
|
|
230
|
+
{
|
|
231
|
+
'header': true,
|
|
232
|
+
'quote': false,
|
|
233
|
+
'linebreak': '\r\n',
|
|
234
|
+
'delimiter': ","
|
|
235
|
+
}
|
|
236
|
+
// delimiter: (String: get rows containing columns, false: get lines without columns)
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
* If you want to use specific option :
|
|
240
|
+
```js
|
|
241
|
+
var option = {
|
|
242
|
+
'header': false, /* or true */
|
|
243
|
+
'quote': true, /* or false */
|
|
244
|
+
'linebreak': '\n', /* '\n' or '\r' or any other string */
|
|
245
|
+
'delimiter': "," /* ';' or any other string or false */
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
var parse;
|
|
249
|
+
// Create object from .csv file
|
|
250
|
+
parse = parseCsv('file_path.csv',option);
|
|
251
|
+
|
|
252
|
+
// Or if you just want create object from text
|
|
253
|
+
parse = parseText(
|
|
254
|
+
`Index,User Id,First Name,Last Name,Sex
|
|
255
|
+
1,5f10e9D33fC5f2b,Sara,Mcguire,Female
|
|
256
|
+
2,751cD1cbF77e005,Alisha,Hebert,Male
|
|
257
|
+
3,DcEFDB2D2e62bF9,Gwendolyn,Sheppard,Male`
|
|
258
|
+
, option);
|
|
259
|
+
|
|
260
|
+
option = {
|
|
261
|
+
'header': false,
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
result = parse.rowOffset(2)
|
|
265
|
+
/*
|
|
266
|
+
{
|
|
267
|
+
"Time": 0,
|
|
268
|
+
"Header": false,
|
|
269
|
+
"Rows": [
|
|
270
|
+
[ "2", "751cD1cbF77e005", "Alisha", "Hebert", "Male" ]
|
|
271
|
+
],
|
|
272
|
+
"Row_count": 1
|
|
273
|
+
}
|
|
274
|
+
*/
|
|
275
|
+
|
|
276
|
+
option = {
|
|
277
|
+
'header': true,
|
|
278
|
+
'delimiter': false
|
|
279
|
+
}
|
|
280
|
+
// delimiter: (String: get rows containing columns, false: get lines without columns)
|
|
281
|
+
/*
|
|
282
|
+
{
|
|
283
|
+
"Time": 0,
|
|
284
|
+
"Header": false,
|
|
285
|
+
"Rows": [
|
|
286
|
+
[ "2,751cD1cbF77e005,Alisha,Hebert,Male" ] // No columns, just string (all line)
|
|
287
|
+
],
|
|
288
|
+
"Row_count": 1
|
|
289
|
+
}
|
|
290
|
+
*/
|
|
291
|
+
|
|
292
|
+
```
|
|
293
|
+
|
|
294
|
+
* If you want reset option after multiple uses of your code :
|
|
295
|
+
```js
|
|
296
|
+
const option = { // Just an exapmle
|
|
297
|
+
'header': false,
|
|
298
|
+
'quote': true,
|
|
299
|
+
'linebreak': '\n'
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
parse.resetOption(option); // All saved values are erased and the object is restared again
|
|
303
|
+
|
|
304
|
+
|
|
305
|
+
```
|
|
306
|
+
|
|
307
|
+
* If you want get information of your object :
|
|
308
|
+
```js
|
|
309
|
+
const option = { // Just an exapmle
|
|
310
|
+
'header': false,
|
|
311
|
+
'quote': true,
|
|
312
|
+
'linebreak': '\n'
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
parse.getInfo();
|
|
316
|
+
/*
|
|
317
|
+
{
|
|
318
|
+
"offset": 275,
|
|
319
|
+
"rowOffset": 7,
|
|
320
|
+
"option": {
|
|
321
|
+
"header": false,
|
|
322
|
+
"quote": false,
|
|
323
|
+
"linebreak": "\n",
|
|
324
|
+
"delimiter": false
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
*/
|
|
328
|
+
|
|
329
|
+
|
|
330
|
+
```
|
package/package.json
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "select-csv",
|
|
3
|
+
"version": "1.0.1",
|
|
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
|
+
"keywords": [
|
|
6
|
+
"csv",
|
|
7
|
+
"parser",
|
|
8
|
+
"parse",
|
|
9
|
+
"parsing",
|
|
10
|
+
"delimited",
|
|
11
|
+
"text",
|
|
12
|
+
"data",
|
|
13
|
+
"auto-detect",
|
|
14
|
+
"comma",
|
|
15
|
+
"tab",
|
|
16
|
+
"pipe",
|
|
17
|
+
"file",
|
|
18
|
+
"filereader",
|
|
19
|
+
"stream",
|
|
20
|
+
"worker",
|
|
21
|
+
"workers",
|
|
22
|
+
"thread",
|
|
23
|
+
"threading",
|
|
24
|
+
"multi-threaded"
|
|
25
|
+
],
|
|
26
|
+
"repository": {
|
|
27
|
+
"type": "git",
|
|
28
|
+
"url": "https://github.com/housseynCheriet"
|
|
29
|
+
},
|
|
30
|
+
"author": {
|
|
31
|
+
"name": "Housseyn Cheriet",
|
|
32
|
+
"url": "https://www.linkedin.com/in/housseyn-cheriet-a08a0016a/"
|
|
33
|
+
},
|
|
34
|
+
"license": "CC-BY-ND",
|
|
35
|
+
"main": "selectcsv.js",
|
|
36
|
+
"scripts": {},
|
|
37
|
+
"dependencies": {
|
|
38
|
+
|
|
39
|
+
}
|
|
40
|
+
}
|
package/selectcsv.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
const fs=require("fs");function getRowsChunk(e,t,i,r){let n,o,[s,f,h,l,a]=i,d=f.length,y=r[0],u=e.indexOf(f,y),p=2,b=0,c=sbstr2="",m=[],w=[];for(;p;){if(l){let i=a.length;if(h)for(;-1!=u;){if(c=e.slice(y,u),o=c.indexOf('"'),-1!=o)for(y0=0,y1=c.indexOf(a);-1!=y1;)sbstr2=c.slice(y0,y1),y1<o?(w.push(sbstr2),y0=y1+i,y1=c.indexOf(a,y0)):(z=y0,n=o+i,o=c.indexOf('"',n),-1!=o?(y1=c.indexOf(a,o+i),o=c.indexOf('"',y1+i),-1==o&&(o=c.length)):y1=c.indexOf(a,n));else for(y0=0,y1=c.indexOf(a);-1!=y1;)sbstr2=c.slice(y0,y1),w.push(sbstr2),y0=y1+i,y1=c.indexOf(a,y0);if(w.push(c.slice(y0)),m.push(w),w=[],b++,b>=t)break;y=u+d,u=e.indexOf(f,y)}else for(;-1!=u;){for(c=e.slice(y,u),y0=0,y1=c.indexOf(a);-1!=y1;)sbstr2=c.slice(y0,y1),w.push(sbstr2),y0=y1+i,y1=c.indexOf(a,y0);if(w.push(c.slice(y0)),m.push(w),w=[],b++,b>=t)break;y=u+d,u=e.indexOf(f,y)}}else for(;-1!=u&&(w.push(e.slice(y,u)),m.push(w),w=[],b++,!(b>=t));)y=u+d,u=e.indexOf(f,y);if(b>=t){y=u+d;break}p--,-1==u&&(p?u=e.length:y=u)}return{get:{Rows:m,Row_count:b},offs:[y,b+r[1]],Row_count:b}}function getAllRows(e,t,i){let r,n,[o,s,f,h,l]=t,a=s.length,d=i[0],y=e.indexOf(s,d),u=2,p=0,b=sbstr2="",c=[],m=[];for(;u;){if(h){let t=l.length;if(f)for(;-1!=y;){if(b=e.slice(d,y),n=b.indexOf('"'),-1!=n)for(y0=0,y1=b.indexOf(l);-1!=y1;)sbstr2=b.slice(y0,y1),y1<n?(m.push(sbstr2),y0=y1+t,y1=b.indexOf(l,y0)):(z=y0,r=n+t,n=b.indexOf('"',r),-1!=n?(y1=b.indexOf(l,n+t),n=b.indexOf('"',y1+t),-1==n&&(n=b.length)):y1=b.indexOf(l,r));else for(y0=0,y1=b.indexOf(l);-1!=y1;)sbstr2=b.slice(y0,y1),m.push(sbstr2),y0=y1+t,y1=b.indexOf(l,y0);m.push(b.slice(y0)),c.push(m),m=[],d=y+a,y=e.indexOf(s,d),p++}else for(;-1!=y;){for(b=e.slice(d,y),y0=0,y1=b.indexOf(l);-1!=y1;)sbstr2=b.slice(y0,y1),m.push(sbstr2),y0=y1+t,y1=b.indexOf(l,y0);m.push(b.slice(y0)),c.push(m),m=[],d=y+a,y=e.indexOf(s,d),p++}}else for(;-1!=y;)m.push(e.slice(d,y)),c.push(m),m=[],d=y+a,y=e.indexOf(s,d),p++;u--,-1==y&&(u?y=e.length:d=y)}return{get:{Rows:c,Row_count:p},offs:[d,p],Row_count:p}}function getRowsOffsFT(e,t,i,r){let n,[o,s,f,h,l]=r,a=s.length,d=0;o&&(n=e.indexOf(s),d=-1==n?e.length:n+a),n=e.indexOf(s,d);let y,u,p=2,b=0,c=0,m=sbstr2="",w=[],x=[];for(;p;){if(h){let r=l.length;if(f)for(;-1!=n;){if(b>=t){if(!(b<i)){b++;break}if(m=e.slice(d,n),u=m.indexOf('"'),-1!=u)for(y0=0,y1=m.indexOf(l);-1!=y1;)sbstr2=m.slice(y0,y1),y1<u?(x.push(sbstr2),y0=y1+r,y1=m.indexOf(l,y0)):(z=y0,y=u+r,u=m.indexOf('"',y),-1!=u?(y1=m.indexOf(l,u+r),u=m.indexOf('"',y1+r),-1==u&&(u=m.length)):y1=m.indexOf(l,y));else for(y0=0,y1=m.indexOf(l);-1!=y1;)sbstr2=m.slice(y0,y1),x.push(sbstr2),y0=y1+r,y1=m.indexOf(l,y0);x.push(m.slice(y0)),w.push(x),x=[],c++}d=n+a,n=e.indexOf(s,d),b++}else for(;-1!=n;){if(!(b<i)){b++;break}if(b>=t){for(m=e.slice(d,n),y0=0,y1=m.indexOf(l);-1!=y1;)sbstr2=m.slice(y0,y1),x.push(sbstr2),y0=y1+r,y1=m.indexOf(l,y0);x.push(m.slice(y0)),w.push(x),x=[],c++}d=n+a,n=e.indexOf(s,d),b++}}else for(;-1!=n;){if(!(b<i)){b++;break}b>=t&&(x.push(e.slice(d,n)),w.push(x),x=[],c++),d=n+a,n=e.indexOf(s,d),b++}if(b>=i){d=n+a;break}p--,-1==n&&(p?n=e.length:d=n)}return{get:{Rows:w,Row_count:c},offs:[d,b],Row_count:c}}function getRowsOffsF(e,t,i){let r,[n,o,s,f,h]=i,l=o.length,a=0;n&&(r=e.indexOf(o),a=-1==r?e.length:r+l),r=e.indexOf(o,a);let d,y,u=2,p=0,b=0,c=sbstr2="",m=[],w=[];for(;u;){if(f){let i=h.length;if(s)for(;-1!=r;){if(p>=t){if(c=e.slice(a,r),y=c.indexOf('"'),-1!=y)for(y0=0,y1=c.indexOf(h);-1!=y1;)sbstr2=c.slice(y0,y1),y1<y?(w.push(sbstr2),y0=y1+i,y1=c.indexOf(h,y0)):(z=y0,d=y+i,y=c.indexOf('"',d),-1!=y?(y1=c.indexOf(h,y+i),y=c.indexOf('"',y1+i),-1==y&&(y=c.length)):y1=c.indexOf(h,d));else for(y0=0,y1=c.indexOf(h);-1!=y1;)sbstr2=c.slice(y0,y1),w.push(sbstr2),y0=y1+i,y1=c.indexOf(h,y0);w.push(c.slice(y0)),m.push(w),w=[],b++}a=r+l,r=e.indexOf(o,a),p++}else for(;-1!=r;){if(p>=t){for(c=e.slice(a,r),y0=0,y1=c.indexOf(h);-1!=y1;)sbstr2=c.slice(y0,y1),w.push(sbstr2),y0=y1+i,y1=c.indexOf(h,y0);w.push(c.slice(y0)),m.push(w),w=[],b++}a=r+l,r=e.indexOf(o,a),p++}}else for(;-1!=r;)p>=t&&(w.push(e.slice(a,r)),m.push(w),w=[],b++),a=r+l,r=e.indexOf(o,a),p++;u--,-1==r&&(u?r=e.length:a=r)}return{get:{Rows:m,Row_count:b},offs:[r,p],Row_count:b}}function get(){let e,t=Date.now(),i=offs_nRow(this.data,this.option);return e=getAllRows(this.data,this.info,i),{Time:Date.now()-t,Header:this.option.header?this.header:this.option.header,...e.get}}function chunk(e){let t,i=Date.now();if(!e||!Number.isInteger(e))throw new Error("The 'chunk' parameter must be an integer");if(!(e>=1))throw new Error("The 'chunk' parameter must be greater than or equal to 1");return t=getRowsChunk(this.data,e,this.info,this.offs_n),this.offs_n=t.offs,{Time:Date.now()-i,Header:this.option.header?this.header:this.option.header,...t.get}}function rowOffset(e,t){let i,r=Date.now();if(!Number.isInteger(e))throw new Error("The first parameter must be an integer");if(!(e>0))throw new Error("The first parameter must be greater than or equal to zero");if(null==t)i=getRowsOffsF(this.data,e,this.info);else{if(!Number.isInteger(t))throw new Error("The second parameter must be an integer");if(!(t>0))throw new Error("The second parameter must be greater than or equal to 1");i=getRowsOffsFT(this.data,e,t,this.info)}return i.Row_count&&(this.offs_n=i.offs),{Time:Date.now()-r,Header:this.option.header?this.header:this.option.header,...i.get}}function setRowOffset(e){if(null!=e){if(!Number.isInteger(e))throw new Error("The 'rowOffs' parameter must be an integer");if(!(e>=0))throw new Error("The 'rowOffs' parameter must be greater than or equal to zero");{let t=this.info[1].length;n=0,x0=0,x1=this.data.indexOf(this.info[1]);let i=this.data.length;for(;-1!=x1;){if(n==e)return i=this.option.header?x1+t:x0,this.offs_n=[i,n],[i,n];x0=x1+t,x1=this.data.indexOf(this.info[1],x0),n++}}}return!1}function resetOption(e){if(null!=e){if("object"!=typeof e)throw new Error("The second parameter must be an object");if("header"in e){if("boolean"!=typeof e.header)throw new Error("The 'header' key in The second parameter must be 'boolean'");this.option.header=e.header}if("quote"in e){if("boolean"!=typeof e.quote)throw new Error("The 'quote' key in The second parameter must be 'boolean'");this.option.quote=e.quote}if("linebreak"in e){if("string"!=typeof e.linebreak)throw new Error("The 'linebreak' key in The second parameter must be 'string'");this.option.linebreak=e.linebreak}if("delimiter"in e){if("string"!=typeof e.delimiter&&("boolean"!=typeof e.delimiter||e.delimiter))throw new Error("The 'delimiter' key in The second parameter must be 'string' or false 'boolean'");this.option.delimiter=e.delimiter,"boolean"===e.delimiter&&(this.column=!1)}}else this.option={header:!0,quote:!1,linebreak:"\r\n",delimiter:","};this.offs_n=offs_nRow(this.data,this.option),this.header=getColumns(this.data,this.option),this.info=[this.option.header,this.option.linebreak,this.option.quote,this.column,this.option.delimiter]}function getInfo(){return{offset:this.offs_n[0],rowOffset:this.offs_n[1],option:this.option}}function offs_nRow(e,t){let i,r=0,n=0;return t.header&&(i=e.indexOf(t.linebreak),n=-1==i?i=e.length:i+t.linebreak.length,r++),[n,r]}function getColumns(e,t){let i=!1;if(t.header){i=[];let r=t.delimiter.length;if(x0=0,x1=e.indexOf(t.linebreak),-1!=x1){let n,o=e.slice(x0,x1),s=0,f=o.indexOf(t.delimiter);for(;-1!=f;)n=o.slice(s,f),i.push(n),s=f+r,f=o.indexOf(t.delimiter,s);i.push(o.slice(s))}}return i}function parseCsv(e,t){let i,r=!0,n={header:!0,quote:!1,linebreak:"\r\n",delimiter:","};if(null==e)throw new Error("The first parameter 'file_path' must be string");if(!fs.existsSync(e))throw new Error(`File "${e}" does not exists`);if(i=fs.readFileSync(e,"utf8"),null!=t){if("object"!=typeof t)throw new Error("The second parameter must be an object");if("header"in t){if("boolean"!=typeof t.header)throw new Error("The 'header' key in The second parameter must be 'boolean'");n.header=t.header}if("quote"in t){if("boolean"!=typeof t.quote)throw new Error("The 'quote' key in The second parameter must be 'boolean'");n.quote=t.quote}if("linebreak"in t){if("string"!=typeof t.linebreak)throw new Error("The 'linebreak' key in The second parameter must be 'string'");n.linebreak=t.linebreak}if("delimiter"in t){if("string"!=typeof t.delimiter&&("boolean"!=typeof t.delimiter||t.delimiter))throw new Error("The 'delimiter' key in The second parameter must be 'string' or false 'boolean'");n.delimiter=t.delimiter,"boolean"===t.delimiter&&(r=!1)}}return new class{constructor(){this.option=n,this.data=i,this.column=r,this.offs_n=offs_nRow(this.data,this.option),this.header=getColumns(this.data,this.option),this.info=[this.option.header,this.option.linebreak,this.option.quote,this.column,this.option.delimiter]}get=get;chunk=chunk;setRowOffset=setRowOffset;rowOffset=rowOffset;getInfo=getInfo}}function parseText(e,t){let i,r={header:!0,quote:!1,linebreak:"\r\n",delimiter:","};if(null==e&&"string"==typeof e)throw new Error("The first parameter must be a file path string");if(i=e,null!=t){if("object"!=typeof t)throw new Error("The second parameter must be an object");if("header"in t){if("boolean"!=typeof t.header)throw new Error("The 'header' key in The second parameter must be 'boolean'");r.header=t.header}if("quote"in t){if("boolean"!=typeof t.quote)throw new Error("The 'quote' key in The second parameter must be 'boolean'");r.quote=t.quote}if("linebreak"in t){if("string"!=typeof t.linebreak)throw new Error("The 'linebreak' key in The second parameter must be 'string'");r.linebreak=t.linebreak}if("delimiter"in t){if("string"!=typeof t.delimiter&&("boolean"!=typeof t.delimiter||t.delimiter))throw new Error("The 'delimiter' key in The second parameter must be 'string' or false 'boolean'");r.delimiter=t.delimiter,"boolean"===t.delimiter&&(this.column=!1)}}return new class{constructor(){this.option=r,this.data=i,this.column=true,this.offs_n=offs_nRow(this.data,this.option),this.header=getColumns(this.data,this.option),this.info=[this.option.header,this.option.linebreak,this.option.quote,this.column,this.option.delimiter]}get=get;chunk=chunk;setRowOffset=setRowOffset;rowOffset=rowOffset;resetOption=resetOption;getInfo=getInfo}}module.exports={parseText:parseText,parseCsv:parseCsv};
|