diff --git a/index.js b/index.js index 397e840..367e6f0 100644 --- a/index.js +++ b/index.js @@ -183,7 +183,7 @@ class CsvParser extends Transform { cells.push(this._empty) } - const skip = this.skipLines && this.skipLines !== this._line + const skip = this.skipLines && this.skipLines > this._line this._line++ if (this._first && !skip) { @@ -198,7 +198,7 @@ class CsvParser extends Transform { const e = new RangeError('Row length does not match headers') this.emit('error', e) } else { - if (!this._first) this._emit(this._Row, cells) + if (!skip) this._emit(this._Row, cells) } } diff --git a/test/skipLines.test.js b/test/skipLines.test.js index e673efe..3f39bd1 100644 --- a/test/skipLines.test.js +++ b/test/skipLines.test.js @@ -2,12 +2,25 @@ const test = require('ava') const { collect } = require('./helpers/helper') -test.cb('skip rows until', (t) => { +test.cb('skip lines', (t) => { const verify = (err, lines) => { t.false(err, 'no err') + t.is(lines.length, 1, '1 row') t.is(JSON.stringify(lines[0]), JSON.stringify({yes: 'ok', yup: 'ok', yeah: 'ok!'})) t.end() } collect('junk_rows.csv', {skipLines: 2}, verify) }) + +test.cb('skip lines with headers', (t) => { + const verify = (err, lines) => { + t.false(err, 'no err') + t.is(lines.length, 2, '2 rows') + t.is(JSON.stringify(lines[0]), JSON.stringify({s: 'yes', p: 'yup', h: 'yeah'})) + t.is(JSON.stringify(lines[1]), JSON.stringify({s: 'ok', p: 'ok', h: 'ok!'})) + t.end() + } + + collect('junk_rows.csv', {headers: ['s', 'p', 'h'], skipLines: 2}, verify) +})