Skip to content
Open
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
9 changes: 9 additions & 0 deletions packages/csv-stringify/lib/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,14 @@ const stringifier = function (options, state, info) {
value,
record_delimiter,
);
// `parse` treats CR, LF and CRLF as record delimiters when
// `record_delimiter` is left to auto-detection (its default), so a
// field holding a bare CR or LF starts a new record and must be
// quoted to round-trip, even when it does not contain the configured
// `record_delimiter` (eg a lone "\r" stringified with the default
// "\n" record delimiter).
const containsNewline =
value.indexOf("\r") !== -1 || value.indexOf("\n") !== -1;
const quotedString = quoted_string && typeof field === "string";
let quotedMatch =
quoted_match &&
Expand Down Expand Up @@ -249,6 +257,7 @@ const stringifier = function (options, state, info) {
containsQuote === true ||
containsdelimiter ||
containsRecordDelimiter ||
containsNewline ||
quoted ||
quotedString ||
quotedMatch;
Expand Down
10 changes: 10 additions & 0 deletions packages/csv-stringify/test/option.record_delimiter.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,14 @@ describe("Option `record_delimiter`", function () {
},
);
});
it("quote a field containing a bare carriage return", function (next) {
// `parse` treats a lone CR as a record delimiter under its default
// auto-detection, so a field holding a CR must be quoted to round-trip even
// though it does not contain the default "\n" record delimiter.
stringify([["x\ry", "z"]], { eof: false }, (err, data) => {
if (err) return next(err);
data.toString().should.eql('"x\ry",z');
next();
});
});
});