Skip to content

Commit 8ff2eea

Browse files
authored
Merge pull request #4 from bigman73/master
Added support for skipHeader option
2 parents eeace16 + 42abb47 commit 8ff2eea

File tree

5 files changed

+35
-2
lines changed

5 files changed

+35
-2
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ A CSV stream reader, with many many features, and ability to work with the large
99
* Support for excel-style multiline cells wrapped in quotes
1010
* Choosing a different delimiter instead of the comma
1111
* Automatic skipping empty lines
12+
* Automatic skipping of the first header row
1213
* Automatic parsing of numbers and booleans
1314
* Automatic trimming
1415
* Being a stream transformer, you can `.pause()` if you need some time to process the row and `.resume()` when you are ready to receive and process more rows.
@@ -29,6 +30,7 @@ Name | Type | Default | Explanation
2930
`multiline` | `Boolean` | `true` | Allow multiline cells, when the cell is wrapped with quotes ("...\n...")
3031
`allowQuotes` | `Boolean` | `true` | Should quotes be treated as a special character that wraps cells etc.
3132
`skipEmptyLines` | `Boolean` | `false` | Should empty lines be automatically skipped?
33+
`skipHeader` | `Boolean` | `false` | Should the first header row be skipped?
3234
`parseNumbers` | `Boolean` | `false` | Should numbers be automatically parsed? This will parse any format supported by `parseFloat` including scientific notation, `Infinity` and `NaN`.
3335
`parseBooleans` | `Boolean` | `false` | Automatically parse booleans (strictly lowercase `true` and `false`)
3436
`ltrim` | `Boolean` | `false` | Automatically left-trims columns

index.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ var Transform = stream.Transform;
2020
* @param {Boolean=false} options.ltrim - Automatically left-trims columns
2121
* @param {Boolean=false} options.rtrim - Automatically right-trims columns
2222
* @param {Boolean=false} options.trim - If true, then both 'ltrim' and 'rtrim' are set to true
23+
* @param {Boolean=false} options.skipheader - If true, then skip the first header row
2324
* @returns {CsvReadableStream}
2425
* @constructor
2526
*/
@@ -42,6 +43,7 @@ var CsvReadableStream = function (options) {
4243
, lastLineEndCR = false
4344
, lookForBOM = true
4445
, isQuoted = false
46+
, rowCount = 0
4547

4648
, multiline = !!options.multiline || typeof options.multiline === 'undefined'
4749
, delimiter = options.delimiter != null ? options.delimiter.toString() || ',' : ','
@@ -52,6 +54,7 @@ var CsvReadableStream = function (options) {
5254
, ltrim = !!options.ltrim || !!options.trim
5355
, rtrim = !!options.rtrim || !!options.trim
5456
, trim = options.ltrim && options.rtrim
57+
, skipHeader = options.skipHeader
5558

5659
, postProcessingEnabled = parseNumbers || parseBooleans || ltrim || rtrim;
5760

@@ -119,6 +122,7 @@ var CsvReadableStream = function (options) {
119122
lastLineEndCR = c === '\r';
120123
dataIndex++;
121124
isFinishedLine = true;
125+
rowCount++;
122126

123127
if (!multiline) {
124128
isQuoted = false;
@@ -172,7 +176,14 @@ var CsvReadableStream = function (options) {
172176
data = null;
173177
}
174178

175-
if (isFinishedLine || (data === null && this._isStreamDone)) {
179+
if (isFinishedLine && skipHeader && rowCount === 1) {
180+
column = '';
181+
columns = [];
182+
// Look to see if there are more rows in available data
183+
this._processChunk();
184+
return;
185+
}
186+
else if (isFinishedLine || (data === null && this._isStreamDone)) {
176187

177188
if (columns.length || column || data || !this._isStreamDone) {
178189

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "A CSV stream reader, with many many features, and ability to work with the largest datasets",
55
"main": "index.js",
66
"scripts": {
7-
"test": "echo \"Error: no test specified\" && exit 1"
7+
"test": "node test/test.js"
88
},
99
"repository": {
1010
"type": "git",

test/test-header.csv

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
NAME,AGE
2+
John Smith,50
3+
Jane Doe,25

test/test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
var fs = require('fs');
2+
var CsvReadableStream = require('../index.js');
3+
4+
var inputStream = fs.createReadStream('test/test-header.csv', 'utf8');
5+
6+
inputStream
7+
.pipe(CsvReadableStream({
8+
parseNumbers: true,
9+
parseBooleans: true,
10+
trim: true,
11+
skipHeader: true }))
12+
.on('data', function (row) {
13+
console.log('A row arrived: ', row);
14+
})
15+
.on('end', function (data) {
16+
console.log('No more rows!');
17+
});

0 commit comments

Comments
 (0)