Skip to content

Commit

Permalink
Merge pull request #90 from orzarchi/master
Browse files Browse the repository at this point in the history
Will now apply configured 'default value' to null data fields
  • Loading branch information
Ilya Radchenko committed Jan 2, 2016
2 parents 334e433 + 9bcf0fd commit e4faf8d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
6 changes: 5 additions & 1 deletion lib/json2csv.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,11 @@ function createColumnContent(params, str) {

if (fieldElement && (typeof fieldElement === 'string' || typeof fieldElement.value === 'string')) {
var path = (typeof fieldElement === 'string') ? fieldElement : fieldElement.value;
val = lodashGet(dataElement, path, fieldElement.default || params.defaultValue);
var defaultValue = fieldElement.default || params.defaultValue;
val = lodashGet(dataElement, path, defaultValue);
if (val === null && defaultValue !== undefined){
val = defaultValue;
}
} else if (fieldElement && typeof fieldElement.value === 'function') {
val = fieldElement.value(dataElement) || fieldElement.default;
}
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/csv/defaultValueEmpty.csv
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
"Audi",0
"BMW",15000
"Mercedes",""
"Porsche",30000
"Porsche",30000

0 comments on commit e4faf8d

Please sign in to comment.