Skip to content

fix(csv-stringify): quote fields prefixed by escape_formulas - #516

Open
abhu85 wants to merge 1 commit into
adaltas:masterfrom
abhu85:fix/escape-formulas-custom-quote
Open

abhu85 wants to merge 1 commit into
adaltas:masterfrom
abhu85:fix/escape-formulas-custom-quote

Conversation

@abhu85

@abhu85 abhu85 commented Sep 30, 2026

Copy link
Copy Markdown

With escape_formulas: true, the ' prefix is added after the checks that decide whether a field has to be quoted (containsQuote, containsdelimiter, containsEscape, containsRecordDelimiter). When any of quote, escape, delimiter or record_delimiter is set to ', the added character is neither escaped nor does it trigger quoting, and csv-parse can no longer read the output back with the same options:

stringify([["=1", "x"]], { escape_formulas: true, quote: "'" })
// '=1,x         -> parse(..., { quote: "'" }) throws "Quote Not Closed"
stringify([["=a", 1], ["b", 2]], { escape_formulas: true, record_delimiter: "'", eof: false })
// '=a,1'b,2     -> parse(..., { record_delimiter: "'" }) throws "Invalid Record Length"

This PR moves those four checks after the escape_formulas block, so they see the prefixed value:

stringify([["=1", "x"]], { escape_formulas: true, quote: "'" })
// '"'=1',x      -> [["'=1", "x"]]

quoted_match is still tested against the original value, since it expresses which user values should be quoted (e.g. quoted_match: /^=/ keeps matching formula-like input). Output with the default options is unchanged.

Tests: added one case each for quote, escape, delimiter and record_delimiter set to ' in test/option.escape_formulas.ts. All four fail without the change. npm test in packages/csv-stringify: 236 passing; the only tsc error, in test/option.cast.ts, is already on master since #511.

With `escape_formulas`, a leading `'` is prepended to the value after the
checks deciding whether the field must be quoted were already computed.
When `quote`, `escape`, `delimiter` or `record_delimiter` is set to `'`,
the added character is neither escaped nor does it trigger quoting, so
the output cannot be parsed back, e.g. `[["=1", "x"]]` with `quote: "'"`
produced `'=1,x`.

Run those checks on the prefixed value. `quoted_match` is still tested
against the original value, since it expresses which user values should
be quoted.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant