From 0a01470568c5b8d53b3c91009aefaf74ae67d7a5 Mon Sep 17 00:00:00 2001 From: David Raynes Date: Wed, 9 Oct 2019 15:04:09 -0400 Subject: [PATCH] fix: Ensure defaultValue is always applied in the case of nulls (#427) --- lib/utils.js | 2 +- test/CLI.js | 6 +++--- test/JSON2CSVAsyncParser.js | 6 +++--- test/JSON2CSVParser.js | 6 +++--- test/JSON2CSVTransform.js | 6 +++--- test/fixtures/csv/defaultValueEmpty.csv | 2 +- test/fixtures/csv/overriddenDefaultValue.csv | 2 +- 7 files changed, 15 insertions(+), 15 deletions(-) diff --git a/lib/utils.js b/lib/utils.js index de22b554..8d47f93e 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -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) { diff --git a/test/CLI.js b/test/CLI.js index 34ff7fe6..f2081630 100644 --- a/test/CLI.js +++ b/test/CLI.js @@ -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); @@ -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); @@ -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); diff --git a/test/JSON2CSVAsyncParser.js b/test/JSON2CSVAsyncParser.js index ef8f802d..2f2d7516 100644 --- a/test/JSON2CSVAsyncParser.js +++ b/test/JSON2CSVAsyncParser.js @@ -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); @@ -530,7 +530,7 @@ module.exports = (testRunner, jsonFixtures, csvFixtures, inMemoryJsonFixtures) = { value: 'price', default: 1 }, { value: 'color' } ], - defaultValue: '' + defaultValue: '-' }; const parser = new AsyncParser(opts); @@ -556,7 +556,7 @@ module.exports = (testRunner, jsonFixtures, csvFixtures, inMemoryJsonFixtures) = value: row => row.color } ], - defaultValue: '' + defaultValue: '-' }; const parser = new AsyncParser(opts); diff --git a/test/JSON2CSVParser.js b/test/JSON2CSVParser.js index 9e62f0be..c7840a18 100644 --- a/test/JSON2CSVParser.js +++ b/test/JSON2CSVParser.js @@ -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); @@ -430,7 +430,7 @@ module.exports = (testRunner, jsonFixtures, csvFixtures) => { { value: 'price', default: 1 }, { value: 'color' } ], - defaultValue: '' + defaultValue: '-' }; const parser = new Json2csvParser(opts); @@ -456,7 +456,7 @@ module.exports = (testRunner, jsonFixtures, csvFixtures) => { value: row => row.color } ], - defaultValue: '' + defaultValue: '-' }; const parser = new Json2csvParser(opts); diff --git a/test/JSON2CSVTransform.js b/test/JSON2CSVTransform.js index 3a5c23d8..1c4f38d9 100644 --- a/test/JSON2CSVTransform.js +++ b/test/JSON2CSVTransform.js @@ -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); @@ -697,7 +697,7 @@ module.exports = (testRunner, jsonFixtures, csvFixtures, inMemoryJsonFixtures) = { value: 'price', default: 1 }, { value: 'color' } ], - defaultValue: '' + defaultValue: '-' }; const transform = new Json2csvTransform(opts); @@ -732,7 +732,7 @@ module.exports = (testRunner, jsonFixtures, csvFixtures, inMemoryJsonFixtures) = value: row => row.color } ], - defaultValue: '' + defaultValue: '-' }; const transform = new Json2csvTransform(opts); diff --git a/test/fixtures/csv/defaultValueEmpty.csv b/test/fixtures/csv/defaultValueEmpty.csv index 32c080bb..e5da5762 100644 --- a/test/fixtures/csv/defaultValueEmpty.csv +++ b/test/fixtures/csv/defaultValueEmpty.csv @@ -1,5 +1,5 @@ "carModel","price" "Audi",0 "BMW",15000 -"Mercedes", +"Mercedes","-" "Porsche",30000 \ No newline at end of file diff --git a/test/fixtures/csv/overriddenDefaultValue.csv b/test/fixtures/csv/overriddenDefaultValue.csv index b0a71fb8..dbd64e89 100644 --- a/test/fixtures/csv/overriddenDefaultValue.csv +++ b/test/fixtures/csv/overriddenDefaultValue.csv @@ -1,5 +1,5 @@ "carModel","price","color" "Audi",0,"blue" "BMW",1,"red" -"Mercedes",20000,"" +"Mercedes",20000,"-" "Porsche",30000,"green" \ No newline at end of file