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
18 changes: 10 additions & 8 deletions packages/csv-stringify/lib/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,14 +212,6 @@ const stringifier = function (options, state, info) {
),
];
}
const containsdelimiter = emits_separator(value, [delimiter]);
const containsQuote = quote !== "" && value.indexOf(quote) >= 0;
const containsEscape = value.indexOf(escape) >= 0 && escape !== quote;
// Testing `\n` and `\r` covers the three sequences `parse` discovers
const containsRecordDelimiter = emits_separator(value, [
record_delimiter,
...(quote_record_delimiter === false ? [] : ["\n", "\r"]),
]);
const quotedString = quoted_string && typeof field === "string";
const quotedMatch = matches_quoted_match(value, quoted_match);
// See https://github.com/adaltas/node-csv/pull/387
Expand Down Expand Up @@ -249,6 +241,16 @@ const stringifier = function (options, state, info) {
break;
}
}
// Inspect the value once the formula prefix is added, it may match
// the `quote`, `escape` or `delimiter` options
const containsdelimiter = emits_separator(value, [delimiter]);
const containsQuote = quote !== "" && value.indexOf(quote) >= 0;
const containsEscape = value.indexOf(escape) >= 0 && escape !== quote;
// Testing `\n` and `\r` covers the three sequences `parse` discovers
const containsRecordDelimiter = emits_separator(value, [
record_delimiter,
...(quote_record_delimiter === false ? [] : ["\n", "\r"]),
]);
const shouldQuote =
containsQuote === true ||
containsdelimiter ||
Expand Down
73 changes: 73 additions & 0 deletions packages/csv-stringify/test/option.escape_formulas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,77 @@ describe("Option `escape_formulas`", function () {
},
);
});

it("with a `quote` option set to the escape prefix", function (next) {
stringify(
[
["=a", 1],
["=b,c", 2],
],
{
escape_formulas: true,
quote: "'",
eof: false,
},
(err, data) => {
if (err) return next(err);
data.should.eql(dedent`
'"'=a',1
'"'=b,c',2
`);
next();
},
);
});

it("with an `escape` option set to the escape prefix", function (next) {
stringify(
[["=a,b", 1]],
{
escape_formulas: true,
escape: "'",
eof: false,
},
(err, data) => {
if (err) return next(err);
data.should.eql(`"''=a,b",1`);
next();
},
);
});

it("with a `delimiter` option set to the escape prefix", function (next) {
stringify(
[["=a", 1]],
{
escape_formulas: true,
delimiter: "'",
eof: false,
},
(err, data) => {
if (err) return next(err);
data.should.eql(`"'=a"'1`);
next();
},
);
});

it("with a `record_delimiter` option set to the escape prefix", function (next) {
stringify(
[
["=a", 1],
["b", 2],
],
{
escape_formulas: true,
record_delimiter: "'",
eof: false,
},
(err, data) => {
if (err) return next(err);
data.should.eql(`"'=a",1'b,2`);
next();
},
);
});
});