Skip to content

Commit

Permalink
fix: properly skip comment lines
Browse files Browse the repository at this point in the history
  • Loading branch information
shellscape committed Feb 16, 2019
1 parent 10fae98 commit 65e283d
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 49 deletions.
16 changes: 8 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,14 @@ class CsvParser extends Transform {
let isQuoted = false
let offset = start

const { skipComments } = this
if (skipComments) {
const char = typeof skipComments === 'string' ? skipComments : '#'
if (buf[start] === bufferFrom(char)[0]) {
return
}
}

const mapValue = (value) => {
if (this._first) {
return value
Expand All @@ -144,14 +152,6 @@ class CsvParser extends Transform {
return this.mapValues({ header, index, value })
}

const { skipComments } = this
if (skipComments) {
const char = typeof skipComments === 'string' ? skipComments : '#'
if (buf[0] === bufferFrom(char)) {
return
}
}

for (let i = start; i < end; i++) {
const isStartingQuote = !isQuoted && buf[i] === this.quote
const isEndingQuote = isQuoted && buf[i] === this.quote && i + 1 <= end && buf[i + 1] === comma
Expand Down
4 changes: 2 additions & 2 deletions test/skipComments.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ test.cb('comment', (t) => {
const verify = (err, lines) => {
t.false(err, 'no err')
t.snapshot(lines)
t.is(lines.length, 2, '2 rowa')
t.is(lines.length, 1, '1 row')
t.end()
}

Expand All @@ -17,7 +17,7 @@ test.cb('custom comment', (t) => {
const verify = (err, lines) => {
t.false(err, 'no err')
t.snapshot(lines)
t.is(lines.length, 2, '2 rows')
t.is(lines.length, 1, '1 row')
t.end()
}

Expand Down
31 changes: 0 additions & 31 deletions test/snapshots/comment.test.js.md

This file was deleted.

Binary file removed test/snapshots/comment.test.js.snap
Binary file not shown.
14 changes: 6 additions & 8 deletions test/snapshots/skipComments.test.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ Generated by [AVA](https://ava.li).
[
Row {
'# comment': 'a',
},
Row {
'# comment': '1',
a: '1',
b: '2',
c: '3',
},
]

Expand All @@ -23,9 +22,8 @@ Generated by [AVA](https://ava.li).
[
Row {
'~ comment': 'a',
},
Row {
'~ comment': '1',
a: '1',
b: '2',
c: '3',
},
]
Binary file modified test/snapshots/skipComments.test.js.snap
Binary file not shown.

0 comments on commit 65e283d

Please sign in to comment.