Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue with fields.value function not receiving correct fields #353

Merged
merged 1 commit into from
Feb 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}
}];