Skip to content

Commit 3180ddb

Browse files
committed
Merge pull request #34 from litixsoft/convert_boolean
fix converting of boolean values in json-2-csv
2 parents 9c6c0fd + 2807432 commit 3180ddb

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
@@ -126,6 +126,8 @@ var convertField = function (value) {
126126
return options.DELIMITER.WRAP + convertData(value, _.keys(value)) + options.DELIMITER.WRAP; // Push the recursively generated CSV
127127
} else if (_.isNumber(value)) { // If we have a number (avoids 0 being converted to '')
128128
return options.DELIMITER.WRAP + value.toString() + options.DELIMITER.WRAP;
129+
} else if (_.isBoolean(value)) { // If we have a boolean (avoids false being converted to '')
130+
return options.DELIMITER.WRAP + value.toString() + options.DELIMITER.WRAP;
129131
}
130132
return options.DELIMITER.WRAP + (value ? value.toString() : '') + options.DELIMITER.WRAP; // Otherwise push the current value
131133
};
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
@@ -11,6 +11,7 @@
1111
{"key": "regularJson", "file": "test/CSV/unQuoted/regularJson.csv"},
1212
{"key": "regularJsonSorted", "file": "test/CSV/unQuoted/regularJsonSorted.csv"},
1313
{"key": "singleDoc", "file": "test/CSV/unQuoted/singleDoc.csv"},
14+
{"key": "singleDocWithBoolean", "file": "test/CSV/unQuoted/singleDocWithBoolean.csv"},
1415
{"key": "differentSchemas", "file": "test/CSV/unQuoted/differentSchemas.csv"}
1516
]
1617
},

test/testJson2Csv.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,16 @@ var json2csvTests = function () {
8686
});
8787
});
8888

89+
it('should parse a single JSON document with Boolean to CSV', function (done) {
90+
converter.json2csv(jsonTestData.singleDocWithBoolean, function (err, csv) {
91+
if (err) { throw err; }
92+
true.should.equal(_.isEqual(err, null));
93+
csv.should.equal(csvTestData.unQuoted.singleDocWithBoolean);
94+
csv.split(options.EOL).length.should.equal(3);
95+
done();
96+
});
97+
});
98+
8999
it('should parse an array of JSON documents to CSV', function (done) {
90100
converter.json2csv(jsonTestData.arrayValue, function (err, csv) {
91101
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)