Skip to content

Commit 5ebb32a

Browse files
authored
Add logic to wrap booleans when specified. (#189)
In order to support the use case of keeping Booleans as String values that can easily be converted back with csv2json, this commit adds a new option `wrapBooleans` which will allow users a way to make this possible Fixes #188
1 parent e72fa24 commit 5ebb32a

File tree

4 files changed

+25
-2
lines changed

4 files changed

+25
-2
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ Looking for examples? Check out the Wiki: [json-2-csv Wiki](https://github.com/m
138138
* `useLocaleFormat` - Boolean - Should values be converted to a locale specific string?
139139
* Default: `false`
140140
* Note: If selected, values will be converted using `toLocaleString()` rather than `toString()`
141+
* `wrapBooleans` - Boolean - Should boolean values be wrapped in wrap delimiters to prevent Excel from converting them to Excel's TRUE/FALSE Boolean values.
142+
* Default: `false`
141143

142144

143145
For examples, please refer to the [json2csv API Documentation (Link)](https://github.com/mrodrig/json-2-csv/wiki/json2csv-Documentation)

lib/constants.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535
"unwindArrays": false,
3636
"useDateIso8601Format": false,
3737
"useLocaleFormat": false,
38-
"parseValue": null
38+
"parseValue": null,
39+
"wrapBooleans": false
3940
},
4041

4142
"values" : {

lib/json2csv.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,8 @@ const Json2Csv = function(options) {
362362
// then enclose it in quotation marks (wrap delimiter)
363363
if (fieldValue.includes(options.delimiter.field) ||
364364
fieldValue.includes(options.delimiter.wrap) ||
365-
fieldValue.match(crlfSearchRegex)) {
365+
fieldValue.match(crlfSearchRegex) ||
366+
options.wrapBooleans && (fieldValue === 'true' || fieldValue === 'false')) {
366367
// wrap the field's value in a wrap delimiter (quotation marks by default)
367368
fieldValue = wrapDelimiter + fieldValue + wrapDelimiter;
368369
}

test/json2csv.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,25 @@ function runTests(jsonTestData, csvTestData) {
618618
parseValue: () => 'Parsed Value'
619619
});
620620
});
621+
622+
it('should wrap boolean values in wrap delimiters, if specified', (done) => {
623+
converter.json2csv(jsonTestData.emptyFieldValues, (err, csv) => {
624+
if (err) done(err);
625+
626+
// Replace raw boolean values with quoted versions
627+
let expectedCsv = csvTestData.emptyFieldValues
628+
.replace(',"",', ',,')
629+
.replace(/\n"",/g, '\n,')
630+
.replace(/false/g, '"false"')
631+
.replace(/true/g, '"true"');
632+
633+
csv.should.equal(expectedCsv);
634+
done();
635+
}, {
636+
emptyFieldValue: '',
637+
wrapBooleans: true
638+
});
639+
});
621640
});
622641
});
623642

0 commit comments

Comments
 (0)