From 8e0f8b8e11736f1223b0bda4dd2a3b37506dd531 Mon Sep 17 00:00:00 2001 From: David Worms Date: Wed, 28 Feb 2024 13:16:38 +0100 Subject: [PATCH] fix(csv-parse): comment infix when comment first field char (fix #415) --- demo/issues-esm/lib/415.js | 2 +- packages/csv-parse/dist/cjs/index.cjs | 10 ++++++---- packages/csv-parse/dist/cjs/sync.cjs | 10 ++++++---- packages/csv-parse/dist/esm/index.js | 10 ++++++---- packages/csv-parse/dist/esm/sync.js | 10 ++++++---- packages/csv-parse/dist/iife/index.js | 10 ++++++---- packages/csv-parse/dist/iife/sync.js | 10 ++++++---- packages/csv-parse/dist/umd/index.js | 10 ++++++---- packages/csv-parse/dist/umd/sync.js | 10 ++++++---- packages/csv-parse/lib/api/index.js | 10 ++++++---- .../csv-parse/test/option.comment_no_infix.coffee | 11 ++++++++++- 11 files changed, 65 insertions(+), 38 deletions(-) diff --git a/demo/issues-esm/lib/415.js b/demo/issues-esm/lib/415.js index 825aee01..daf1d45c 100644 --- a/demo/issues-esm/lib/415.js +++ b/demo/issues-esm/lib/415.js @@ -13,7 +13,7 @@ await pipeline( parse({ comment: "#", delimiter: "\t", - relax_column_count: true, + comment_no_infix: true }), stringify({ delimiter: "|" }), process.stdout diff --git a/packages/csv-parse/dist/cjs/index.cjs b/packages/csv-parse/dist/cjs/index.cjs index 7e6e2a00..b3313f5c 100644 --- a/packages/csv-parse/dist/cjs/index.cjs +++ b/packages/csv-parse/dist/cjs/index.cjs @@ -847,10 +847,12 @@ const transform = function(original_options = {}) { if(this.state.commenting){ continue; } - const commentCount = comment === null ? 0 : this.__compareBytes(comment, buf, pos, chr); - if(commentCount !== 0 && (comment_no_infix === false || this.state.field.length === 0)){ - this.state.commenting = true; - continue; + if(comment !== null && (comment_no_infix === false || (this.state.record.length === 0 && this.state.field.length === 0))) { + const commentCount = this.__compareBytes(comment, buf, pos, chr); + if(commentCount !== 0){ + this.state.commenting = true; + continue; + } } const delimiterLength = this.__isDelimiter(buf, pos, chr); if(delimiterLength !== 0){ diff --git a/packages/csv-parse/dist/cjs/sync.cjs b/packages/csv-parse/dist/cjs/sync.cjs index 4d0058d3..f05e6aac 100644 --- a/packages/csv-parse/dist/cjs/sync.cjs +++ b/packages/csv-parse/dist/cjs/sync.cjs @@ -845,10 +845,12 @@ const transform = function(original_options = {}) { if(this.state.commenting){ continue; } - const commentCount = comment === null ? 0 : this.__compareBytes(comment, buf, pos, chr); - if(commentCount !== 0 && (comment_no_infix === false || this.state.field.length === 0)){ - this.state.commenting = true; - continue; + if(comment !== null && (comment_no_infix === false || (this.state.record.length === 0 && this.state.field.length === 0))) { + const commentCount = this.__compareBytes(comment, buf, pos, chr); + if(commentCount !== 0){ + this.state.commenting = true; + continue; + } } const delimiterLength = this.__isDelimiter(buf, pos, chr); if(delimiterLength !== 0){ diff --git a/packages/csv-parse/dist/esm/index.js b/packages/csv-parse/dist/esm/index.js index 2beed0f6..bfffcfdd 100644 --- a/packages/csv-parse/dist/esm/index.js +++ b/packages/csv-parse/dist/esm/index.js @@ -5969,10 +5969,12 @@ const transform = function(original_options = {}) { if(this.state.commenting){ continue; } - const commentCount = comment === null ? 0 : this.__compareBytes(comment, buf, pos, chr); - if(commentCount !== 0 && (comment_no_infix === false || this.state.field.length === 0)){ - this.state.commenting = true; - continue; + if(comment !== null && (comment_no_infix === false || (this.state.record.length === 0 && this.state.field.length === 0))) { + const commentCount = this.__compareBytes(comment, buf, pos, chr); + if(commentCount !== 0){ + this.state.commenting = true; + continue; + } } const delimiterLength = this.__isDelimiter(buf, pos, chr); if(delimiterLength !== 0){ diff --git a/packages/csv-parse/dist/esm/sync.js b/packages/csv-parse/dist/esm/sync.js index 40566284..09a250d3 100644 --- a/packages/csv-parse/dist/esm/sync.js +++ b/packages/csv-parse/dist/esm/sync.js @@ -2815,10 +2815,12 @@ const transform = function(original_options = {}) { if(this.state.commenting){ continue; } - const commentCount = comment === null ? 0 : this.__compareBytes(comment, buf, pos, chr); - if(commentCount !== 0 && (comment_no_infix === false || this.state.field.length === 0)){ - this.state.commenting = true; - continue; + if(comment !== null && (comment_no_infix === false || (this.state.record.length === 0 && this.state.field.length === 0))) { + const commentCount = this.__compareBytes(comment, buf, pos, chr); + if(commentCount !== 0){ + this.state.commenting = true; + continue; + } } const delimiterLength = this.__isDelimiter(buf, pos, chr); if(delimiterLength !== 0){ diff --git a/packages/csv-parse/dist/iife/index.js b/packages/csv-parse/dist/iife/index.js index c86d31cd..43810dc5 100644 --- a/packages/csv-parse/dist/iife/index.js +++ b/packages/csv-parse/dist/iife/index.js @@ -5972,10 +5972,12 @@ var csv_parse = (function (exports) { if(this.state.commenting){ continue; } - const commentCount = comment === null ? 0 : this.__compareBytes(comment, buf, pos, chr); - if(commentCount !== 0 && (comment_no_infix === false || this.state.field.length === 0)){ - this.state.commenting = true; - continue; + if(comment !== null && (comment_no_infix === false || (this.state.record.length === 0 && this.state.field.length === 0))) { + const commentCount = this.__compareBytes(comment, buf, pos, chr); + if(commentCount !== 0){ + this.state.commenting = true; + continue; + } } const delimiterLength = this.__isDelimiter(buf, pos, chr); if(delimiterLength !== 0){ diff --git a/packages/csv-parse/dist/iife/sync.js b/packages/csv-parse/dist/iife/sync.js index b4e3de6c..1b5ce0d8 100644 --- a/packages/csv-parse/dist/iife/sync.js +++ b/packages/csv-parse/dist/iife/sync.js @@ -2818,10 +2818,12 @@ var csv_parse_sync = (function (exports) { if(this.state.commenting){ continue; } - const commentCount = comment === null ? 0 : this.__compareBytes(comment, buf, pos, chr); - if(commentCount !== 0 && (comment_no_infix === false || this.state.field.length === 0)){ - this.state.commenting = true; - continue; + if(comment !== null && (comment_no_infix === false || (this.state.record.length === 0 && this.state.field.length === 0))) { + const commentCount = this.__compareBytes(comment, buf, pos, chr); + if(commentCount !== 0){ + this.state.commenting = true; + continue; + } } const delimiterLength = this.__isDelimiter(buf, pos, chr); if(delimiterLength !== 0){ diff --git a/packages/csv-parse/dist/umd/index.js b/packages/csv-parse/dist/umd/index.js index 38072204..8ecfc7cb 100644 --- a/packages/csv-parse/dist/umd/index.js +++ b/packages/csv-parse/dist/umd/index.js @@ -5975,10 +5975,12 @@ if(this.state.commenting){ continue; } - const commentCount = comment === null ? 0 : this.__compareBytes(comment, buf, pos, chr); - if(commentCount !== 0 && (comment_no_infix === false || this.state.field.length === 0)){ - this.state.commenting = true; - continue; + if(comment !== null && (comment_no_infix === false || (this.state.record.length === 0 && this.state.field.length === 0))) { + const commentCount = this.__compareBytes(comment, buf, pos, chr); + if(commentCount !== 0){ + this.state.commenting = true; + continue; + } } const delimiterLength = this.__isDelimiter(buf, pos, chr); if(delimiterLength !== 0){ diff --git a/packages/csv-parse/dist/umd/sync.js b/packages/csv-parse/dist/umd/sync.js index 4b3c2b72..ebe39aa8 100644 --- a/packages/csv-parse/dist/umd/sync.js +++ b/packages/csv-parse/dist/umd/sync.js @@ -2821,10 +2821,12 @@ if(this.state.commenting){ continue; } - const commentCount = comment === null ? 0 : this.__compareBytes(comment, buf, pos, chr); - if(commentCount !== 0 && (comment_no_infix === false || this.state.field.length === 0)){ - this.state.commenting = true; - continue; + if(comment !== null && (comment_no_infix === false || (this.state.record.length === 0 && this.state.field.length === 0))) { + const commentCount = this.__compareBytes(comment, buf, pos, chr); + if(commentCount !== 0){ + this.state.commenting = true; + continue; + } } const delimiterLength = this.__isDelimiter(buf, pos, chr); if(delimiterLength !== 0){ diff --git a/packages/csv-parse/lib/api/index.js b/packages/csv-parse/lib/api/index.js index 9833a68f..0fc5e3f8 100644 --- a/packages/csv-parse/lib/api/index.js +++ b/packages/csv-parse/lib/api/index.js @@ -259,10 +259,12 @@ const transform = function(original_options = {}) { if(this.state.commenting){ continue; } - const commentCount = comment === null ? 0 : this.__compareBytes(comment, buf, pos, chr); - if(commentCount !== 0 && (comment_no_infix === false || this.state.field.length === 0)){ - this.state.commenting = true; - continue; + if(comment !== null && (comment_no_infix === false || (this.state.record.length === 0 && this.state.field.length === 0))) { + const commentCount = this.__compareBytes(comment, buf, pos, chr); + if(commentCount !== 0){ + this.state.commenting = true; + continue; + } } const delimiterLength = this.__isDelimiter(buf, pos, chr); if(delimiterLength !== 0){ diff --git a/packages/csv-parse/test/option.comment_no_infix.coffee b/packages/csv-parse/test/option.comment_no_infix.coffee index e3ccf76d..fbdcd03a 100644 --- a/packages/csv-parse/test/option.comment_no_infix.coffee +++ b/packages/csv-parse/test/option.comment_no_infix.coffee @@ -19,7 +19,16 @@ describe 'Option `comment_no_infix`', -> message: 'Invalid option comment_no_infix: value must be a boolean, got 2' code: 'CSV_INVALID_OPTION_COMMENT' - it 'with `true`', (next) -> + it 'with `true`, field starting with comment', (next) -> + parse ''' + a,#,c + ''', comment: '#', comment_no_infix: true, (err, records) -> + records.should.eql [ + ['a', '#', 'c'] + ] unless err + next err + + it 'with `true`, field not starting with comment', (next) -> parse ''' a,b#,c ''', comment: '#', comment_no_infix: true, (err, records) ->