Skip to content

Commit

Permalink
fix: Ensure defaultValue is always applied in the case of nulls (#427)
Browse files Browse the repository at this point in the history
  • Loading branch information
rayners authored and knownasilya committed Oct 9, 2019
1 parent d891c64 commit 0a01470
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 15 deletions.
2 changes: 1 addition & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

function getProp(obj, path, defaultValue) {
return obj[path] === undefined ? defaultValue : obj[path];
return (obj[path] === undefined || obj[path] === null) ? defaultValue : obj[path];
}

function setProp(obj, path, value) {
Expand Down
6 changes: 3 additions & 3 deletions test/CLI.js
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ module.exports = (testRunner, jsonFixtures, csvFixtures) => {
// Default value

testRunner.add('should output the default value as set in \'defaultValue\'', (t) => {
const opts = ' --fields carModel,price --default-value ""';
const opts = ' --fields carModel,price --default-value "-"';

child_process.exec(cli + '-i ' + getFixturePath('/json/defaultValueEmpty.json') + opts, (err, stdout, stderr) => {
t.notOk(stderr);
Expand All @@ -372,7 +372,7 @@ module.exports = (testRunner, jsonFixtures, csvFixtures) => {

testRunner.add('should override \'options.defaultValue\' with \'field.defaultValue\'', (t) => {
const opts = ' --fields-config ' + getFixturePath('/fields/overriddenDefaultValue.json')
+ ' --default-value ""';
+ ' --default-value "-"';

child_process.exec(cli + '-i ' + getFixturePath('/json/overriddenDefaultValue.json') + opts, (err, stdout, stderr) => {
t.notOk(stderr);
Expand All @@ -384,7 +384,7 @@ module.exports = (testRunner, jsonFixtures, csvFixtures) => {

testRunner.add('should use \'options.defaultValue\' when no \'field.defaultValue\'', (t) => {
const opts = ' --fields-config ' + getFixturePath('/fields/overriddenDefaultValue2.js')
+ ' --default-value ""';
+ ' --default-value "-"';

child_process.exec(cli + '-i ' + getFixturePath('/json/overriddenDefaultValue.json') + opts, (err, stdout, stderr) => {
t.notOk(stderr);
Expand Down
6 changes: 3 additions & 3 deletions test/JSON2CSVAsyncParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ module.exports = (testRunner, jsonFixtures, csvFixtures, inMemoryJsonFixtures) =
testRunner.add('should output the default value as set in \'defaultValue\'', (t) => {
const opts = {
fields: ['carModel', 'price'],
defaultValue: ''
defaultValue: '-'
};

const parser = new AsyncParser(opts);
Expand All @@ -530,7 +530,7 @@ module.exports = (testRunner, jsonFixtures, csvFixtures, inMemoryJsonFixtures) =
{ value: 'price', default: 1 },
{ value: 'color' }
],
defaultValue: ''
defaultValue: '-'
};

const parser = new AsyncParser(opts);
Expand All @@ -556,7 +556,7 @@ module.exports = (testRunner, jsonFixtures, csvFixtures, inMemoryJsonFixtures) =
value: row => row.color
}
],
defaultValue: ''
defaultValue: '-'
};

const parser = new AsyncParser(opts);
Expand Down
6 changes: 3 additions & 3 deletions test/JSON2CSVParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ module.exports = (testRunner, jsonFixtures, csvFixtures) => {
testRunner.add('should output the default value as set in \'defaultValue\'', (t) => {
const opts = {
fields: ['carModel', 'price'],
defaultValue: ''
defaultValue: '-'
};

const parser = new Json2csvParser(opts);
Expand All @@ -430,7 +430,7 @@ module.exports = (testRunner, jsonFixtures, csvFixtures) => {
{ value: 'price', default: 1 },
{ value: 'color' }
],
defaultValue: ''
defaultValue: '-'
};

const parser = new Json2csvParser(opts);
Expand All @@ -456,7 +456,7 @@ module.exports = (testRunner, jsonFixtures, csvFixtures) => {
value: row => row.color
}
],
defaultValue: ''
defaultValue: '-'
};

const parser = new Json2csvParser(opts);
Expand Down
6 changes: 3 additions & 3 deletions test/JSON2CSVTransform.js
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,7 @@ module.exports = (testRunner, jsonFixtures, csvFixtures, inMemoryJsonFixtures) =
testRunner.add('should output the default value as set in \'defaultValue\'', (t) => {
const opts = {
fields: ['carModel', 'price'],
defaultValue: ''
defaultValue: '-'
};

const transform = new Json2csvTransform(opts);
Expand All @@ -697,7 +697,7 @@ module.exports = (testRunner, jsonFixtures, csvFixtures, inMemoryJsonFixtures) =
{ value: 'price', default: 1 },
{ value: 'color' }
],
defaultValue: ''
defaultValue: '-'
};

const transform = new Json2csvTransform(opts);
Expand Down Expand Up @@ -732,7 +732,7 @@ module.exports = (testRunner, jsonFixtures, csvFixtures, inMemoryJsonFixtures) =
value: row => row.color
}
],
defaultValue: ''
defaultValue: '-'
};

const transform = new Json2csvTransform(opts);
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/csv/defaultValueEmpty.csv
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"carModel","price"
"Audi",0
"BMW",15000
"Mercedes",
"Mercedes","-"
"Porsche",30000
2 changes: 1 addition & 1 deletion test/fixtures/csv/overriddenDefaultValue.csv
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"carModel","price","color"
"Audi",0,"blue"
"BMW",1,"red"
"Mercedes",20000,""
"Mercedes",20000,"-"
"Porsche",30000,"green"

0 comments on commit 0a01470

Please sign in to comment.