diff --git a/packages/csv-stringify/lib/api/index.js b/packages/csv-stringify/lib/api/index.js index 1fb77be2..b3f40e5d 100644 --- a/packages/csv-stringify/lib/api/index.js +++ b/packages/csv-stringify/lib/api/index.js @@ -256,7 +256,7 @@ const stringifier = function (options, state, info) { quoted || quotedString || quotedMatch; - if (shouldQuote === true && containsEscape === true) { + if (shouldQuote === true && containsEscape === true && quote !== "") { value = value.replaceAll(escape, () => escape + escape); } if (containsQuote === true) { diff --git a/packages/csv-stringify/test/option.quote.ts b/packages/csv-stringify/test/option.quote.ts index 66d1902f..9464a285 100644 --- a/packages/csv-stringify/test/option.quote.ts +++ b/packages/csv-stringify/test/option.quote.ts @@ -241,4 +241,55 @@ describe("Option `quote`", function () { eof: false, }).should.eql("|a\\|b|"); }); + + describe("disabled", function () { + it("preserves special characters when quoting is disabled", function () { + stringifySync([['say "hi", bob'], ['say "hi"\nbob']], { + quote: false, + }).should.eql('say "hi", bob\nsay "hi"\nbob\n'); + }); + + it("preserves fields selected for quoting when quoting is disabled", function () { + for (const options of [ + { quoted: true }, + { quoted_string: true }, + { quoted_match: /hi/ }, + ]) { + stringifySync([['say "hi"']], { quote: false, ...options }).should.eql( + 'say "hi"\n', + ); + } + }); + + it("preserves quotes supplied by a cast with quoting disabled", function () { + stringifySync([["abc", 1]], { + quoted: true, + cast: { + number: (value) => ({ value: `="${value}"`, quote: false }), + }, + }).should.eql('"abc",="1"\n'); + }); + + it("preserves custom escape characters when quoting is disabled", function () { + stringifySync([["C:\\dir,x"]], { + quote: false, + escape: "\\", + }).should.eql("C:\\dir,x\n"); + }); + + it("preserves quotes in fields containing a custom delimiter", function () { + stringifySync([['{"a":1}\tx']], { + quote: "", + delimiter: "\t", + }).should.eql('{"a":1}\tx\n'); + }); + + it("preserves selected fields in the callback API with quoting disabled", function (next) { + stringify([['say "hi"']], { quote: false, quoted: true }, (err, data) => { + if (err) return next(err); + data.should.eql('say "hi"\n'); + next(); + }); + }); + }); });