@@ -49,10 +49,8 @@ class JSON2CSVBase {
49
49
( typeof field === 'string' )
50
50
? field
51
51
: ( field . label || field . value )
52
- ) . map ( header =>
53
- JSON . stringify ( header )
54
- . replace ( / " / g, this . params . quote )
55
52
)
53
+ . map ( header => this . processValue ( header , true ) )
56
54
. join ( this . params . delimiter ) ;
57
55
}
58
56
@@ -86,7 +84,7 @@ class JSON2CSVBase {
86
84
}
87
85
88
86
return this . params . fields
89
- . map ( fieldInfo => this . processField ( row , fieldInfo ) )
87
+ . map ( fieldInfo => this . processCell ( row , fieldInfo ) )
90
88
. join ( this . params . delimiter ) ;
91
89
}
92
90
@@ -97,14 +95,25 @@ class JSON2CSVBase {
97
95
* @param {Object } fieldInfo Details of the field to process to be a CSV cell
98
96
* @returns {String } CSV string (cell)
99
97
*/
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
106
100
? fieldInfo . stringify
107
101
: 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 ;
108
117
109
118
let value ;
110
119
if ( fieldInfo ) {
@@ -123,10 +132,19 @@ class JSON2CSVBase {
123
132
}
124
133
}
125
134
126
- value = ( value === null || value === undefined )
135
+ return ( value === null || value === undefined )
127
136
? defaultValue
128
137
: value ;
138
+ }
129
139
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 ) {
130
148
if ( value === null || value === undefined ) {
131
149
return undefined ;
132
150
}
0 commit comments