Skip to content

Commit

Permalink
Add fancy fields option support, zemirco#78
Browse files Browse the repository at this point in the history
{
  fields: [
    // Support label -> simple path
    {
      label: 'some label',
      value: 'path.to.something'
    },
    // Support label -> derived value
    {
      label: 'some label', // Supporting duplicate labels
      value: function(row) {
        return row.path1 + row.path2;
      }
    },
    // Support pathname -> pathvalue (label optional, keeps backwards compatibility)
    'simplepath'
    'path.to.value'
  ]
}
  • Loading branch information
azhang committed Oct 8, 2015
1 parent b127e85 commit 07b2fd3
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 3 deletions.
17 changes: 15 additions & 2 deletions lib/json2csv.js
Expand Up @@ -60,7 +60,13 @@ function checkParams(params, callback) {
return callback(new Error('fieldNames and fields should be of the same length, if fieldNames is provided.'));
}

params.fieldNames = params.fieldNames || params.fields;
// Get fieldNames from fields
params.fieldNames = params.fields.map(function (field, i) {
if (params.fieldNames && typeof field === 'string') {
return params.fieldNames[i];
}
return (typeof field === 'string') ? field : (field.label || field.value);
});

//#check delimiter
params.del = params.del || ',';
Expand Down Expand Up @@ -142,7 +148,14 @@ function createColumnContent(params, str) {
var eol = params.newLine || os.EOL || '\n';

params.fields.forEach(function (fieldElement) {
var val = lodashGet(dataElement, fieldElement, params.defaultValue);
var val;

if (fieldElement && (typeof fieldElement === 'string' || typeof fieldElement.value === 'string')) {
var path = (typeof fieldElement === 'string') ? fieldElement : fieldElement.value;
val = lodashGet(dataElement, path, params.defaultValue);
} else if (fieldElement && typeof fieldElement.value === 'function') {
val = fieldElement.value(dataElement);
}

if (val !== undefined) {
var stringifiedElement = JSON.stringify(val);
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/csv/fancyfields.csv
@@ -0,0 +1,3 @@
"PATH1","PATH1+PATH2","NEST1","bird.nest2"
"hello ","hello world!","chirp","cheep"
"good ","good bye!","meep","meep"
3 changes: 2 additions & 1 deletion test/helpers/load-fixtures.js
Expand Up @@ -16,7 +16,8 @@ var fixtures = [
'fieldNames',
'withSimpleQuotes',
'nested',
'defaultValue'
'defaultValue',
'fancyfields'
];

/*eslint-disable no-console*/
Expand Down
39 changes: 39 additions & 0 deletions test/index.js
Expand Up @@ -269,4 +269,43 @@ async.parallel(loadFixtures(csvFixtures), function (err) {
}
});

test('should process fancy fields option', function (t) {
json2csv({
data: [{
path1: 'hello ',
path2: 'world!',
bird: {
nest1: 'chirp',
nest2: 'cheep'
}
}, {
path1: 'good ',
path2: 'bye!',
bird: {
nest1: 'meep',
nest2: 'meep'
}
}],
fields: [{
label: 'PATH1',
value: 'path1'
}, {
label: 'PATH1+PATH2',
value: function (row) {
return row.path1+row.path2;
}
}, {
label: 'NEST1',
value: 'bird.nest1'
},
'bird.nest2'
],
defaultValue: 'NULL'
}, function (error, csv) {
t.error(error);
t.equal(csv, csvFixtures.fancyfields);
t.end();
});
});

});

0 comments on commit 07b2fd3

Please sign in to comment.