Skip to content

Commit caca5c3

Browse files
committed
feat(csv-parse): new comment_no_infix option (fix #325)
1 parent 87fe919 commit caca5c3

15 files changed

Lines changed: 166 additions & 19 deletions

File tree

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,16 @@ const normalize_options = function(opts){
278278
], options);
279279
}
280280
}
281+
// Normalize option `comment_no_infix`
282+
if(options.comment_no_infix === undefined || options.comment_no_infix === null || options.comment_no_infix === false){
283+
options.comment_no_infix = false;
284+
}else if(options.comment_no_infix !== true){
285+
throw new CsvError('CSV_INVALID_OPTION_COMMENT', [
286+
'Invalid option comment_no_infix:',
287+
'value must be a boolean,',
288+
`got ${JSON.stringify(options.comment_no_infix)}`
289+
], options);
290+
}
281291
// Normalize option `delimiter`
282292
const delimiter_json = JSON.stringify(options.delimiter);
283293
if(!Array.isArray(options.delimiter)) options.delimiter = [options.delimiter];
@@ -639,7 +649,7 @@ const transform = function(original_options = {}) {
639649
},
640650
// Central parser implementation
641651
parse: function(nextBuf, end, push, close){
642-
const {bom, encoding, from_line, ltrim, max_record_size,raw, relax_quotes, rtrim, skip_empty_lines, to, to_line} = this.options;
652+
const {bom, comment_no_infix, encoding, from_line, ltrim, max_record_size,raw, relax_quotes, rtrim, skip_empty_lines, to, to_line} = this.options;
643653
let {comment, escape, quote, record_delimiter} = this.options;
644654
const {bomSkipped, previousBuf, rawBuffer, escapeIsQuote} = this.state;
645655
let buf;
@@ -838,7 +848,7 @@ const transform = function(original_options = {}) {
838848
continue;
839849
}
840850
const commentCount = comment === null ? 0 : this.__compareBytes(comment, buf, pos, chr);
841-
if(commentCount !== 0){
851+
if(commentCount !== 0 && (comment_no_infix === false || this.state.field.length === 0)){
842852
this.state.commenting = true;
843853
continue;
844854
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,12 @@ export interface Options {
8888
* Treat all the characters after this one as a comment, default to '' (disabled).
8989
*/
9090
comment?: string;
91+
/**
92+
* Restrict the definition of comments to a full line. Comment characters
93+
* defined in the middle of the line are not interpreted as such. The
94+
* option require the activation of comments.
95+
*/
96+
comment_no_infix?: boolean;
9197
/**
9298
* Set the field delimiter. One character only, defaults to comma.
9399
*/

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,16 @@ const normalize_options = function(opts){
276276
], options);
277277
}
278278
}
279+
// Normalize option `comment_no_infix`
280+
if(options.comment_no_infix === undefined || options.comment_no_infix === null || options.comment_no_infix === false){
281+
options.comment_no_infix = false;
282+
}else if(options.comment_no_infix !== true){
283+
throw new CsvError('CSV_INVALID_OPTION_COMMENT', [
284+
'Invalid option comment_no_infix:',
285+
'value must be a boolean,',
286+
`got ${JSON.stringify(options.comment_no_infix)}`
287+
], options);
288+
}
279289
// Normalize option `delimiter`
280290
const delimiter_json = JSON.stringify(options.delimiter);
281291
if(!Array.isArray(options.delimiter)) options.delimiter = [options.delimiter];
@@ -637,7 +647,7 @@ const transform = function(original_options = {}) {
637647
},
638648
// Central parser implementation
639649
parse: function(nextBuf, end, push, close){
640-
const {bom, encoding, from_line, ltrim, max_record_size,raw, relax_quotes, rtrim, skip_empty_lines, to, to_line} = this.options;
650+
const {bom, comment_no_infix, encoding, from_line, ltrim, max_record_size,raw, relax_quotes, rtrim, skip_empty_lines, to, to_line} = this.options;
641651
let {comment, escape, quote, record_delimiter} = this.options;
642652
const {bomSkipped, previousBuf, rawBuffer, escapeIsQuote} = this.state;
643653
let buf;
@@ -836,7 +846,7 @@ const transform = function(original_options = {}) {
836846
continue;
837847
}
838848
const commentCount = comment === null ? 0 : this.__compareBytes(comment, buf, pos, chr);
839-
if(commentCount !== 0){
849+
if(commentCount !== 0 && (comment_no_infix === false || this.state.field.length === 0)){
840850
this.state.commenting = true;
841851
continue;
842852
}

packages/csv-parse/dist/esm/index.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,12 @@ export interface Options {
8888
* Treat all the characters after this one as a comment, default to '' (disabled).
8989
*/
9090
comment?: string;
91+
/**
92+
* Restrict the definition of comments to a full line. Comment characters
93+
* defined in the middle of the line are not interpreted as such. The
94+
* option require the activation of comments.
95+
*/
96+
comment_no_infix?: boolean;
9197
/**
9298
* Set the field delimiter. One character only, defaults to comma.
9399
*/

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5400,6 +5400,16 @@ const normalize_options = function(opts){
54005400
], options);
54015401
}
54025402
}
5403+
// Normalize option `comment_no_infix`
5404+
if(options.comment_no_infix === undefined || options.comment_no_infix === null || options.comment_no_infix === false){
5405+
options.comment_no_infix = false;
5406+
}else if(options.comment_no_infix !== true){
5407+
throw new CsvError('CSV_INVALID_OPTION_COMMENT', [
5408+
'Invalid option comment_no_infix:',
5409+
'value must be a boolean,',
5410+
`got ${JSON.stringify(options.comment_no_infix)}`
5411+
], options);
5412+
}
54035413
// Normalize option `delimiter`
54045414
const delimiter_json = JSON.stringify(options.delimiter);
54055415
if(!Array.isArray(options.delimiter)) options.delimiter = [options.delimiter];
@@ -5761,7 +5771,7 @@ const transform = function(original_options = {}) {
57615771
},
57625772
// Central parser implementation
57635773
parse: function(nextBuf, end, push, close){
5764-
const {bom, encoding, from_line, ltrim, max_record_size,raw, relax_quotes, rtrim, skip_empty_lines, to, to_line} = this.options;
5774+
const {bom, comment_no_infix, encoding, from_line, ltrim, max_record_size,raw, relax_quotes, rtrim, skip_empty_lines, to, to_line} = this.options;
57655775
let {comment, escape, quote, record_delimiter} = this.options;
57665776
const {bomSkipped, previousBuf, rawBuffer, escapeIsQuote} = this.state;
57675777
let buf;
@@ -5960,7 +5970,7 @@ const transform = function(original_options = {}) {
59605970
continue;
59615971
}
59625972
const commentCount = comment === null ? 0 : this.__compareBytes(comment, buf, pos, chr);
5963-
if(commentCount !== 0){
5973+
if(commentCount !== 0 && (comment_no_infix === false || this.state.field.length === 0)){
59645974
this.state.commenting = true;
59655975
continue;
59665976
}

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2246,6 +2246,16 @@ const normalize_options = function(opts){
22462246
], options);
22472247
}
22482248
}
2249+
// Normalize option `comment_no_infix`
2250+
if(options.comment_no_infix === undefined || options.comment_no_infix === null || options.comment_no_infix === false){
2251+
options.comment_no_infix = false;
2252+
}else if(options.comment_no_infix !== true){
2253+
throw new CsvError('CSV_INVALID_OPTION_COMMENT', [
2254+
'Invalid option comment_no_infix:',
2255+
'value must be a boolean,',
2256+
`got ${JSON.stringify(options.comment_no_infix)}`
2257+
], options);
2258+
}
22492259
// Normalize option `delimiter`
22502260
const delimiter_json = JSON.stringify(options.delimiter);
22512261
if(!Array.isArray(options.delimiter)) options.delimiter = [options.delimiter];
@@ -2607,7 +2617,7 @@ const transform = function(original_options = {}) {
26072617
},
26082618
// Central parser implementation
26092619
parse: function(nextBuf, end, push, close){
2610-
const {bom, encoding, from_line, ltrim, max_record_size,raw, relax_quotes, rtrim, skip_empty_lines, to, to_line} = this.options;
2620+
const {bom, comment_no_infix, encoding, from_line, ltrim, max_record_size,raw, relax_quotes, rtrim, skip_empty_lines, to, to_line} = this.options;
26112621
let {comment, escape, quote, record_delimiter} = this.options;
26122622
const {bomSkipped, previousBuf, rawBuffer, escapeIsQuote} = this.state;
26132623
let buf;
@@ -2806,7 +2816,7 @@ const transform = function(original_options = {}) {
28062816
continue;
28072817
}
28082818
const commentCount = comment === null ? 0 : this.__compareBytes(comment, buf, pos, chr);
2809-
if(commentCount !== 0){
2819+
if(commentCount !== 0 && (comment_no_infix === false || this.state.field.length === 0)){
28102820
this.state.commenting = true;
28112821
continue;
28122822
}

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5403,6 +5403,16 @@ var csv_parse = (function (exports) {
54035403
], options);
54045404
}
54055405
}
5406+
// Normalize option `comment_no_infix`
5407+
if(options.comment_no_infix === undefined || options.comment_no_infix === null || options.comment_no_infix === false){
5408+
options.comment_no_infix = false;
5409+
}else if(options.comment_no_infix !== true){
5410+
throw new CsvError('CSV_INVALID_OPTION_COMMENT', [
5411+
'Invalid option comment_no_infix:',
5412+
'value must be a boolean,',
5413+
`got ${JSON.stringify(options.comment_no_infix)}`
5414+
], options);
5415+
}
54065416
// Normalize option `delimiter`
54075417
const delimiter_json = JSON.stringify(options.delimiter);
54085418
if(!Array.isArray(options.delimiter)) options.delimiter = [options.delimiter];
@@ -5764,7 +5774,7 @@ var csv_parse = (function (exports) {
57645774
},
57655775
// Central parser implementation
57665776
parse: function(nextBuf, end, push, close){
5767-
const {bom, encoding, from_line, ltrim, max_record_size,raw, relax_quotes, rtrim, skip_empty_lines, to, to_line} = this.options;
5777+
const {bom, comment_no_infix, encoding, from_line, ltrim, max_record_size,raw, relax_quotes, rtrim, skip_empty_lines, to, to_line} = this.options;
57685778
let {comment, escape, quote, record_delimiter} = this.options;
57695779
const {bomSkipped, previousBuf, rawBuffer, escapeIsQuote} = this.state;
57705780
let buf;
@@ -5963,7 +5973,7 @@ var csv_parse = (function (exports) {
59635973
continue;
59645974
}
59655975
const commentCount = comment === null ? 0 : this.__compareBytes(comment, buf, pos, chr);
5966-
if(commentCount !== 0){
5976+
if(commentCount !== 0 && (comment_no_infix === false || this.state.field.length === 0)){
59675977
this.state.commenting = true;
59685978
continue;
59695979
}

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2249,6 +2249,16 @@ var csv_parse_sync = (function (exports) {
22492249
], options);
22502250
}
22512251
}
2252+
// Normalize option `comment_no_infix`
2253+
if(options.comment_no_infix === undefined || options.comment_no_infix === null || options.comment_no_infix === false){
2254+
options.comment_no_infix = false;
2255+
}else if(options.comment_no_infix !== true){
2256+
throw new CsvError('CSV_INVALID_OPTION_COMMENT', [
2257+
'Invalid option comment_no_infix:',
2258+
'value must be a boolean,',
2259+
`got ${JSON.stringify(options.comment_no_infix)}`
2260+
], options);
2261+
}
22522262
// Normalize option `delimiter`
22532263
const delimiter_json = JSON.stringify(options.delimiter);
22542264
if(!Array.isArray(options.delimiter)) options.delimiter = [options.delimiter];
@@ -2610,7 +2620,7 @@ var csv_parse_sync = (function (exports) {
26102620
},
26112621
// Central parser implementation
26122622
parse: function(nextBuf, end, push, close){
2613-
const {bom, encoding, from_line, ltrim, max_record_size,raw, relax_quotes, rtrim, skip_empty_lines, to, to_line} = this.options;
2623+
const {bom, comment_no_infix, encoding, from_line, ltrim, max_record_size,raw, relax_quotes, rtrim, skip_empty_lines, to, to_line} = this.options;
26142624
let {comment, escape, quote, record_delimiter} = this.options;
26152625
const {bomSkipped, previousBuf, rawBuffer, escapeIsQuote} = this.state;
26162626
let buf;
@@ -2809,7 +2819,7 @@ var csv_parse_sync = (function (exports) {
28092819
continue;
28102820
}
28112821
const commentCount = comment === null ? 0 : this.__compareBytes(comment, buf, pos, chr);
2812-
if(commentCount !== 0){
2822+
if(commentCount !== 0 && (comment_no_infix === false || this.state.field.length === 0)){
28132823
this.state.commenting = true;
28142824
continue;
28152825
}

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5406,6 +5406,16 @@
54065406
], options);
54075407
}
54085408
}
5409+
// Normalize option `comment_no_infix`
5410+
if(options.comment_no_infix === undefined || options.comment_no_infix === null || options.comment_no_infix === false){
5411+
options.comment_no_infix = false;
5412+
}else if(options.comment_no_infix !== true){
5413+
throw new CsvError('CSV_INVALID_OPTION_COMMENT', [
5414+
'Invalid option comment_no_infix:',
5415+
'value must be a boolean,',
5416+
`got ${JSON.stringify(options.comment_no_infix)}`
5417+
], options);
5418+
}
54095419
// Normalize option `delimiter`
54105420
const delimiter_json = JSON.stringify(options.delimiter);
54115421
if(!Array.isArray(options.delimiter)) options.delimiter = [options.delimiter];
@@ -5767,7 +5777,7 @@
57675777
},
57685778
// Central parser implementation
57695779
parse: function(nextBuf, end, push, close){
5770-
const {bom, encoding, from_line, ltrim, max_record_size,raw, relax_quotes, rtrim, skip_empty_lines, to, to_line} = this.options;
5780+
const {bom, comment_no_infix, encoding, from_line, ltrim, max_record_size,raw, relax_quotes, rtrim, skip_empty_lines, to, to_line} = this.options;
57715781
let {comment, escape, quote, record_delimiter} = this.options;
57725782
const {bomSkipped, previousBuf, rawBuffer, escapeIsQuote} = this.state;
57735783
let buf;
@@ -5966,7 +5976,7 @@
59665976
continue;
59675977
}
59685978
const commentCount = comment === null ? 0 : this.__compareBytes(comment, buf, pos, chr);
5969-
if(commentCount !== 0){
5979+
if(commentCount !== 0 && (comment_no_infix === false || this.state.field.length === 0)){
59705980
this.state.commenting = true;
59715981
continue;
59725982
}

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2252,6 +2252,16 @@
22522252
], options);
22532253
}
22542254
}
2255+
// Normalize option `comment_no_infix`
2256+
if(options.comment_no_infix === undefined || options.comment_no_infix === null || options.comment_no_infix === false){
2257+
options.comment_no_infix = false;
2258+
}else if(options.comment_no_infix !== true){
2259+
throw new CsvError('CSV_INVALID_OPTION_COMMENT', [
2260+
'Invalid option comment_no_infix:',
2261+
'value must be a boolean,',
2262+
`got ${JSON.stringify(options.comment_no_infix)}`
2263+
], options);
2264+
}
22552265
// Normalize option `delimiter`
22562266
const delimiter_json = JSON.stringify(options.delimiter);
22572267
if(!Array.isArray(options.delimiter)) options.delimiter = [options.delimiter];
@@ -2613,7 +2623,7 @@
26132623
},
26142624
// Central parser implementation
26152625
parse: function(nextBuf, end, push, close){
2616-
const {bom, encoding, from_line, ltrim, max_record_size,raw, relax_quotes, rtrim, skip_empty_lines, to, to_line} = this.options;
2626+
const {bom, comment_no_infix, encoding, from_line, ltrim, max_record_size,raw, relax_quotes, rtrim, skip_empty_lines, to, to_line} = this.options;
26172627
let {comment, escape, quote, record_delimiter} = this.options;
26182628
const {bomSkipped, previousBuf, rawBuffer, escapeIsQuote} = this.state;
26192629
let buf;
@@ -2812,7 +2822,7 @@
28122822
continue;
28132823
}
28142824
const commentCount = comment === null ? 0 : this.__compareBytes(comment, buf, pos, chr);
2815-
if(commentCount !== 0){
2825+
if(commentCount !== 0 && (comment_no_infix === false || this.state.field.length === 0)){
28162826
this.state.commenting = true;
28172827
continue;
28182828
}

0 commit comments

Comments
 (0)