Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/csv-stringify/lib/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
51 changes: 51 additions & 0 deletions packages/csv-stringify/test/option.quote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
});
});