Skip to content

Commit

Permalink
feat: Added flag to signal if resulting function value should be stri…
Browse files Browse the repository at this point in the history
…ngified or not (#192)

* Added flag to signal if resulting function value should be stringified or not. By default it is set to true to keep backward compatibility

* Added tests
  • Loading branch information
enver authored and knownasilya committed Jul 30, 2017
1 parent e706c25 commit aaa6b05
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 2 deletions.
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

0 comments on commit aaa6b05

Please sign in to comment.