Skip to content

Commit 3186dbe

Browse files
committed
updated readme
1 parent d9c9d81 commit 3186dbe

File tree

1 file changed

+83
-22
lines changed

1 file changed

+83
-22
lines changed

README.md

Lines changed: 83 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ A CSV file reader, with many features, and ability to work with large datasets.
1212
* Automatic parsing of numbers and booleans
1313
* Automatic trimming
1414
* You can `.pause()` if you need some time to process the current row and `.resume()` when you are ready to receive and process more rows.
15+
* You can `.end()` if you want to end the process. After this method is called the process will end and no more rows will be parsed.
1516
* Consumes and emits rows one-by-one, allowing you to process datasets in any size imaginable.
1617

1718
## Installation:
@@ -25,58 +26,118 @@ npm install simple-csv-processor
2526
Name | Type | Default | Explanation
2627
---- | ---- | ------- | -----------
2728
`delimiter` | `String` | `,` | The character that separates between cells.
29+
`allowSpecialQuotes` | `Boolean` | `true` | Should quotes be treated as a special character that wraps cells.
30+
`quote` | `String` | `"` | If `allowSpecialQuotes` is true, this will specify the quote character.
31+
`skipComments` | `Boolean | String` | `false` | If true, lines which begin with # will be skipped. To use a custom character passe it as a sring.
32+
`skipLines` | `Number` | `0` | Specifies the number of lines at the beginning of the file to skip over.
2833
`skipEmptyLines` | `Boolean` | `false` | Should empty lines be automatically skipped?
29-
`skipHeader` | `Boolean` | `false` | Should the first header row be skipped?
30-
`rowAsObject` | `Boolean` | `false` | If true, each row will be converted automatically to an object based on the header. This implied `skipHeader=true`.
3134
`parseNumbers` | `Boolean` | `false` | Should numbers be automatically parsed? This will parse any format supported by `parseFloat`.
3235
`parseBooleans` | `Boolean` | `false` | Automatically parse booleans (Auto conversion to lowercase `true` and `false`).
3336
`ltrim` | `Boolean` | `false` | Automatically left-trims columns.
3437
`rtrim` | `Boolean` | `false` | Automatically right-trims columns.
35-
`trim` | `Boolean` | `false` | If true, then both 'ltrim' and 'rtrim' are set to true.
38+
`trim` | `Boolean` | `false` | If true, trim all columns.
39+
`maxRowBytes` | `Number` | `MAX_ROW_BYTES` | Specifies the maximum number of bytes per row, the default value is on 10 peta byte.
40+
`rowAsObject` | `Boolean` | `false` | If true, each row will be converted automatically to an object based on the header. This implies `skipLines=1 & strict=true`.
41+
`strict` | `Boolean` | `false` | If true, the number of columns in each row must match the number of headers.
3642
`errorLog` | `Boolean` | `false` | If true, errors will be logged to the console whether the `error` event is used or not.
3743

3844
## Events:
3945

4046
A `'row'` event will be emitted with each row, either in an array format (`(string|number|boolean)[]`) or an Object format (`Object<string, (string|number|boolean)>`), depending on the `rowAsObject` option.
4147
A preliminary `'header'` event will be emitted with the first row, only in an array format.
42-
Another 2 events usefull events: `finish` and `error`.
48+
2 more usefull events: `finish` and `error`.
4349

44-
## Usage example:
50+
## Basic usage example:
51+
52+
Suppose you have a CSV file data.csv which contains the data:
53+
54+
```
55+
Name, Age
56+
Jone Doe,24
57+
Tom Doe,22
58+
```
59+
60+
It could then be parsed, and results shown like so:
61+
62+
```javascript
63+
document.querySelector('#csv-input').addEventListener('change', processCSVFile);
64+
65+
function processCSVFile(e){
66+
let file = e.target.files[0];
67+
let process = new ProcessCSV({
68+
delimiter: ',',
69+
parseBooleans: true,
70+
parseNumbers: true,
71+
trim: true,
72+
strict: true,
73+
rowAsObject: false,
74+
})
75+
.process(file)
76+
.on('header', header => {
77+
// If rowAsObject is set to true, the header event will not be emitted.
78+
console.log("header: ", header); // Header: ['Name', 'Age']
79+
})
80+
.on('row', row => {
81+
console.log("row: ", row); // First row: ['Jone Doe', 24]
82+
83+
// If rowAsObject is set to true, the row will be converted to an object.
84+
// First row: {Name: 'Jone Doe', Age: 24}
85+
})
86+
.on('finish', () => {
87+
// Once the all rows have been parsed, this event will be emitted.
88+
console.log("No more rows!");
89+
})
90+
.on('error', error => {
91+
// Any errors will be in this block.
92+
console.error(error);
93+
});
94+
}
95+
96+
```
97+
98+
## Advanced usage example:
99+
100+
When working with large datasets, these options might come in handy. Also the `pause` & `resume` event will be needed. Here's an example using
101+
all the available features:
45102

46103
```javascript
47104

48105
let file = e.target.files[0];
49106
let process = new ProcessCSV({
107+
// seperator
50108
delimiter: ',',
109+
// skippers
110+
skipLines: 0,
51111
skipEmptyLines: true,
52-
skipHeader: false,
53-
rowAsObject: false,
54-
parseNumbers: true,
112+
skipComments: "#",
113+
// conversion
55114
parseBooleans: true,
115+
parseNumbers: true,
116+
// quote handler
117+
allowSpecialQuotes: true,
118+
quote: '"',
119+
// more options
56120
trim: true,
57-
errorLog: false,
58-
})
59-
.processFile(file)
121+
strict: true,
122+
maxRowBytes: 200000,
123+
}).process(file)
60124
.on('header', header => {
61125
console.log("header: ", header);
62126
})
63-
.on('row', rowData => {
64-
console.log("row: ", rowData);
65-
66-
// you can pause receiving more rows if you need time to process the current one.
127+
.on('row', row => {
67128
process.pause();
68-
setTimeout(() => {
69-
// then you can resume
70-
process.resume();
71-
}, 100);
129+
130+
if(false)
131+
process.end(); // end the process
132+
else
133+
process.resume(); // receive the next row
72134
})
73135
.on('finish', () => {
74-
console.log("No more rows!");
136+
console.log("No more rows to process!")
75137
})
76138
.on('error', error => {
77-
console.error(error);
139+
console.error(error)
78140
});
79-
80141
```
81142

82143
## Contributing

0 commit comments

Comments
 (0)