Skip to content

Commit

Permalink
Merge pull request #248 from mrodrig/feat/add-escapeNestedDots-option
Browse files Browse the repository at this point in the history
Add escapeHeaderNestedDots option
  • Loading branch information
mrodrig authored Feb 23, 2024
2 parents 7fda5f2 + ea84114 commit 6b67f9c
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 2 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,27 @@ Returns the CSV `string` or rejects with an `Error` if there was an issue.
* Default: `\n`
* `emptyFieldValue` - Any - Value that, if specified, will be substituted in for field values that are `undefined`, `null`, or an empty string.
* Default: none
* `escapeHeaderNestedDots` - Boolean - Should nested dots in header keys be escaped?
* Default: `true`
* Example:
```json
[
{
"a.a": "1"
}
]
```
* `true` will generate the following CSV:
```csv
a\.a
1
```
* `false` will generate the following CSV:
```csv
a.a
1
```
* Note: This may result in CSV output that does not map back exactly to the original JSON.
* `excelBOM` - Boolean - Should a unicode character be prepended to allow Excel to open a UTF-8 encoded file with non-ASCII characters present.
* `excludeKeys` - Array - Specify the keys that should be excluded from the output. Provided keys will also be used as a RegExp to help exclude keys under a specified prefix, such as all keys of Objects in an Array when `expandArrayObjects` is `true`.
* Default: `[]`
Expand Down
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export const defaultJson2CsvOptions: DefaultJson2CsvOptions = {
eol : '\n'
},
emptyFieldValue: undefined,
escapeHeaderNestedDots: true,
excelBOM: false,
excludeKeys: [],
expandNestedObjects: true,
Expand Down
13 changes: 11 additions & 2 deletions src/json2csv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export const Json2Csv = function(options: FullJson2CsvOptions) {
expandNestedObjects: options.expandNestedObjects,
expandArrayObjects: expandingWithoutUnwinding,
ignoreEmptyArraysWhenExpanding: expandingWithoutUnwinding,
escapeNestedDots: true
escapeNestedDots: true,
};

/** HEADER FIELD FUNCTIONS **/
Expand Down Expand Up @@ -144,7 +144,16 @@ export const Json2Csv = function(options: FullJson2CsvOptions) {

params.header = params.headerFields
.map(function(field) {
const headerKey = fieldTitleMapKeys.includes(field) ? options.fieldTitleMap[field] : field;
let headerKey = field;

// If a custom field title was provided for this field, use that
if (fieldTitleMapKeys.includes(field)) {
headerKey = 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, '.');
}

return wrapFieldValueIfNecessary(headerKey);
})
.join(options.delimiter.field);
Expand Down
6 changes: 6 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ export interface Json2CsvOptions extends SharedConverterOptions {
*/
emptyFieldValue?: unknown;

/**
* Should dots (`.`) appearing in header keys be escaped with a preceding slash (`\`)?
* @default true
*/
escapeHeaderNestedDots?: boolean;

/**
* Should nested objects be deep-converted to CSV
* @default true
Expand Down
12 changes: 12 additions & 0 deletions test/json2csv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,18 @@ export function runTests() {
assert.equal(csv, csvTestData.excludeKeyPattern);
});

// Test case for #245
it('should not escape nested dots in keys with nested dots in them if turned on via the option', () => {
const csv = json2csv(jsonTestData.nestedDotKeys, { escapeHeaderNestedDots: true }); // Default option value
assert.equal(csv, csvTestData.nestedDotKeys);
});

// Test case for #245
it('should not escape nested dots in keys with nested dots in them if turned off via the option', () => {
const csv = json2csv(jsonTestData.nestedDotKeys, { escapeHeaderNestedDots: false });
assert.equal(csv, csvTestData.nestedDotKeys.replace(/\\\./g, '.'));
});

it('should use a custom value parser function when provided', () => {
const updatedCsv = csvTestData.trimmedFields.split('\n');
const textRow = 'Parsed Value,Parsed Value,Parsed Value,Parsed Value,Parsed Value';
Expand Down

0 comments on commit 6b67f9c

Please sign in to comment.