Skip to content

Commit 00d9227

Browse files
committed
feat: add escapeHeaderNestedDots option to allow for unescaping generated header keys
Fixes #245
1 parent 7fda5f2 commit 00d9227

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

src/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export const defaultJson2CsvOptions: DefaultJson2CsvOptions = {
2525
eol : '\n'
2626
},
2727
emptyFieldValue: undefined,
28+
escapeHeaderNestedDots: true,
2829
excelBOM: false,
2930
excludeKeys: [],
3031
expandNestedObjects: true,

src/json2csv.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export const Json2Csv = function(options: FullJson2CsvOptions) {
1515
expandNestedObjects: options.expandNestedObjects,
1616
expandArrayObjects: expandingWithoutUnwinding,
1717
ignoreEmptyArraysWhenExpanding: expandingWithoutUnwinding,
18-
escapeNestedDots: true
18+
escapeNestedDots: true,
1919
};
2020

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

145145
params.header = params.headerFields
146146
.map(function(field) {
147-
const headerKey = fieldTitleMapKeys.includes(field) ? options.fieldTitleMap[field] : field;
147+
let headerKey = field;
148+
149+
// If a custom field title was provided for this field, use that
150+
if (fieldTitleMapKeys.includes(field)) {
151+
headerKey = options.fieldTitleMap[field];
152+
} else if (!options.escapeHeaderNestedDots) {
153+
// Otherwise, if the user doesn't want nested dots in keys to be escaped, then unescape them
154+
headerKey = headerKey.replace(/\\\./g, '.');
155+
}
156+
148157
return wrapFieldValueIfNecessary(headerKey);
149158
})
150159
.join(options.delimiter.field);

src/types.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,12 @@ export interface Json2CsvOptions extends SharedConverterOptions {
9898
*/
9999
emptyFieldValue?: unknown;
100100

101+
/**
102+
* Should dots (`.`) appearing in header keys be escaped with a preceding slash (`\`)?
103+
* @default true
104+
*/
105+
escapeHeaderNestedDots?: boolean;
106+
101107
/**
102108
* Should nested objects be deep-converted to CSV
103109
* @default true

0 commit comments

Comments
 (0)