node-csv
node-csv copied to clipboard
Support multiple quote characters when parsing
Summary
The library should be able to parse CSVs while considering multiple different potential quote character used for each value independently. E.g. allow a CSV mixing both single quote and double quotes.
Motivation
Use cases where CSVs mix values ported over from other formats (e.g. literals in a programming language) which enclose values in single quotes, and manually input. Reconciling the two quote styles would enable parsing the CSV regardless of which (if any) quote character was used.
Alternative
Regex to strip replace single quotes with double quotes via a utility, though this will break quotes in values.
Draft
Let the quote option take in an array of characters, and consider each when parsing the values.
Example unit test:
const records = parse(`
a,"b",'c'
'd',e,"f"
`.trim(), {
quote: ['"', '\''],
});
assert.deepStrictEqual(
records, [
['a', 'b', 'c'],
['d', 'e', 'f']
]
);
Additional context
Similar to https://github.com/adaltas/node-csv/issues/400. Perhaps automated detection is a sensible option.
We do it for some other options, not too hard to implement so no problem to go this direction.
I can propose a PR, seems the changes would be fairly localized. Lmk if that would be any help
Agreed, please propose a PR. (not sure what Lmk stands for)