Skip to content

Commit 2807432

Browse files
author
Andreas Krummsdorf
committed
fix converting of boolean values in json-2-csv
* `true` gets converted to "true" * `false` gets converted to "false"
1 parent 745f7c5 commit 2807432

File tree

6 files changed

+24
-0
lines changed

6 files changed

+24
-0
lines changed

lib/json-2-csv.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ var convertField = function (value) {
115115
return options.DELIMITER.WRAP + convertData(value, _.keys(value)) + options.DELIMITER.WRAP; // Push the recursively generated CSV
116116
} else if (_.isNumber(value)) { // If we have a number (avoids 0 being converted to '')
117117
return options.DELIMITER.WRAP + value.toString() + options.DELIMITER.WRAP;
118+
} else if (_.isBoolean(value)) { // If we have a boolean (avoids false being converted to '')
119+
return options.DELIMITER.WRAP + value.toString() + options.DELIMITER.WRAP;
118120
}
119121
return options.DELIMITER.WRAP + (value ? value.toString() : '') + options.DELIMITER.WRAP; // Otherwise push the current value
120122
};
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
name,email,country,githubUsername,human,hipster
2+
mrodrig,,USA,mrodrig,true,false
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "mrodrig",
3+
"email": null,
4+
"country": "USA",
5+
"githubUsername": "mrodrig",
6+
"human": true,
7+
"hipster": false
8+
}

test/testCsvFilesList.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
{"key": "noData", "file": "test/CSV/unQuoted/noData.csv"},
1111
{"key": "regularJson", "file": "test/CSV/unQuoted/regularJson.csv"},
1212
{"key": "singleDoc", "file": "test/CSV/unQuoted/singleDoc.csv"},
13+
{"key": "singleDocWithBoolean", "file": "test/CSV/unQuoted/singleDocWithBoolean.csv"},
1314
{"key": "differentSchemas", "file": "test/CSV/unQuoted/differentSchemas.csv"}
1415
]
1516
},

test/testJson2Csv.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,16 @@ var json2csvTests = function () {
7676
});
7777
});
7878

79+
it('should parse a single JSON document with Boolean to CSV', function (done) {
80+
converter.json2csv(jsonTestData.singleDocWithBoolean, function (err, csv) {
81+
if (err) { throw err; }
82+
true.should.equal(_.isEqual(err, null));
83+
csv.should.equal(csvTestData.unQuoted.singleDocWithBoolean);
84+
csv.split(options.EOL).length.should.equal(3);
85+
done();
86+
});
87+
});
88+
7989
it('should parse an array of JSON documents to CSV', function (done) {
8090
converter.json2csv(jsonTestData.arrayValue, function (err, csv) {
8191
if (err) { throw err; }

test/testJsonFilesList.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ module.exports = {
99
noData: require('./JSON/noData'),
1010
regularJson: require('./JSON/regularJson'),
1111
singleDoc: require('./JSON/singleDoc'),
12+
singleDocWithBoolean: require('./JSON/singleDocWithBoolean'),
1213
sameSchemaDifferentOrdering: require('./JSON/sameSchemaDifferentOrdering'),
1314
differentSchemas: require('./JSON/differentSchemas')
1415
};

0 commit comments

Comments
 (0)