Skip to content

feat(csv-parse): accept an array of quote characters - #560

Open
lyn1dev wants to merge 1 commit into
adaltas:masterfrom
lyn1dev:feat/csv-parse-multiple-quote-characters
Open

lyn1dev wants to merge 1 commit into
adaltas:masterfrom
lyn1dev:feat/csv-parse-multiple-quote-characters

Conversation

@lyn1dev

@lyn1dev lyn1dev commented Nov 9, 2026

Copy link
Copy Markdown

Fixes #431.

quote accepts an array of characters, so a single input can mix fields quoted with different characters:

parse(`a,"b",'c'\n'd',e,"f"`, { quote: ['"', "'"] });
// [ [ 'a', 'b', 'c' ], [ 'd', 'e', 'f' ] ]

Changes:

  • normalize_options normalizes quote to an array of buffers, the shape delimiter already uses. quote: '"', quote: Buffer.from('"') and quote: ['"', "'"] all work, and quote: [] behaves like quote: false.
  • The tokenizer records the quote which opened the current field and only that quote closes it, so "a'b" keeps the single quote and 'a"b' keeps the double quote. Escaping still applies to the quote in use.
  • __needMoreData and state.needMoreDataSize use the longest configured quote, so multi character quotes split across chunks are still matched.
  • The quote JSDoc in lib/options.d.ts documents the array input. The website documentation is published from the separate node-csv-docs repository, so it is not part of this diff.

Tests in packages/csv-parse/test/option.quote.ts cover the mixed input above, writes of one character at a time, multi character quotes, the other quote inside a field, relax_quotes, fields containing delimiters, and the unclosed quote error. The quote normalization assertions were updated to the array shape.

Verification, run in packages/csv-parse:

  • npx tsc --noEmit: no errors.
  • npx mocha "test/**/*.{js,ts}" --ignore test/samples.js: 568 passing, 2 pending. test/samples.js is ignored because it resolves import.meta.url with path.resolve and fails on Windows paths with or without this change.
  • npx eslint on the changed files: clean.
  • packages/csv, packages/csv-stringify and packages/stream-transform: type check and tests pass.

AI was used to write this code.

The `quote` option now accepts an array of characters, so a single input can
mix fields quoted with different characters, for example double and single
quotes. The option is normalized to an array of buffers, the same way the
`delimiter` option already is.

A quoted field is closed by the quote which opened it, so the other quote
characters are kept as regular content inside the field. Escaping still only
applies to the quote in use, and the streaming parser buffers enough bytes for
the longest configured quote to be matched across chunks.
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.

Support multiple quote characters when parsing

1 participant