Skip to content

Commit

Permalink
fix boolean false value to empty string
Browse files Browse the repository at this point in the history
  • Loading branch information
solee0524 committed Jun 10, 2016
1 parent 5e4e85b commit 34fc9e1
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ module.exports = function *(options) {
var tmpField = field.split('.');
var tmpValue = '';
if (tmpField.length === 1) {
tmpValue = item[field] || '';
tmpValue = item[field] === undefined || item[field] === null ? '' : item[field];
} else {
tmpValue = item;
var n = 0;
while (n < tmpField.length) {
tmpValue = tmpValue[tmpField[n]] || '';
tmpValue = tmpValue[tmpField[n]] === undefined || tmpValue[tmpField[n]] === null ? '' : tmpValue[tmpField[n]];
if (!tmpValue) {
break;
}
Expand Down
41 changes: 41 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,46 @@ describe('json to csv test with incorrect input', function () {
var csv = yield json2csv(options);
csv.should.to.have.string(',,,\n');
});
});

describe('validate boolean inpunt', function () {
it('mix boolean value', function * () {
var data = [{
hello: true,
world: false
}];
var options = {
data: data,
fields: ['hello', 'world']
};
var csv = yield json2csv(options);
csv.should.to.have.string('true,false');
});

it('all true boolean value', function * () {
var data = [{
hello: true,
world: true
}];
var options = {
data: data,
fields: ['hello', 'world']
};
var csv = yield json2csv(options);
csv.should.to.have.string('true,true');
});

it('all false boolean value', function * () {
var data = [{
hello: false,
world: false
}];
var options = {
data: data,
fields: ['hello', 'world']
};
var csv = yield json2csv(options);
csv.should.to.have.string('false,false');
});

});

0 comments on commit 34fc9e1

Please sign in to comment.