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

Added flag to signal if resulting function value should be stringified or not #192

Merged
merged 2 commits into from
Jul 30, 2017
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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ try {
// data = full data object
return row.path1 + row.path2;
},
default: 'NULL' // default if value function returns null or undefined
default: 'NULL', // default if value function returns null or undefined
stringify: true // If value is function use this flag to signal if resulting string will be quoted (stringified) or not (optional, default: true)
},

// Support pathname -> pathvalue
Expand Down
9 changes: 8 additions & 1 deletion lib/json2csv.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ function createColumnContent(params, str) {
params.fields.forEach(function (fieldElement) {
var val;
var defaultValue = params.defaultValue;
var stringify = true;
if (typeof fieldElement === 'object' && 'default' in fieldElement) {
defaultValue = fieldElement.default;
}
Expand All @@ -228,6 +229,9 @@ function createColumnContent(params, str) {
default: fieldElement.default
};
val = fieldElement.value(dataElement, field, params.data);
if (fieldElement.stringify !== undefined) {
stringify = fieldElement.stringify;
}
}

if (val === null || val === undefined){
Expand All @@ -241,7 +245,10 @@ function createColumnContent(params, str) {
.replace(/\r/g, '\u2029');
}

var stringifiedElement = JSON.stringify(val);
var stringifiedElement = val;
if (stringify) {
stringifiedElement = JSON.stringify(val);
}

if (params.preserveNewLinesInValues && typeof val === 'string') {
stringifiedElement = stringifiedElement
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/csv/functionNoStringify.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"Value1"
"abc"
1234
3 changes: 3 additions & 0 deletions test/fixtures/csv/functionStringifyByDefault.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"Value1"
"abc"
"1234"
2 changes: 2 additions & 0 deletions test/helpers/load-fixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ var fixtures = [
'embeddedjson',
'flattenedEmbeddedJson',
'fancyfields',
'functionStringifyByDefault',
'functionNoStringify',
'trailingBackslash',
'excelStrings',
'overriddenDefaultValue',
Expand Down
41 changes: 41 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,47 @@ async.parallel(loadFixtures(csvFixtures), function (err) {
});
});

test('function value should stringify results by default', function (t) {
json2csv({
data: [{
value1: 'abc'
}, {
value1: '1234'
}],
fields: [{
label: 'Value1',
value: function (row) {
return row.value1.toLocaleString();
}
}]
}, function (error, csv) {
t.error(error);
t.equal(csv, csvFixtures.functionStringifyByDefault);
t.end();
});
});

test('function value do not stringify', function (t) {
json2csv({
data: [{
value1: '"abc"'
}, {
value1: '1234'
}],
fields: [{
label: 'Value1',
value: function (row) {
return row.value1.toLocaleString();
},
stringify: false
}]
}, function (error, csv) {
t.error(error);
t.equal(csv, csvFixtures.functionNoStringify);
t.end();
});
});

test('should parse JSON values with trailing backslashes', function (t) {
json2csv({
data: jsonTrailingBackslash,
Expand Down