JSON2CSVBase.js
8.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
'use strict';
const os = require('os');
const lodashGet = require('lodash.get');
const lodashSet = require('lodash.set');
const lodashCloneDeep = require('lodash.clonedeep');
class JSON2CSVBase {
constructor(opts) {
this.opts = this.preprocessOpts(opts);
}
/**
* Check passing opts and set defaults.
*
* @param {Json2CsvOptions} opts Options object containing fields,
* delimiter, default value, quote mark, header, etc.
*/
preprocessOpts(opts) {
const processedOpts = opts || {};
processedOpts.unwind = !Array.isArray(processedOpts.unwind)
? (processedOpts.unwind ? [processedOpts.unwind] : [])
: processedOpts.unwind
processedOpts.delimiter = processedOpts.delimiter || ',';
processedOpts.eol = processedOpts.eol || os.EOL;
processedOpts.quote = typeof processedOpts.quote === 'string'
? opts.quote
: '"';
processedOpts.doubleQuote = typeof processedOpts.doubleQuote === 'string'
? processedOpts.doubleQuote
: Array(3).join(processedOpts.quote);
processedOpts.defaultValue = processedOpts.defaultValue;
processedOpts.header = processedOpts.header !== false;
processedOpts.includeEmptyRows = processedOpts.includeEmptyRows || false;
processedOpts.withBOM = processedOpts.withBOM || false;
return processedOpts;
}
/**
* Create the title row with all the provided fields as column headings
*
* @returns {String} titles as a string
*/
getHeader() {
return this.opts.fields
.map(field =>
(typeof field === 'string')
? field
: (field.label || field.value)
)
.map(header => this.processValue(header, true))
.join(this.opts.delimiter);
}
/**
* Preprocess each object according to the give opts (unwind, flatten, etc.).
*
* @param {Object} row JSON object to be converted in a CSV row
*/
preprocessRow(row) {
const processedRow = (this.opts.unwind && this.opts.unwind.length)
? this.unwindData(row, this.opts.unwind)
: [row];
if (this.opts.flatten) {
return processedRow.map(this.flatten);
}
return processedRow;
}
/**
* Create the content of a specific CSV row
*
* @param {Object} row JSON object to be converted in a CSV row
* @returns {String} CSV string (row)
*/
processRow(row) {
if (!row
|| (Object.getOwnPropertyNames(row).length === 0
&& !this.opts.includeEmptyRows)) {
return undefined;
}
return this.opts.fields
.map(fieldInfo => this.processCell(row, fieldInfo))
.join(this.opts.delimiter);
}
/**
* Create the content of a specfic CSV row cell
*
* @param {Object} row JSON object representing the CSV row that the cell belongs to
* @param {Object} fieldInfo Details of the field to process to be a CSV cell
* @returns {String} CSV string (cell)
*/
processCell(row, fieldInfo) {
const stringify = typeof fieldInfo === 'object' && fieldInfo.stringify !== undefined
? fieldInfo.stringify
: true;
return this.processValue(this.getValue(row, fieldInfo), stringify);
}
/**
* Create the content of a specfic CSV row cell
*
* @param {Object} row JSON object representing the CSV row that the cell belongs to
* @param {Object} fieldInfo Details of the field to process to be a CSV cell
* @returns {Any} Field value
*/
getValue(row, fieldInfo) {
const defaultValue = typeof fieldInfo === 'object' && 'default' in fieldInfo
? fieldInfo.default
: this.opts.defaultValue;
let value;
if (fieldInfo) {
if (typeof fieldInfo === 'string') {
value = lodashGet(row, fieldInfo, defaultValue);
} else if (typeof fieldInfo === 'object') {
if (typeof fieldInfo.value === 'string') {
value = lodashGet(row, fieldInfo.value, defaultValue);
} else if (typeof fieldInfo.value === 'function') {
const field = {
label: fieldInfo.label,
default: fieldInfo.default
};
value = fieldInfo.value(row, field);
}
}
}
return (value === null || value === undefined)
? defaultValue
: value;
}
/**
* Create the content of a specfic CSV row cell
*
* @param {Any} value Value to be included in a CSV cell
* @param {Boolean} stringify Details of the field to process to be a CSV cell
* @returns {String} Value stringified and processed
*/
processValue(value, stringify) {
if (value === null || value === undefined) {
return undefined;
}
const isValueString = typeof value === 'string';
if (isValueString) {
value = value
.replace(/\n/g, '\u2028')
.replace(/\r/g, '\u2029');
}
//JSON.stringify('\\') results in a string with two backslash
//characters in it. I.e. '\\\\'.
let stringifiedValue = (stringify
? JSON.stringify(value)
: value);
if (typeof value === 'object' && !/^"(.*)"$/.test(stringifiedValue)) {
// Stringify object that are not stringified to a
// JSON string (like Date) to escape commas, quotes, etc.
stringifiedValue = JSON.stringify(stringifiedValue);
}
if (stringifiedValue === undefined) {
return undefined;
}
if (isValueString) {
stringifiedValue = stringifiedValue
.replace(/\u2028/g, '\n')
.replace(/\u2029/g, '\r');
}
if (this.opts.quote === '"') {
// Replace automatically scaped single quotes by doubleQuotes
stringifiedValue = stringifiedValue
.replace(/(\\")(?!$)/g, this.opts.doubleQuote);
} else {
// Unescape automatically escaped double quote symbol
// Replace wrapping quotes
// Replace single quote with double quote
stringifiedValue = stringifiedValue
.replace(/(\\")(?!$)/g, '"')
.replace(new RegExp(this.opts.quote, 'g'), this.opts.doubleQuote)
.replace(/^"(.*)"$/, this.opts.quote + '$1' + this.opts.quote);
}
// Remove double backslashes
stringifiedValue = stringifiedValue
.replace(/\\\\/g, '\\');
if (this.opts.excelStrings && typeof value === 'string') {
stringifiedValue = '"="' + stringifiedValue + '""';
}
return stringifiedValue;
}
/**
* Performs the flattening of a data row recursively
*
* @param {Object} dataRow Original JSON object
* @returns {Object} Flattened object
*/
flatten(dataRow) {
function step (obj, flatDataRow, currentPath) {
Object.keys(obj).forEach((key) => {
const value = obj[key];
const newPath = currentPath
? `${currentPath}.${key}`
: key;
if (typeof value !== 'object'
|| value === null
|| Array.isArray(value)
|| Object.prototype.toString.call(value.toJSON) === '[object Function]'
|| !Object.keys(value).length) {
flatDataRow[newPath] = value;
return;
}
step(value, flatDataRow, newPath);
});
return flatDataRow;
}
return step(dataRow, {});
}
/**
* Performs the unwind recursively in specified sequence
*
* @param {Object} dataRow Original JSON object
* @param {String[]} unwindPaths The paths as strings to be used to deconstruct the array
* @returns {Array} Array of objects containing all rows after unwind of chosen paths
*/
unwindData(dataRow, unwindPaths) {
const unwind = (rows, unwindPath) => {
const clone = unwindPath.indexOf('.') !== -1
? o => lodashCloneDeep(o)
: o => Object.assign({}, o);
return rows
.map(row => {
const unwindArray = lodashGet(row, unwindPath);
if (!Array.isArray(unwindArray)) {
return row;
}
if (!unwindArray.length) {
return lodashSet(clone(row), unwindPath, undefined);
}
return unwindArray.map((unwindRow, index) => {
const clonedRow = (this.opts.unwindBlank && index > 0)
? {}
: clone(row);
return lodashSet(clonedRow, unwindPath, unwindRow);
});
})
.reduce((a, e) => a.concat(e), []);
};
return unwindPaths.reduce(unwind, [dataRow]);
}
}
module.exports = JSON2CSVBase;