Skip to content

fix: wrap custom field titles that contain the delimiter#286

Open
greymoth-jp wants to merge 1 commit into
mrodrig:mainfrom
greymoth-jp:fix-wrap-titles-with-delimiter
Open

fix: wrap custom field titles that contain the delimiter#286
greymoth-jp wants to merge 1 commit into
mrodrig:mainfrom
greymoth-jp:fix-wrap-titles-with-delimiter

Conversation

@greymoth-jp

Copy link
Copy Markdown

When a key is given a custom title via keys: [{ field, title }], the title is written into the header row without passing through wrapFieldValueIfNecessary. Field names (in wrapHeaderFields) and record values both go through that helper, so a title that contains the field delimiter, the wrap delimiter, or a line break leaks into the header and breaks the column layout.

json2csv([{ price: 100, qty: 2 }], {
  keys: [
    { field: 'price', title: 'Price, USD' },
    { field: 'qty', title: 'Qty' },
  ],
});
// header row: Price, USD,Qty   (three columns instead of two)

Reading that output back with csv2json returns {"Price":100," USD":2,"Qty":""} rather than two aligned columns.

RFC 4180 treats the header line as a normal record (section 2.3), and any field containing the delimiter or a double quote must be enclosed in double quotes (sections 2.6 and 2.7). The title replaces a field name in the header, so it needs the same wrapping the field name would have received.

The fix wraps the title in generateCsvHeader with the existing wrapFieldValueIfNecessary helper. Non-titled header fields are already wrapped earlier in wrapHeaderFields, so only the title branch is changed, which avoids double-wrapping.

Added a regression test; the full suite passes.

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