Skip to content

Commit

Permalink
fix: issue with fields.value function not receiving correct fields (#353
Browse files Browse the repository at this point in the history
)
  • Loading branch information
juanjoDiaz authored and knownasilya committed Feb 7, 2019
1 parent 9b243d1 commit 851c02f
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 3 deletions.
7 changes: 4 additions & 3 deletions lib/JSON2CSVBase.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions test/CLI.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down
19 changes: 19 additions & 0 deletions test/JSON2CSVParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: [{
Expand Down
25 changes: 25 additions & 0 deletions test/JSON2CSVTransform.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: [{
Expand Down
9 changes: 9 additions & 0 deletions test/fixtures/fields/functionWithCheck.js
Original file line number Diff line number Diff line change
@@ -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();
}
}];

0 comments on commit 851c02f

Please sign in to comment.