Skip to content

Commit 515df0b

Browse files
committed
Process header cells as any other cell
1 parent efe9888 commit 515df0b

File tree

2 files changed

+30
-12
lines changed

2 files changed

+30
-12
lines changed

lib/JSON2CSVBase.js

+29-11
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,8 @@ class JSON2CSVBase {
4949
(typeof field === 'string')
5050
? field
5151
: (field.label || field.value)
52-
).map(header =>
53-
JSON.stringify(header)
54-
.replace(/"/g, this.params.quote)
5552
)
53+
.map(header => this.processValue(header, true))
5654
.join(this.params.delimiter);
5755
}
5856

@@ -86,7 +84,7 @@ class JSON2CSVBase {
8684
}
8785

8886
return this.params.fields
89-
.map(fieldInfo => this.processField(row, fieldInfo))
87+
.map(fieldInfo => this.processCell(row, fieldInfo))
9088
.join(this.params.delimiter);
9189
}
9290

@@ -97,14 +95,25 @@ class JSON2CSVBase {
9795
* @param {Object} fieldInfo Details of the field to process to be a CSV cell
9896
* @returns {String} CSV string (cell)
9997
*/
100-
processField(row, fieldInfo) {
101-
const isFieldInfoObject = typeof fieldInfo === 'object';
102-
const defaultValue = isFieldInfoObject && 'default' in fieldInfo
103-
? fieldInfo.default
104-
: this.params.defaultValue;
105-
const stringify = isFieldInfoObject && fieldInfo.stringify !== undefined
98+
processCell(row, fieldInfo) {
99+
const stringify = typeof fieldInfo === 'object' && fieldInfo.stringify !== undefined
106100
? fieldInfo.stringify
107101
: true;
102+
103+
return this.processValue(this.getValue(row, fieldInfo), stringify);
104+
}
105+
106+
/**
107+
* Create the content of a specfic CSV row cell
108+
*
109+
* @param {Object} row JSON object representing the CSV row that the cell belongs to
110+
* @param {Object} fieldInfo Details of the field to process to be a CSV cell
111+
* @returns {Any} Field value
112+
*/
113+
getValue(row, fieldInfo) {
114+
const defaultValue = typeof fieldInfo === 'object' && 'default' in fieldInfo
115+
? fieldInfo.default
116+
: this.params.defaultValue;
108117

109118
let value;
110119
if (fieldInfo) {
@@ -123,10 +132,19 @@ class JSON2CSVBase {
123132
}
124133
}
125134

126-
value = (value === null || value === undefined)
135+
return (value === null || value === undefined)
127136
? defaultValue
128137
: value;
138+
}
129139

140+
/**
141+
* Create the content of a specfic CSV row cell
142+
*
143+
* @param {Any} value Value to be included in a CSV cell
144+
* @param {Boolean} stringify Details of the field to process to be a CSV cell
145+
* @returns {String} Value stringified and processed
146+
*/
147+
processValue(value, stringify) {
130148
if (value === null || value === undefined) {
131149
return undefined;
132150
}

test/fixtures/csv/excelStrings.csv

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"carModel","price","color"
1+
"=""carModel""","=""price""","=""color"""
22
"=""Audi""",0,"=""blue"""
33
"=""BMW""",15000,"=""red"""
44
"=""Mercedes""",20000,"=""yellow"""

0 commit comments

Comments
 (0)