Skip to content

Commit

Permalink
fix: Avoid throwing an error on elements that can't be stringified (l…
Browse files Browse the repository at this point in the history
…ike functions) (#223)
  • Loading branch information
juanjoDiaz authored and knownasilya committed Jan 21, 2018
1 parent f71a3df commit 679c687
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 32 deletions.
66 changes: 34 additions & 32 deletions lib/json2csv.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,39 +248,41 @@ function createColumnContent(params, str) {
stringifiedElement = JSON.stringify(val);
}

if (params.preserveNewLinesInValues && typeof val === 'string') {
stringifiedElement = stringifiedElement
.replace(/\u2028/g, '\n')
.replace(/\u2029/g, '\r');
}

if (typeof val === 'object') {
// In some cases (e.g. val is a Date), stringifiedElement is already a quoted string.
// Strip the leading and trailing quote if so, so we don't end up double-quoting it
stringifiedElement = replaceQuotationMarks(stringifiedElement, '');

// If val is a normal object, we want to escape its JSON so any commas etc
// don't get interpreted as field separators
stringifiedElement = JSON.stringify(stringifiedElement);
}

if (params.quote !== '"') {
stringifiedElement = replaceQuotationMarks(stringifiedElement, params.quote);
if (stringifiedElement !== undefined) {
if (params.preserveNewLinesInValues && typeof val === 'string') {
stringifiedElement = stringifiedElement
.replace(/\u2028/g, '\n')
.replace(/\u2029/g, '\r');
}

if (typeof val === 'object') {
// In some cases (e.g. val is a Date), stringifiedElement is already a quoted string.
// Strip the leading and trailing quote if so, so we don't end up double-quoting it
stringifiedElement = replaceQuotationMarks(stringifiedElement, '');

// If val is a normal object, we want to escape its JSON so any commas etc
// don't get interpreted as field separators
stringifiedElement = JSON.stringify(stringifiedElement);
}

if (params.quote !== '"') {
stringifiedElement = replaceQuotationMarks(stringifiedElement, params.quote);
}

//JSON.stringify('\\') results in a string with two backslash
//characters in it. I.e. '\\\\'.
stringifiedElement = stringifiedElement.replace(/\\\\/g, '\\');

if (params.excelStrings && typeof val === 'string') {
stringifiedElement = '"="' + stringifiedElement + '""';
}

//Replace single quote with double quote. Single quote are preceeded by
//a backslash, and it's not at the end of the stringifiedElement.
stringifiedElement = stringifiedElement.replace(/(\\")(?=.)/g, params.doubleQuote);

line += stringifiedElement;
}

//JSON.stringify('\\') results in a string with two backslash
//characters in it. I.e. '\\\\'.
stringifiedElement = stringifiedElement.replace(/\\\\/g, '\\');

if (params.excelStrings && typeof val === 'string') {
stringifiedElement = '"="' + stringifiedElement + '""';
}

//Replace single quote with double quote. Single quote are preceeded by
//a backslash, and it's not at the end of the stringifiedElement.
stringifiedElement = stringifiedElement.replace(/(\\")(?=.)/g, params.doubleQuote);

line += stringifiedElement;
}

line += params.delimiter;
Expand Down
13 changes: 13 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,19 @@ async.parallel(loadFixtures(csvFixtures), function (err) {
});
});

test('should parse json to csv even if json include functions', function (t) {
json2csv({
data: {
a: 1,
funct: function (a) { return a + 1; },
}
}, function (error, csv) {
t.error(error);
t.equal(csv, '"a","funct"\n1,');
t.end();
});
});

test('should parse data:{} to csv with only column title', function (t) {
json2csv({
data: {},
Expand Down

0 comments on commit 679c687

Please sign in to comment.