Skip to content

Commit

Permalink
Merge pull request #4 from bigman73/master
Browse files Browse the repository at this point in the history
Added support for skipHeader option
  • Loading branch information
danielgindi authored May 5, 2018
2 parents eeace16 + 42abb47 commit 8ff2eea
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ A CSV stream reader, with many many features, and ability to work with the large
* Support for excel-style multiline cells wrapped in quotes
* Choosing a different delimiter instead of the comma
* Automatic skipping empty lines
* Automatic skipping of the first header row
* Automatic parsing of numbers and booleans
* Automatic trimming
* 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.
Expand All @@ -29,6 +30,7 @@ Name | Type | Default | Explanation
`multiline` | `Boolean` | `true` | Allow multiline cells, when the cell is wrapped with quotes ("...\n...")
`allowQuotes` | `Boolean` | `true` | Should quotes be treated as a special character that wraps cells etc.
`skipEmptyLines` | `Boolean` | `false` | Should empty lines be automatically skipped?
`skipHeader` | `Boolean` | `false` | Should the first header row be skipped?
`parseNumbers` | `Boolean` | `false` | Should numbers be automatically parsed? This will parse any format supported by `parseFloat` including scientific notation, `Infinity` and `NaN`.
`parseBooleans` | `Boolean` | `false` | Automatically parse booleans (strictly lowercase `true` and `false`)
`ltrim` | `Boolean` | `false` | Automatically left-trims columns
Expand Down
13 changes: 12 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ var Transform = stream.Transform;
* @param {Boolean=false} options.ltrim - Automatically left-trims columns
* @param {Boolean=false} options.rtrim - Automatically right-trims columns
* @param {Boolean=false} options.trim - If true, then both 'ltrim' and 'rtrim' are set to true
* @param {Boolean=false} options.skipheader - If true, then skip the first header row
* @returns {CsvReadableStream}
* @constructor
*/
Expand All @@ -42,6 +43,7 @@ var CsvReadableStream = function (options) {
, lastLineEndCR = false
, lookForBOM = true
, isQuoted = false
, rowCount = 0

, multiline = !!options.multiline || typeof options.multiline === 'undefined'
, delimiter = options.delimiter != null ? options.delimiter.toString() || ',' : ','
Expand All @@ -52,6 +54,7 @@ var CsvReadableStream = function (options) {
, ltrim = !!options.ltrim || !!options.trim
, rtrim = !!options.rtrim || !!options.trim
, trim = options.ltrim && options.rtrim
, skipHeader = options.skipHeader

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

Expand Down Expand Up @@ -119,6 +122,7 @@ var CsvReadableStream = function (options) {
lastLineEndCR = c === '\r';
dataIndex++;
isFinishedLine = true;
rowCount++;

if (!multiline) {
isQuoted = false;
Expand Down Expand Up @@ -172,7 +176,14 @@ var CsvReadableStream = function (options) {
data = null;
}

if (isFinishedLine || (data === null && this._isStreamDone)) {
if (isFinishedLine && skipHeader && rowCount === 1) {
column = '';
columns = [];
// Look to see if there are more rows in available data
this._processChunk();
return;
}
else if (isFinishedLine || (data === null && this._isStreamDone)) {

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

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "A CSV stream reader, with many many features, and ability to work with the largest datasets",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "node test/test.js"
},
"repository": {
"type": "git",
Expand Down
3 changes: 3 additions & 0 deletions test/test-header.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
NAME,AGE
John Smith,50
Jane Doe,25
17 changes: 17 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
var fs = require('fs');
var CsvReadableStream = require('../index.js');

var inputStream = fs.createReadStream('test/test-header.csv', 'utf8');

inputStream
.pipe(CsvReadableStream({
parseNumbers: true,
parseBooleans: true,
trim: true,
skipHeader: true }))
.on('data', function (row) {
console.log('A row arrived: ', row);
})
.on('end', function (data) {
console.log('No more rows!');
});

0 comments on commit 8ff2eea

Please sign in to comment.