fix(csv-stringify): quote fields containing a carriage return#485
fix(csv-stringify): quote fields containing a carriage return#485sarathfrancis90 wants to merge 1 commit into
Conversation
|
The detection shall be generic. The current implementation checks if value contains record_delimiter, which ever the characters and without treating |
4231bda to
aa8f100
Compare
|
You are right, and sorry for the delay here. Reworked along those lines: the quoting test stays generic and I moved the fix to the defaults, which is where the asymmetry you described actually is. Kept it off
|
|
Instead of returning |
aa8f100 to
afcd986
Compare
|
Done, that is a cleaner shape than what I had.
Setting it explicitly works in both directions — Tests are in One thing worth flagging: if a |
afcd986 to
98a4bed
Compare
|
Rebased on master — it had moved under me while I was writing that. The check now composes with your const containsRecordDelimiter =
emits_separator(value, record_delimiter) ||
(quote_record_delimiter === true &&
(emits_separator(value, "\n") || emits_separator(value, "\r")));Both are single characters so the fusion branch is inert for them, but it keeps the three checks reading the same way. 214 tests and the round trip still pass. |
Could you illustrate this with a test and we'll be good. |
Left to its default, `parse` discovers `\r\n`, `\n` and `\r` as record delimiters while `stringify` only writes `\n`, so a field holding a lone `\r` was written unquoted and read back as several records. The option quotes a field against the three sequences. It defaults to true unless `record_delimiter` is provided, in which case that delimiter stays the only one quoted against.
98a4bed to
a6af2c9
Compare
|
Added three, and writing them corrected what I said above. The cast So the cases are: a 217 tests pass. |
A field holding a lone carriage return is written unquoted, because the quoting check only looks for the configured
record_delimiter(\nby default). The parser treats both\rand\nas record delimiters, so the value gets split apart and the document no longer round-trips:\nand\r\nare already quoted; only a bare\rslips through.The fix: when the record delimiter is newline-based, quote any field that contains
\ror\n. A custom, non-newline delimiter still leaves line breaks as data, so that behaviour is unchanged.Found while fuzzing the
stringify→parseround trip. Added a regression test; the escape-formulas test is updated since a\r-leading field is now correctly quoted. Fullcsv-stringifysuite (202) andtscpass.