Skip to content

Commit 679c687

Browse files
juanjoDiazknownasilya
authored andcommitted
fix: Avoid throwing an error on elements that can't be stringified (like functions) (#223)
1 parent f71a3df commit 679c687

File tree

2 files changed

+47
-32
lines changed

2 files changed

+47
-32
lines changed

lib/json2csv.js

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -248,39 +248,41 @@ function createColumnContent(params, str) {
248248
stringifiedElement = JSON.stringify(val);
249249
}
250250

251-
if (params.preserveNewLinesInValues && typeof val === 'string') {
252-
stringifiedElement = stringifiedElement
253-
.replace(/\u2028/g, '\n')
254-
.replace(/\u2029/g, '\r');
255-
}
256-
257-
if (typeof val === 'object') {
258-
// In some cases (e.g. val is a Date), stringifiedElement is already a quoted string.
259-
// Strip the leading and trailing quote if so, so we don't end up double-quoting it
260-
stringifiedElement = replaceQuotationMarks(stringifiedElement, '');
261-
262-
// If val is a normal object, we want to escape its JSON so any commas etc
263-
// don't get interpreted as field separators
264-
stringifiedElement = JSON.stringify(stringifiedElement);
265-
}
266-
267-
if (params.quote !== '"') {
268-
stringifiedElement = replaceQuotationMarks(stringifiedElement, params.quote);
251+
if (stringifiedElement !== undefined) {
252+
if (params.preserveNewLinesInValues && typeof val === 'string') {
253+
stringifiedElement = stringifiedElement
254+
.replace(/\u2028/g, '\n')
255+
.replace(/\u2029/g, '\r');
256+
}
257+
258+
if (typeof val === 'object') {
259+
// In some cases (e.g. val is a Date), stringifiedElement is already a quoted string.
260+
// Strip the leading and trailing quote if so, so we don't end up double-quoting it
261+
stringifiedElement = replaceQuotationMarks(stringifiedElement, '');
262+
263+
// If val is a normal object, we want to escape its JSON so any commas etc
264+
// don't get interpreted as field separators
265+
stringifiedElement = JSON.stringify(stringifiedElement);
266+
}
267+
268+
if (params.quote !== '"') {
269+
stringifiedElement = replaceQuotationMarks(stringifiedElement, params.quote);
270+
}
271+
272+
//JSON.stringify('\\') results in a string with two backslash
273+
//characters in it. I.e. '\\\\'.
274+
stringifiedElement = stringifiedElement.replace(/\\\\/g, '\\');
275+
276+
if (params.excelStrings && typeof val === 'string') {
277+
stringifiedElement = '"="' + stringifiedElement + '""';
278+
}
279+
280+
//Replace single quote with double quote. Single quote are preceeded by
281+
//a backslash, and it's not at the end of the stringifiedElement.
282+
stringifiedElement = stringifiedElement.replace(/(\\")(?=.)/g, params.doubleQuote);
283+
284+
line += stringifiedElement;
269285
}
270-
271-
//JSON.stringify('\\') results in a string with two backslash
272-
//characters in it. I.e. '\\\\'.
273-
stringifiedElement = stringifiedElement.replace(/\\\\/g, '\\');
274-
275-
if (params.excelStrings && typeof val === 'string') {
276-
stringifiedElement = '"="' + stringifiedElement + '""';
277-
}
278-
279-
//Replace single quote with double quote. Single quote are preceeded by
280-
//a backslash, and it's not at the end of the stringifiedElement.
281-
stringifiedElement = stringifiedElement.replace(/(\\")(?=.)/g, params.doubleQuote);
282-
283-
line += stringifiedElement;
284286
}
285287

286288
line += params.delimiter;

test/index.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,19 @@ async.parallel(loadFixtures(csvFixtures), function (err) {
123123
});
124124
});
125125

126+
test('should parse json to csv even if json include functions', function (t) {
127+
json2csv({
128+
data: {
129+
a: 1,
130+
funct: function (a) { return a + 1; },
131+
}
132+
}, function (error, csv) {
133+
t.error(error);
134+
t.equal(csv, '"a","funct"\n1,');
135+
t.end();
136+
});
137+
});
138+
126139
test('should parse data:{} to csv with only column title', function (t) {
127140
json2csv({
128141
data: {},

0 commit comments

Comments
 (0)