Skip to content

Commit 8e0f8b8

Browse files
committed
fix(csv-parse): comment infix when comment first field char (fix #415)
1 parent 6575fc6 commit 8e0f8b8

File tree

11 files changed

+65
-38
lines changed

11 files changed

+65
-38
lines changed

demo/issues-esm/lib/415.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ await pipeline(
1313
parse({
1414
comment: "#",
1515
delimiter: "\t",
16-
relax_column_count: true,
16+
comment_no_infix: true
1717
}),
1818
stringify({ delimiter: "|" }),
1919
process.stdout

packages/csv-parse/dist/cjs/index.cjs

+6-4
Original file line numberDiff line numberDiff line change
@@ -847,10 +847,12 @@ const transform = function(original_options = {}) {
847847
if(this.state.commenting){
848848
continue;
849849
}
850-
const commentCount = comment === null ? 0 : this.__compareBytes(comment, buf, pos, chr);
851-
if(commentCount !== 0 && (comment_no_infix === false || this.state.field.length === 0)){
852-
this.state.commenting = true;
853-
continue;
850+
if(comment !== null && (comment_no_infix === false || (this.state.record.length === 0 && this.state.field.length === 0))) {
851+
const commentCount = this.__compareBytes(comment, buf, pos, chr);
852+
if(commentCount !== 0){
853+
this.state.commenting = true;
854+
continue;
855+
}
854856
}
855857
const delimiterLength = this.__isDelimiter(buf, pos, chr);
856858
if(delimiterLength !== 0){

packages/csv-parse/dist/cjs/sync.cjs

+6-4
Original file line numberDiff line numberDiff line change
@@ -845,10 +845,12 @@ const transform = function(original_options = {}) {
845845
if(this.state.commenting){
846846
continue;
847847
}
848-
const commentCount = comment === null ? 0 : this.__compareBytes(comment, buf, pos, chr);
849-
if(commentCount !== 0 && (comment_no_infix === false || this.state.field.length === 0)){
850-
this.state.commenting = true;
851-
continue;
848+
if(comment !== null && (comment_no_infix === false || (this.state.record.length === 0 && this.state.field.length === 0))) {
849+
const commentCount = this.__compareBytes(comment, buf, pos, chr);
850+
if(commentCount !== 0){
851+
this.state.commenting = true;
852+
continue;
853+
}
852854
}
853855
const delimiterLength = this.__isDelimiter(buf, pos, chr);
854856
if(delimiterLength !== 0){

packages/csv-parse/dist/esm/index.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -5969,10 +5969,12 @@ const transform = function(original_options = {}) {
59695969
if(this.state.commenting){
59705970
continue;
59715971
}
5972-
const commentCount = comment === null ? 0 : this.__compareBytes(comment, buf, pos, chr);
5973-
if(commentCount !== 0 && (comment_no_infix === false || this.state.field.length === 0)){
5974-
this.state.commenting = true;
5975-
continue;
5972+
if(comment !== null && (comment_no_infix === false || (this.state.record.length === 0 && this.state.field.length === 0))) {
5973+
const commentCount = this.__compareBytes(comment, buf, pos, chr);
5974+
if(commentCount !== 0){
5975+
this.state.commenting = true;
5976+
continue;
5977+
}
59765978
}
59775979
const delimiterLength = this.__isDelimiter(buf, pos, chr);
59785980
if(delimiterLength !== 0){

packages/csv-parse/dist/esm/sync.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -2815,10 +2815,12 @@ const transform = function(original_options = {}) {
28152815
if(this.state.commenting){
28162816
continue;
28172817
}
2818-
const commentCount = comment === null ? 0 : this.__compareBytes(comment, buf, pos, chr);
2819-
if(commentCount !== 0 && (comment_no_infix === false || this.state.field.length === 0)){
2820-
this.state.commenting = true;
2821-
continue;
2818+
if(comment !== null && (comment_no_infix === false || (this.state.record.length === 0 && this.state.field.length === 0))) {
2819+
const commentCount = this.__compareBytes(comment, buf, pos, chr);
2820+
if(commentCount !== 0){
2821+
this.state.commenting = true;
2822+
continue;
2823+
}
28222824
}
28232825
const delimiterLength = this.__isDelimiter(buf, pos, chr);
28242826
if(delimiterLength !== 0){

packages/csv-parse/dist/iife/index.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -5972,10 +5972,12 @@ var csv_parse = (function (exports) {
59725972
if(this.state.commenting){
59735973
continue;
59745974
}
5975-
const commentCount = comment === null ? 0 : this.__compareBytes(comment, buf, pos, chr);
5976-
if(commentCount !== 0 && (comment_no_infix === false || this.state.field.length === 0)){
5977-
this.state.commenting = true;
5978-
continue;
5975+
if(comment !== null && (comment_no_infix === false || (this.state.record.length === 0 && this.state.field.length === 0))) {
5976+
const commentCount = this.__compareBytes(comment, buf, pos, chr);
5977+
if(commentCount !== 0){
5978+
this.state.commenting = true;
5979+
continue;
5980+
}
59795981
}
59805982
const delimiterLength = this.__isDelimiter(buf, pos, chr);
59815983
if(delimiterLength !== 0){

packages/csv-parse/dist/iife/sync.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -2818,10 +2818,12 @@ var csv_parse_sync = (function (exports) {
28182818
if(this.state.commenting){
28192819
continue;
28202820
}
2821-
const commentCount = comment === null ? 0 : this.__compareBytes(comment, buf, pos, chr);
2822-
if(commentCount !== 0 && (comment_no_infix === false || this.state.field.length === 0)){
2823-
this.state.commenting = true;
2824-
continue;
2821+
if(comment !== null && (comment_no_infix === false || (this.state.record.length === 0 && this.state.field.length === 0))) {
2822+
const commentCount = this.__compareBytes(comment, buf, pos, chr);
2823+
if(commentCount !== 0){
2824+
this.state.commenting = true;
2825+
continue;
2826+
}
28252827
}
28262828
const delimiterLength = this.__isDelimiter(buf, pos, chr);
28272829
if(delimiterLength !== 0){

packages/csv-parse/dist/umd/index.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -5975,10 +5975,12 @@
59755975
if(this.state.commenting){
59765976
continue;
59775977
}
5978-
const commentCount = comment === null ? 0 : this.__compareBytes(comment, buf, pos, chr);
5979-
if(commentCount !== 0 && (comment_no_infix === false || this.state.field.length === 0)){
5980-
this.state.commenting = true;
5981-
continue;
5978+
if(comment !== null && (comment_no_infix === false || (this.state.record.length === 0 && this.state.field.length === 0))) {
5979+
const commentCount = this.__compareBytes(comment, buf, pos, chr);
5980+
if(commentCount !== 0){
5981+
this.state.commenting = true;
5982+
continue;
5983+
}
59825984
}
59835985
const delimiterLength = this.__isDelimiter(buf, pos, chr);
59845986
if(delimiterLength !== 0){

packages/csv-parse/dist/umd/sync.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -2821,10 +2821,12 @@
28212821
if(this.state.commenting){
28222822
continue;
28232823
}
2824-
const commentCount = comment === null ? 0 : this.__compareBytes(comment, buf, pos, chr);
2825-
if(commentCount !== 0 && (comment_no_infix === false || this.state.field.length === 0)){
2826-
this.state.commenting = true;
2827-
continue;
2824+
if(comment !== null && (comment_no_infix === false || (this.state.record.length === 0 && this.state.field.length === 0))) {
2825+
const commentCount = this.__compareBytes(comment, buf, pos, chr);
2826+
if(commentCount !== 0){
2827+
this.state.commenting = true;
2828+
continue;
2829+
}
28282830
}
28292831
const delimiterLength = this.__isDelimiter(buf, pos, chr);
28302832
if(delimiterLength !== 0){

packages/csv-parse/lib/api/index.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -259,10 +259,12 @@ const transform = function(original_options = {}) {
259259
if(this.state.commenting){
260260
continue;
261261
}
262-
const commentCount = comment === null ? 0 : this.__compareBytes(comment, buf, pos, chr);
263-
if(commentCount !== 0 && (comment_no_infix === false || this.state.field.length === 0)){
264-
this.state.commenting = true;
265-
continue;
262+
if(comment !== null && (comment_no_infix === false || (this.state.record.length === 0 && this.state.field.length === 0))) {
263+
const commentCount = this.__compareBytes(comment, buf, pos, chr);
264+
if(commentCount !== 0){
265+
this.state.commenting = true;
266+
continue;
267+
}
266268
}
267269
const delimiterLength = this.__isDelimiter(buf, pos, chr);
268270
if(delimiterLength !== 0){

packages/csv-parse/test/option.comment_no_infix.coffee

+10-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,16 @@ describe 'Option `comment_no_infix`', ->
1919
message: 'Invalid option comment_no_infix: value must be a boolean, got 2'
2020
code: 'CSV_INVALID_OPTION_COMMENT'
2121

22-
it 'with `true`', (next) ->
22+
it 'with `true`, field starting with comment', (next) ->
23+
parse '''
24+
a,#,c
25+
''', comment: '#', comment_no_infix: true, (err, records) ->
26+
records.should.eql [
27+
['a', '#', 'c']
28+
] unless err
29+
next err
30+
31+
it 'with `true`, field not starting with comment', (next) ->
2332
parse '''
2433
a,b#,c
2534
''', comment: '#', comment_no_infix: true, (err, records) ->

0 commit comments

Comments
 (0)