From fa991cfe93e5043d548feb03229866cc69329ead Mon Sep 17 00:00:00 2001 From: Juanjo Diaz Date: Mon, 5 Mar 2018 23:45:23 +0200 Subject: [PATCH] fix: double quote escaping before new line (#268) --- lib/JSON2CSVBase.js | 2 +- test/JSON2CSVParser.js | 12 ++++++++++++ test/JSON2CSVTransform.js | 18 ++++++++++++++++++ test/fixtures/csv/backslashBeforeNewLine.csv | 6 ++++++ test/fixtures/json/backslashBeforeNewLine.json | 5 +++++ 5 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/csv/backslashBeforeNewLine.csv create mode 100644 test/fixtures/json/backslashBeforeNewLine.json diff --git a/lib/JSON2CSVBase.js b/lib/JSON2CSVBase.js index f055f531..33121bf6 100644 --- a/lib/JSON2CSVBase.js +++ b/lib/JSON2CSVBase.js @@ -181,7 +181,7 @@ class JSON2CSVBase { //a backslash, and it's not at the end of the stringifiedValue. stringifiedValue = stringifiedValue .replace(/^"(.*)"$/, this.opts.quote + '$1' + this.opts.quote) - .replace(/(\\")(?=.)/g, this.opts.doubleQuote) + .replace(/(\\")(?!$)/g, this.opts.doubleQuote) .replace(/\\\\/g, '\\'); if (this.opts.excelStrings && typeof value === 'string') { diff --git a/test/JSON2CSVParser.js b/test/JSON2CSVParser.js index 178bb885..8cd7c87c 100644 --- a/test/JSON2CSVParser.js +++ b/test/JSON2CSVParser.js @@ -425,6 +425,18 @@ module.exports = (testRunner, jsonFixtures, csvFixtures) => { t.end(); }); + testRunner.add('should escape quotes before new line with value in \'doubleQuote\'', (t) => { + const opts = { + fields: ['a string'] + }; + + const parser = new Json2csvParser(opts); + const csv = parser.parse(jsonFixtures.backslashBeforeNewLine); + + t.equal(csv, csvFixtures.backslashBeforeNewLine); + t.end(); + }); + // Delimiter testRunner.add('should use a custom delimiter when \'delimiter\' property is defined', (t) => { diff --git a/test/JSON2CSVTransform.js b/test/JSON2CSVTransform.js index f3a3d4d9..d8822e42 100644 --- a/test/JSON2CSVTransform.js +++ b/test/JSON2CSVTransform.js @@ -612,6 +612,24 @@ module.exports = (testRunner, jsonFixtures, csvFixtures) => { .on('error', err => t.notOk(true, err.message)); }); + testRunner.add('should escape quotes before new line with value in \'doubleQuote\'', (t) => { + const opts = { + fields: ['a string'] + }; + + const transform = new Json2csvTransform(opts); + const processor = jsonFixtures.backslashBeforeNewLine().pipe(transform); + + let csv = ''; + processor + .on('data', chunk => (csv += chunk.toString())) + .on('end', () => { + t.equal(csv, csvFixtures.backslashBeforeNewLine); + t.end(); + }) + .on('error', err => t.notOk(true, err.message)); + }); + // Delimiter testRunner.add('should use a custom delimiter when \'delimiter\' property is defined', (t) => { diff --git a/test/fixtures/csv/backslashBeforeNewLine.csv b/test/fixtures/csv/backslashBeforeNewLine.csv new file mode 100644 index 00000000..c8045eb5 --- /dev/null +++ b/test/fixtures/csv/backslashBeforeNewLine.csv @@ -0,0 +1,6 @@ +"a string" +"with a description" +"with a description and "" +quotes and backslash\" +"with a description and "" +quotes and backslash\\" \ No newline at end of file diff --git a/test/fixtures/json/backslashBeforeNewLine.json b/test/fixtures/json/backslashBeforeNewLine.json new file mode 100644 index 00000000..65a3fbe2 --- /dev/null +++ b/test/fixtures/json/backslashBeforeNewLine.json @@ -0,0 +1,5 @@ +[ + {"a string": "with a description"}, + {"a string": "with a description and \"\nquotes and backslash\\"}, + {"a string": "with a description and \"\nquotes and backslash\\\\"} +] \ No newline at end of file