select-csv 1.1.21 → 1.1.22

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.
Files changed (2) hide show
  1. package/README.md +358 -126
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -4,7 +4,7 @@ The fastest, simplest, and most powerful CSV parser for Node.js. Optimized for h
4
4
 
5
5
  `select-csv` converts `.csv` files into arrays, JSON objects, or raw lines. It provides two main functions, `parseCsv` (for local files) and `parseText` (for raw strings), both sharing the same methods and features.
6
6
 
7
- ##Key Features
7
+ ## Key Features
8
8
  - **Ultra-Lightweight:** Package size is less than 30 KB.
9
9
  - **Fast Mode:** Synchronous execution for maximum speed and zero overhead.
10
10
  - **Memory Efficient:** Streams large files using chunks and row offsets instead of loading the entire file into RAM.
@@ -14,21 +14,32 @@ The fastest, simplest, and most powerful CSV parser for Node.js. Optimized for h
14
14
 
15
15
  ---
16
16
 
17
- ## 🚀 Installation
18
17
 
19
- ```bash
20
- npm install select-csv
21
- 🛠 Usage Examples
22
- Initialization
23
- JavaScript
18
+ Install:
19
+ -------
24
20
 
25
- const { parseCsv, parseText } = require("select-csv");
26
- let parse;
21
+ select-csv is available on [npm](https://www.npmjs.com/package/select-csv). It
22
+ can be installed with the following command:
27
23
 
28
- // 1. Create object from a local .csv file
24
+ npm install select-csv
25
+
26
+
27
+
28
+ Usage:
29
+ -------
30
+
31
+ Here there are clearly different examples
32
+
33
+
34
+ ```js
35
+ const {parseCsv,parseText} = require("select-csv");
36
+
37
+ var parse;
38
+
39
+ // First create object from .csv file
29
40
  parse = parseCsv('file_path.csv');
30
41
 
31
- // 2. Create object from raw text string
42
+ // Or if you just want create object from text
32
43
  parse = parseText(
33
44
  `Index,User Id,First Name,Last Name,Sex
34
45
  1,5f10e9D33fC5f2b,Sara,Mcguire,Female
@@ -42,177 +53,398 @@ parse = parseText(
42
53
  9,F563CcbFBfEcf5a,Emma,Robinson,Female
43
54
  10,f2dceFc00F62542,Pedro,Cordova,Male`
44
55
  );
45
- Get All Rows
46
- JavaScript
47
56
 
48
- const result = parse.get();
57
+
58
+ ```
59
+
60
+ * If you want to get all rows :
61
+ ```js
62
+ const result = parse.get(); //Return all rows
49
63
  /*
50
- Returns:
51
- {
52
- time: '1 ms',
53
- header: ["Index","User Id","First Name","Last Name","Sex"],
54
- rows: [
55
- ["1","5f10e9D33fC5f2b","Sara","Mcguire","Female"],
56
- ["2","751cD1cbF77e005","Alisha","Hebert","Male"],
57
- ...
58
- ],
59
- row_count: 10
60
- }
64
+ {
65
+ time:1,
66
+ header:["Index","User Id","First Name","Last Name","Sex"],
67
+ rows:[
68
+ ["1","5f10e9D33fC5f2b","Sara","Mcguire","Female"],
69
+ ["2","751cD1cbF77e005","Alisha","Hebert","Male"],
70
+ ["3","DcEFDB2D2e62bF9","Gwendolyn","Sheppard","Male"],
71
+ ["4","C88661E02EEDA9e","Kristine","Mccann","Female"],
72
+ ["5","fafF1aBDebaB2a6","Bobby","Pittman","Female"],
73
+ ["6","BdDb6C8Af309202","Calvin","Ramsey","Female"],
74
+ ["7","FCdfFf08196f633","Collin","Allison","Male"],
75
+ ["8","356279dAa0F7CbD","Nicholas","Branch","Male"],
76
+ ["9","F563CcbFBfEcf5a","Emma","Robinson","Female"],
77
+ ["10","f2dceFc00F62542","Pedro","Cordova","Male"]
78
+ ],
79
+ row_count:10
80
+ }
61
81
  */
62
- Parsing in Chunks
63
- The chunk(c) method allows you to fetch a specific number of rows. The parser saves the current offset automatically.
64
82
 
65
- JavaScript
83
+ ```
84
+
85
+ * If you want to get a chunks of rows :
66
86
 
67
- let result;
87
+ ```js
88
+ var result;
89
+ result = parse.chunk(c)
90
+ //The 'c' parameter must be an integer and greater than or equal to 1
68
91
 
69
- // Get rows 0 and 1
70
- result = parse.chunk(2);
92
+ //Examples:
93
+ result = parse.chunk(2) //Return row 0 and 1
71
94
  /*
72
95
  {
73
- "time": "0 ms",
74
- "header": [ "Index", "User Id", "First Name", "Last Name", "Sex" ],
75
- "rows": [
96
+ "Time": 0,
97
+ "Header": [ "Index", "User Id", "First Name", "Last Name", "Sex" ],
98
+ "Rows": [
76
99
  [ "1", "5f10e9D33fC5f2b", "Sara", "Mcguire", "Female" ],
77
100
  [ "2", "751cD1cbF77e005", "Alisha", "Hebert", "Male" ]
78
101
  ],
79
- "row_count": 2
102
+ "row_count:": 2
80
103
  }
81
104
  */
82
105
 
83
- // Get rows 2, 3, and 4 (continues from last offset)
84
- result = parse.chunk(3);
106
+ result = parse.chunk(3) //Return row 2,3 and 4 (Get rows from last offset saved)
107
+ /*
108
+ {
109
+ "Time": 0,
110
+ "Header": [ "Index", "User Id", "First Name", "Last Name", "Sex" ],
111
+ "Rows": [
112
+ [ "3", "DcEFDB2D2e62bF9", "Gwendolyn", "Sheppard", "Male" ],
113
+ [ "4", "C88661E02EEDA9e", "Kristine", "Mccann", "Female" ],
114
+ [ "5", "fafF1aBDebaB2a6", "Bobby", "Pittman", "Female" ]
115
+ ],
116
+ "row_count:": 3
117
+ }
118
+ */
119
+
120
+ result = parse.chunk(1) //Return row 5 (Get rows from last offset saved)
121
+ /*
122
+ {
123
+ "Time": 0,
124
+ "Header": [ "Index", "User Id", "First Name", "Last Name", "Sex" ],
125
+ "Rows": [
126
+ [ "6", "BdDb6C8Af309202", "Calvin", "Ramsey", "Female" ]
127
+ ],
128
+ "row_count:": 1
129
+ }
130
+ */
131
+
132
+
133
+ ```
134
+
135
+ * If you want to get specific rows :
85
136
 
86
- // Get row 5
87
- result = parse.chunk(1);
88
- Row Offsets (Specific Range)
89
- Use rowOffset(from, to) to fetch a specific range of rows.
137
+ ```js
138
+ var result
139
+ result = parse.rowOffset(from)
140
+ // The 'from' parameter must be an integer and greater than or equal to 0
90
141
 
91
- JavaScript
142
+ // Or
143
+ result = parse.rowOffset(from,to)
144
+ // The 'to' parameter must be an integer and greater than or equal to 1
92
145
 
93
- // Get all rows from the 6th row to the last row
94
- let result = parse.rowOffset(6);
146
+ //Examples:
147
+ result = parse.rowOffset(6) //Returns all rows from the sixth row to the last row
148
+ /*
149
+ {
150
+ "Time": 0,
151
+ "Header": [ "Index", "User Id", "First Name", "Last Name", "Sex" ],
152
+ "Rows": [
153
+ [ "7", "FCdfFf08196f633", "Collin", "Allison", "Male" ],
154
+ [ "8", "356279dAa0F7CbD", "Nicholas", "Branch", "Male" ],
155
+ [ "9", "F563CcbFBfEcf5a", "Emma", "Robinson", "Female" ],
156
+ [ "10", "f2dceFc00F62542", "Pedro", "Cordova", "Male" ]
157
+ ],
158
+ "row_count:": 4
159
+ }
160
+ */
95
161
 
96
- // Get rows from 5th to 8th row
97
- result = parse.rowOffset(5, 8);
162
+ result = parse.rowOffset(5,8) //Returns all rows from 5th to 8th row
98
163
  /*
99
164
  {
100
- "time": "1 ms",
101
- "header": [ "Index", "User Id", "First Name", "Last Name", "Sex" ],
102
- "rows": [
165
+ "Time": 1,
166
+ "Header": [ "Index", "User Id", "First Name", "Last Name", "Sex" ],
167
+ "Rows": [
103
168
  [ "6", "BdDb6C8Af309202", "Calvin", "Ramsey", "Female" ],
104
169
  [ "7", "FCdfFf08196f633", "Collin", "Allison", "Male" ],
105
170
  [ "8", "356279dAa0F7CbD", "Nicholas", "Branch", "Male" ]
106
171
  ],
107
- "row_count": 3
172
+ "row_count:": 3
108
173
  }
109
174
  */
110
- Manual Offset Control
111
- You can manually set the row pointer using setRowOffset(offs).
112
175
 
113
- JavaScript
114
176
 
115
- // If offset exists, returns [byte_offset, row_number]
116
- let status = parse.setRowOffset(5); // [236, 5]
117
177
 
118
- // Get row 6 after setting offset
119
- let nextRow = parse.chunk(1);
178
+ ```
120
179
 
121
- // Returns false if row number does not exist
122
- let fail = parse.setRowOffset(20); // false
123
- ⚙️ Configuration Options
124
- The default configuration is:
180
+ * If you want to change the row offset :
125
181
 
126
- JavaScript
182
+ ```js
183
+ parse.setRowOffset(offs)
127
184
 
185
+ // The 'offs' parameter must be an integer and greater than or equal to 0.
186
+
187
+ // If the offset exists, return [offset,row_number].
188
+ result = parse.setRowOffset(5)
189
+ /*
190
+ [236,5]
191
+ */
192
+ result = parse.chunk(1) // Get rows from last offset saved
193
+ /*
128
194
  {
129
- 'header': true, // Treat first row as header
130
- 'quote': false, // Handle quoted values
131
- 'linebreak': '\r\n', // Custom line break
132
- 'delimiter': ",", // Column separator (set to false for raw lines)
133
- 'json': false, // Return rows as JSON objects (requires header: true)
134
- 'bufferSize': 1048576 // 1MB buffer (for parseCsv only)
195
+ "Time": 0,
196
+ "Header": [ "Index", "User Id", "First Name", "Last Name", "Sex" ],
197
+ "Rows": [
198
+ [ "6", "BdDb6C8Af309202", "Calvin", "Ramsey", "Female" ]
199
+ ],
200
+ "row_count:": 1
135
201
  }
136
- Custom Options Example:
137
- JavaScript
202
+ */
138
203
 
139
- let option = {
140
- 'header': false,
141
- 'quote': true,
142
- 'linebreak': '\n',
143
- 'delimiter': ",",
144
- 'json': false,
145
- 'bufferSize': 2000
146
- };
204
+ // If not , returns false and the offset not changed.
205
+ result = parse.setRowOffset(20)
206
+ /*
207
+ false
208
+ */
209
+ result = parse.chunk(1) // Get rows from last offset saved
210
+ /*
211
+ {
212
+ "Time": 0,
213
+ "Header": [ "Index", "User Id", "First Name", "Last Name", "Sex" ],
214
+ "Rows": [
215
+ [ "7", "FCdfFf08196f633", "Collin", "Allison", "Male" ]
216
+ ],
217
+ "row_count:": 1
218
+ }
219
+ */
220
+
221
+
222
+ ```
223
+
224
+ * The default object option :
147
225
 
148
- parse = parseCsv('data.csv', option);
149
- Working with JSON Output:
150
- If you want the result as an array of objects based on the header:
226
+ ```js
227
+ {
228
+ 'header': true,
229
+ 'quote': false,
230
+ 'linebreak': '\r\n',
231
+ 'delimiter': ",",
232
+ 'bufferSize':1024*1024
233
+ }
234
+ // delimiter: (String: get rows containing columns, false: get lines without columns)
235
+ //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
236
+ ```
237
+
238
+ * If you want to use specific option :
239
+ ```js
240
+ var option = {
241
+ 'header': false, /* or true */
242
+ 'quote': true, /* or false */
243
+ 'linebreak': '\n', /* '\n' or '\r' or any other string */
244
+ 'delimiter': "," /* ';' or any other string or false */
245
+ 'bufferSize':2000 /* It only works with a CSV file */
246
+ }
151
247
 
152
- JavaScript
248
+ var parse;
249
+ // Create object from .csv file
250
+ parse = parseCsv('file_path.csv',option);
153
251
 
154
- const parse = parseCsv('data.csv', { 'header': true, 'json': true });
155
- const result = parse.get();
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)
156
265
  /*
157
- rows: [
158
- { "Index": "1", "User Id": "5f10e9D33fC5f2b", "First Name": "Sara", ... },
159
- ...
160
- ]
266
+ {
267
+ "Time": 0,
268
+ "Header": false,
269
+ "Rows": [
270
+ [ "2", "751cD1cbF77e005", "Alisha", "Hebert", "Male" ]
271
+ ],
272
+ "row_count:": 1
273
+ }
161
274
  */
162
- Raw Lines (No Delimiter):
163
- If delimiter is set to false, the parser returns full lines as strings.
164
275
 
165
- JavaScript
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
+ */
166
291
 
167
- parse.resetOption({ 'header': true, 'delimiter': false });
168
- let result = parse.rowOffset(2);
169
- // rows: [ ["2,751cD1cbF77e005,Alisha,Hebert,Male"] ]
170
- 📊 Benchmarking Large Files
171
- Tested with a 100 Million Row Dataset:
292
+ ```
172
293
 
173
- JavaScript
294
+ * If you want to 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
+ }
174
301
 
175
- const parse = parseCsv('huge-data.csv', {"header": false});
302
+ parse.resetOption(option); // All saved values are erased and the object is restared again
176
303
 
177
- // Fetch 100,000 rows
178
- let result = parse.chunk(100000); // ~222ms
179
304
 
180
- // Jump to row 30,000,000
181
- result = parse.rowOffset(30000000, 30000005); // ~3743ms
305
+ ```
182
306
 
183
- // Jump to row 90,000,000
184
- result = parse.rowOffset(90000000, 90000004); // ~44126ms
307
+ * If you want to get information of your object :
308
+ ```js
185
309
 
186
- // Current metadata
187
- console.log(parse.getInfo());
310
+ const result = parse.getInfo();
188
311
  /*
189
312
  {
190
- offset: 3599945660,
191
- rowOffset: 90000008,
192
- option: { ... }
313
+ "offset": 275,
314
+ "rowOffset": 7,
315
+ "option": {
316
+ "header": false,
317
+ "quote": false,
318
+ "linebreak": "\n",
319
+ "delimiter": false
320
+ }
193
321
  }
194
322
  */
195
- 🧪 Advanced Methods
196
- getInfo()
197
- Returns current byte offset, row offset, and active options.
198
323
 
199
- JavaScript
324
+ ```
325
+
326
+ * Examples of parsing a large CSV file:
327
+ (https://www.kaggle.com/datasets/zanjibar/100-million-data-csv)
200
328
 
201
- const info = parse.getInfo();
202
- resetOption(newOptions)
203
- Clears internal state and re-initializes the parser with new settings.
204
329
 
205
- JavaScript
330
+ ```js
206
331
 
207
- parse.resetOption({ 'header': false, 'quote': true });
208
- header()
209
- Returns the detected CSV header array.
332
+ const parse = parseCsv('100-million-data.csv',{"header": false});
333
+ var result;
334
+ result = parse.chunk(100000)
335
+ /*
336
+ {
337
+ time: 222,
338
+ header: false,
339
+ rows: [
340
+ [ '198801', '1', '103', '100', '000000190', '0', '35843', '34353' ],
341
+ [ '198801', '1', '103', '100', '120991000', '0', '1590', '4154' ],
342
+ [ '198801', '1', '103', '100', '210390900', '0', '4500', '2565' ],
343
+ .
344
+ .
345
+ .
346
+ [ '198801', '1', '103', '100', '391590000', '0', '95000', '7850' ],
347
+ [ '198801', '1', '103', '100', '391620000', '0', '1000', '404' ],
348
+ [ '198801', '1', '103', '100', '391723000', '0', '545', '479' ],
349
+ [ '198801', '1', '103', '100', '391732100', '0', '24', '393' ],
350
+ [ '198801', '1', '103', '100', '391732900', '0', '60', '758' ],
351
+ [ '198801', '1', '103', '100', '391810100', '0', '1935', '1042' ],
352
+ [ '198801', '1', '103', '100', '391910200', '0', '510', '1303' ],
353
+ [ '198801', '1', '103', '100', '391910300', '0', '133', '379' ],
354
+ [ '198801', '1', '103', '100', '391990300', '0', '450', '1668' ],
355
+ [ '198801', '1', '103', '100', '391990500', '0', '942', '1721' ],
356
+ [ '198801', '1', '103', '100', '391990900', '0', '40', '235' ],
357
+ [ '198801', '1', '103', '100', '392030000', '0', '406', '652' ],
358
+ ... 99900 more items
359
+ ],
360
+ row_count: 100000
361
+ }
362
+ */
210
363
 
211
- JavaScript
364
+ result = parse.chunk(3) // Return row 100001,100002 and 100003 (Get rows from last offset saved)
365
+ /*
366
+ {
367
+ time: 1,
368
+ header: false,
369
+ rows: [
370
+ [ '198801', '1', '326', '500', '841330000', '90', '81', '246' ],
371
+ [ '198801', '1', '326', '500', '841510000', '0', '35', '1366' ],
372
+ [ '198801', '1', '326', '500', '841582100', '0', '6', '334' ]
373
+ ],
374
+ row_count: 3
375
+ }
376
+ */
377
+
378
+ const from = 1000*1000*30;
379
+ const to = from + 5;
380
+ result = parse.rowOffset(from,to)
381
+ /*
382
+ {
383
+ time: 3743,
384
+ header: false,
385
+ rows: [
386
+ [
387
+ '199804', '2',
388
+ '213', '502',
389
+ '848130000', '16035',
390
+ '746', '8380'
391
+ ],
392
+ [ '199804', '2', '213', '502', '848140000', '168', '152', '1891' ],
393
+ [ '199804', '2', '213', '502', '848180010', '77', '404', '1366' ],
394
+ [ '199804', '2', '213', '502', '848190000', '0', '131', '570' ],
395
+ [ '199804', '2', '213', '502', '848230000', '300', '4', '882' ]
396
+ ],
397
+ row_count: 5
398
+ }
399
+ */
400
+
401
+ const from = 1000*1000*90;
402
+ const to = from + 4;
403
+ result = parse.rowOffset(from,to)
404
+ /*
405
+ {
406
+ time: 44126,
407
+ header: false,
408
+ rows: [
409
+ [ '201412', '1', '125', '400', '283525000', '0', '160000', '6492' ],
410
+ [ '201412', '1', '125', '400', '390740100', '0', '17500', '5579' ],
411
+ [ '201412', '1', '125', '400', '390950000', '0', '36000', '21423' ],
412
+ [ '201412', '1', '125', '400', '392329000', '0', '520', '1413' ]
413
+ ],
414
+ row_count: 4
415
+ }
416
+ */
417
+
418
+ result = parse.chunk(3) // Get rows from last offset saved ( row to,to+1 and to+2 )
419
+ /*
420
+ {
421
+ time: 29,
422
+ header: false,
423
+ rows: [
424
+ [ '201412', '1', '125', '400', '400932000', '0', '18', '526' ],
425
+ [ '201412', '1', '125', '400', '401110000', '173', '1735', '1197' ],
426
+ [ '201412', '1', '125', '400', '401120000', '133', '1707', '1099' ]
427
+ ],
428
+ row_count: 3
429
+ }
430
+ */
431
+
432
+ result = parse.getInfo() // Get all the information
433
+ /*
434
+ {
435
+ offset: 3599945660,
436
+ rowOffset: 90000008,
437
+ option: {
438
+ header: false,
439
+ quote: false,
440
+ linebreak: '\r\n',
441
+ delimiter: ',',
442
+ bufferSize: 1048576
443
+ }
444
+ }
445
+ */
446
+ ```
212
447
 
213
- const headers = parse.header();
214
- 🤝 Contributing
215
- Found a bug or have a suggestion? Open an issue at GitHub Issues.
448
+ ## 📄 License
216
449
 
217
- 📄 License
218
- This project is licensed under the MIT License.
450
+ MIT License
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "select-csv",
3
- "version": "1.1.21",
3
+ "version": "1.1.22",
4
4
  "description": "A high-performance, memory-efficient CSV parser for Node.js. Supports streaming large files via chunks and row offsets with zero dependencies.",
5
5
  "keywords": [
6
6
  "csv",