Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/json2csv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,9 @@ export const Json2Csv = function (options: DefaultJson2CsvOptions) {

// If a custom field title was provided for this field, use that
if (fieldTitleMapKeys.includes(field)) {
headerKey = options.fieldTitleMap[field];
// Wrap the title the same way field names and record values are wrapped,
// since a title may contain the field delimiter, wrap delimiter, or a line break.
headerKey = wrapFieldValueIfNecessary(options.fieldTitleMap[field]);
} else if (!options.escapeHeaderNestedDots) {
// Otherwise, if the user doesn't want nested dots in keys to be escaped, then unescape them
headerKey = headerKey.replace(/\\\./g, '.');
Expand Down
10 changes: 10 additions & 0 deletions test/json2csv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,16 @@ export function runTests() {
assert.equal(csv, csvTestData.unwindWithSpecifiedKeys.replace('data.category,data.options.name', 'Category,Option Name'));
});

it('should wrap a title that contains the field delimiter', () => {
const csv = json2csv([{ price: 100, qty: 2 }], {
keys: [
{ field: 'price', title: 'Price, USD' },
{ field: 'qty', title: 'Qty' }
]
});
assert.equal(csv, '"Price, USD",Qty\n100,2');
});

it('should exclude specified keys from the output', () => {
// Change the features array to be empty
const updatedCsvPerOptions = csvTestData.arrayObjects.replace('features.cons,', '')
Expand Down