Skip to content

Commit

Permalink
fix: skipLines with header option. fixes #110, #84
Browse files Browse the repository at this point in the history
this change integrates the proposed changes by @combizs in #111.
  • Loading branch information
shellscape committed Feb 16, 2019
1 parent 65e283d commit ead4c1f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
}
}

Expand Down
15 changes: 14 additions & 1 deletion test/skipLines.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})

0 comments on commit ead4c1f

Please sign in to comment.