diff --git a/lib/JSON2CSVBase.js b/lib/JSON2CSVBase.js index 6e020397..167a509a 100644 --- a/lib/JSON2CSVBase.js +++ b/lib/JSON2CSVBase.js @@ -204,14 +204,16 @@ class JSON2CSVBase { .replace(/\u21E5/g, '\t'); } - // Replace automatically scaped single quotes by doubleQuotes - stringifiedValue = stringifiedValue - .replace(/\\"(?!$)/g, this.opts.doubleQuote); - - if (this.opts.quote !== '"') { + if (this.opts.quote === '"') { + // Replace automatically escaped single quotes by doubleQuotes + stringifiedValue = stringifiedValue + .replace(/\\"(?!$)/g, this.opts.doubleQuote); + } else { + // Unescape double quotes ('"') // Replace single quote with double quote // Replace wrapping quotes stringifiedValue = stringifiedValue + .replace(/\\"(?!$)/g, '"') .replace(new RegExp(this.opts.quote, 'g'), this.opts.doubleQuote) .replace(/^"/, this.opts.quote) .replace(/"$/, this.opts.quote); diff --git a/test/CLI.js b/test/CLI.js index 8147026c..2b3604c1 100644 --- a/test/CLI.js +++ b/test/CLI.js @@ -429,6 +429,17 @@ module.exports = (testRunner, jsonFixtures, csvFixtures) => { }); }); + testRunner.add('should not escape \'"\' when setting \'quote\' set to something else', (t) => { + const opts = ' --quote "\'"'; + + child_process.exec(cli + '-i ' + getFixturePath('/json/doubleQuotes.json') + opts, (err, stdout, stderr) => { + t.notOk(stderr); + const csv = stdout; + t.equal(csv, csvFixtures.doubleQuotesUnescaped); + t.end(); + }); + }); + // Double Quote testRunner.add('should escape quotes with double quotes', (t) => { diff --git a/test/JSON2CSVAsyncParser.js b/test/JSON2CSVAsyncParser.js index 5d4b2f90..e660c473 100644 --- a/test/JSON2CSVAsyncParser.js +++ b/test/JSON2CSVAsyncParser.js @@ -567,6 +567,18 @@ module.exports = (testRunner, jsonFixtures, csvFixtures, inMemoryJsonFixtures) = .then(() => t.end()); }); + testRunner.add('should not escape \'"\' when setting \'quote\' set to something else', (t) => { + const opts = { + quote: '\'' + }; + + const parser = new AsyncParser(opts); + parser.fromInput(jsonFixtures.doubleQuotes()).promise() + .then(csv => t.equal(csv, csvFixtures.doubleQuotesUnescaped)) + .catch(err => t.notOk(true, err.message)) + .then(() => t.end()); + }); + // Double Quote testRunner.add('should escape quotes with double quotes', (t) => { diff --git a/test/JSON2CSVParser.js b/test/JSON2CSVParser.js index 0445b5bc..07e6103f 100644 --- a/test/JSON2CSVParser.js +++ b/test/JSON2CSVParser.js @@ -507,6 +507,18 @@ module.exports = (testRunner, jsonFixtures, csvFixtures) => { t.end(); }); + testRunner.add('should not escape \'"\' when setting \'quote\' set to something else', (t) => { + const opts = { + quote: '\'' + }; + + const parser = new Json2csvParser(opts); + const csv = parser.parse(jsonFixtures.doubleQuotes); + + t.equal(csv, csvFixtures.doubleQuotesUnescaped); + t.end(); + }); + // Double Quote testRunner.add('should escape quotes with double quotes', (t) => { diff --git a/test/JSON2CSVTransform.js b/test/JSON2CSVTransform.js index 479cdef6..e384bac5 100644 --- a/test/JSON2CSVTransform.js +++ b/test/JSON2CSVTransform.js @@ -819,6 +819,27 @@ module.exports = (testRunner, jsonFixtures, csvFixtures, inMemoryJsonFixtures) = }); }); + testRunner.add('should not escape \'"\' when setting \'quote\' set to something else', (t) => { + const opts = { + quote: '\'' + }; + + const transform = new Json2csvTransform(opts); + const processor = jsonFixtures.doubleQuotes().pipe(transform); + + let csv = ''; + processor + .on('data', chunk => (csv += chunk.toString())) + .on('end', () => { + t.equal(csv, csvFixtures.doubleQuotesUnescaped); + t.end(); + }) + .on('error', err => { + t.notOk(true, err.message) + t.end(); + }); + }); + // Double Quote testRunner.add('should escape quotes with double quotes', (t) => { diff --git a/test/fixtures/csv/doubleQuotesUnescaped.csv b/test/fixtures/csv/doubleQuotesUnescaped.csv new file mode 100644 index 00000000..af79ed0f --- /dev/null +++ b/test/fixtures/csv/doubleQuotesUnescaped.csv @@ -0,0 +1,3 @@ +'a string' +'with a description' +'with a description and "quotes"' \ No newline at end of file