File tree Expand file tree Collapse file tree 3 files changed +18
-2
lines changed Expand file tree Collapse file tree 3 files changed +18
-2
lines changed Original file line number Diff line number Diff 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 ,
Original file line number Diff line number Diff 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 ) ;
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments