Skip to content

Commit 65e283d

Browse files
committed
fix: properly skip comment lines
1 parent 10fae98 commit 65e283d

6 files changed

Lines changed: 16 additions & 49 deletions

File tree

index.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,14 @@ class CsvParser extends Transform {
133133
let isQuoted = false
134134
let offset = start
135135

136+
const { skipComments } = this
137+
if (skipComments) {
138+
const char = typeof skipComments === 'string' ? skipComments : '#'
139+
if (buf[start] === bufferFrom(char)[0]) {
140+
return
141+
}
142+
}
143+
136144
const mapValue = (value) => {
137145
if (this._first) {
138146
return value
@@ -144,14 +152,6 @@ class CsvParser extends Transform {
144152
return this.mapValues({ header, index, value })
145153
}
146154

147-
const { skipComments } = this
148-
if (skipComments) {
149-
const char = typeof skipComments === 'string' ? skipComments : '#'
150-
if (buf[0] === bufferFrom(char)) {
151-
return
152-
}
153-
}
154-
155155
for (let i = start; i < end; i++) {
156156
const isStartingQuote = !isQuoted && buf[i] === this.quote
157157
const isEndingQuote = isQuoted && buf[i] === this.quote && i + 1 <= end && buf[i + 1] === comma

test/skipComments.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ test.cb('comment', (t) => {
66
const verify = (err, lines) => {
77
t.false(err, 'no err')
88
t.snapshot(lines)
9-
t.is(lines.length, 2, '2 rowa')
9+
t.is(lines.length, 1, '1 row')
1010
t.end()
1111
}
1212

@@ -17,7 +17,7 @@ test.cb('custom comment', (t) => {
1717
const verify = (err, lines) => {
1818
t.false(err, 'no err')
1919
t.snapshot(lines)
20-
t.is(lines.length, 2, '2 rows')
20+
t.is(lines.length, 1, '1 row')
2121
t.end()
2222
}
2323

test/snapshots/comment.test.js.md

Lines changed: 0 additions & 31 deletions
This file was deleted.
-245 Bytes
Binary file not shown.

test/snapshots/skipComments.test.js.md

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,9 @@ Generated by [AVA](https://ava.li).
1010
1111
[
1212
Row {
13-
'# comment': 'a',
14-
},
15-
Row {
16-
'# comment': '1',
13+
a: '1',
14+
b: '2',
15+
c: '3',
1716
},
1817
]
1918

@@ -23,9 +22,8 @@ Generated by [AVA](https://ava.li).
2322
2423
[
2524
Row {
26-
'~ comment': 'a',
27-
},
28-
Row {
29-
'~ comment': '1',
25+
a: '1',
26+
b: '2',
27+
c: '3',
3028
},
3129
]
-24 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)