From 24f33b906dc17e42d041a4aac2f47377848ea3e0 Mon Sep 17 00:00:00 2001 From: Juanjo Diaz Date: Wed, 6 Feb 2019 22:51:05 +0200 Subject: [PATCH] Fix issue with fields.value function not receiving correct fields --- lib/JSON2CSVBase.js | 7 ++++--- test/CLI.js | 11 ++++++++++ test/JSON2CSVParser.js | 19 +++++++++++++++++ test/JSON2CSVTransform.js | 25 +++++++++++++++++++++++ test/fixtures/fields/functionWithCheck.js | 9 ++++++++ 5 files changed, 68 insertions(+), 3 deletions(-) create mode 100644 test/fixtures/fields/functionWithCheck.js diff --git a/lib/JSON2CSVBase.js b/lib/JSON2CSVBase.js index 1ecf600d..2a6a25c5 100644 --- a/lib/JSON2CSVBase.js +++ b/lib/JSON2CSVBase.js @@ -67,10 +67,11 @@ class JSON2CSVBase { } if (typeof fieldInfo.value === 'function') { + const label = fieldInfo.label || fieldInfo.value; + const field = { label, default: defaultValue }; return { - label: fieldInfo.label || fieldInfo.value, - value: row => { - const field = { label: this.label, default: defaultValue }; + label, + value(row) { const value = fieldInfo.value(row, field); return (value === null || value === undefined) ? defaultValue diff --git a/test/CLI.js b/test/CLI.js index 36cdf8d7..117acb7a 100644 --- a/test/CLI.js +++ b/test/CLI.js @@ -242,6 +242,17 @@ module.exports = (testRunner, jsonFixtures, csvFixtures) => { }); }); + testRunner.add('field.value function should receive a valid field object', (t) => { + const opts = ' --fields-config ' + getFixturePath('/fields/functionWithCheck.js'); + + child_process.exec(cli + '-i ' + getFixturePath('/json/functionStringifyByDefault.json') + opts, (err, stdout, stderr) => { + t.notOk(stderr); + const csv = stdout; + t.equal(csv, csvFixtures.functionStringifyByDefault); + t.end(); + }); + }); + testRunner.add('field.value function should stringify results by default', (t) => { const opts = ' --fields-config ' + getFixturePath('/fields/functionStringifyByDefault.js'); diff --git a/test/JSON2CSVParser.js b/test/JSON2CSVParser.js index e6958de2..11882068 100644 --- a/test/JSON2CSVParser.js +++ b/test/JSON2CSVParser.js @@ -244,6 +244,25 @@ module.exports = (testRunner, jsonFixtures, csvFixtures) => { t.end(); }); + testRunner.add('field.value function should receive a valid field object', (t) => { + const opts = { + fields: [{ + label: 'Value1', + default: 'default value', + value: (row, field) => { + t.deepEqual(field, { label: 'Value1', default: 'default value' }); + return row.value1.toLocaleString(); + } + }] + }; + + const parser = new Json2csvParser(opts); + const csv = parser.parse(jsonFixtures.functionStringifyByDefault); + + t.equal(csv, csvFixtures.functionStringifyByDefault); + t.end(); + }); + testRunner.add('field.value function should stringify results by default', (t) => { const opts = { fields: [{ diff --git a/test/JSON2CSVTransform.js b/test/JSON2CSVTransform.js index 36bed337..8818d860 100644 --- a/test/JSON2CSVTransform.js +++ b/test/JSON2CSVTransform.js @@ -374,6 +374,31 @@ module.exports = (testRunner, jsonFixtures, csvFixtures, inMemoryJsonFixtures) = .on('error', err => t.notOk(true, err.message)); }); + testRunner.add('field.value function should receive a valid field object', (t) => { + const opts = { + fields: [{ + label: 'Value1', + default: 'default value', + value: (row, field) => { + t.deepEqual(field, { label: 'Value1', default: 'default value' }); + return row.value1.toLocaleString(); + } + }] + }; + + const transform = new Json2csvTransform(opts); + const processor = jsonFixtures.functionStringifyByDefault().pipe(transform); + + let csv = ''; + processor + .on('data', chunk => (csv += chunk.toString())) + .on('end', () => { + t.equal(csv, csvFixtures.functionStringifyByDefault); + t.end(); + }) + .on('error', err => t.notOk(true, err.message)); + }); + testRunner.add('field.value function should stringify results by default', (t) => { const opts = { fields: [{ diff --git a/test/fixtures/fields/functionWithCheck.js b/test/fixtures/fields/functionWithCheck.js new file mode 100644 index 00000000..36bbcfb7 --- /dev/null +++ b/test/fixtures/fields/functionWithCheck.js @@ -0,0 +1,9 @@ +module.exports = [{ + label: 'Value1', + value: (row, field) => { + if(field.label !== 'Value1' && field.default !== 'default value') { + throw new Error(`Expected ${JSON.stringify(field)} to equals ${JSON.stringify({ label: 'Value1', default: 'default value' })}.`); + } + return row.value1.toLocaleString(); + } +}]; \ No newline at end of file