Skip to content

Commit 84c13aa

Browse files
authored
Fix #149 (#150)
* Handle invalid parsed values from JSON.parse in csv2json Since JSON.parse "interprets" some values, such as 39e1804 which is converted to Infinity. However, this isn't desirable since we want to return as close to the original value as possible. This commit adds some minor logic to verify that JSON.parse didn't determine the value is Infinity or -Infinity before setting the value in the returned JSON. If this is the case, the value is simply returned as the same string that's contained in the CSV. Fixes #149 * Version bump to 3.7.1
1 parent 826b1b1 commit 84c13aa

File tree

9 files changed

+36
-7
lines changed

9 files changed

+36
-7
lines changed

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
},
66
"name": "json-2-csv",
77
"description": "A JSON to CSV and CSV to JSON converter that natively supports sub-documents and auto-generates the CSV heading.",
8-
"version": "3.7.0",
8+
"version": "3.7.1",
99
"repository": {
1010
"type": "git",
1111
"url": "https://github.com/mrodrig/json-2-csv.git"

src/csv2json.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ const Csv2Json = function(options) {
242242
let parsedJson = parseValue(fieldValue);
243243
// If parsedJson is anything aside from an error, then we want to use the parsed value
244244
// This allows us to interpret values like 'null' --> null, 'false' --> false
245-
if (!utils.isError(parsedJson)) {
245+
if (!utils.isError(parsedJson) && !utils.isInvalid(parsedJson)) {
246246
fieldValue = parsedJson;
247247
} else if (fieldValue === 'undefined') {
248248
fieldValue = undefined;

src/utils.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ module.exports = {
1515
removeEmptyFields,
1616
getNCharacters,
1717
unwind,
18+
isInvalid,
1819

1920
// underscore replacements:
2021
isString,
@@ -282,3 +283,12 @@ function unique(array) {
282283
function flatten(array) {
283284
return [].concat(...array);
284285
}
286+
287+
/**
288+
* Used to help avoid incorrect values returned by JSON.parse when converting
289+
* CSV back to JSON, such as '39e1804' which JSON.parse converts to Infinity
290+
*/
291+
function isInvalid(parsedJson) {
292+
return parsedJson === Infinity ||
293+
parsedJson === -Infinity;
294+
}

test/config/testCsvFilesList.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ const fs = require('fs'),
3131
{key: 'csvEmptyLastValue', file: '../data/csv/csvEmptyLastValue.csv'},
3232
{key: 'unwind', file: '../data/csv/unwind.csv'},
3333
{key: 'unwindWithSpecifiedKeys', file: '../data/csv/unwindWithSpecifiedKeys.csv'},
34-
{key: 'localeFormat', file: '../data/csv/localeFormat.csv'}
34+
{key: 'localeFormat', file: '../data/csv/localeFormat.csv'},
35+
{key: 'invalidParsedValues', file: '../data/csv/invalidParsedValues.csv'}
3536
];
3637

3738
function readCsvFile(filePath) {

test/config/testJsonFilesList.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,6 @@ module.exports = {
2323
quotedEmptyFieldValue: require('../data/json/quotedEmptyFieldValue'),
2424
csvEmptyLastValue: require('../data/json/csvEmptyLastValue'),
2525
unwind: require('../data/json/unwind'),
26-
localeFormat: require('../data/json/localeFormat')
26+
localeFormat: require('../data/json/localeFormat'),
27+
invalidParsedValues: require('../data/json/invalidParsedValues')
2728
};

test/csv2json.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,15 @@ function runTests(jsonTestData, csvTestData) {
162162
done();
163163
});
164164
});
165+
166+
// Test case for #149
167+
it('should properly handle invalid parsed values', (done) => {
168+
converter.csv2json(csvTestData.invalidParsedValues, (err, json) => {
169+
if (err) done(err);
170+
json.should.deepEqual(jsonTestData.invalidParsedValues);
171+
done();
172+
});
173+
});
165174
});
166175

167176
describe('Error Handling', () => {
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
code1,code2
2+
39e1804,-39e1804
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[
2+
{
3+
"code1": "39e1804",
4+
"code2": "-39e1804"
5+
}
6+
]

0 commit comments

Comments
 (0)