Skip to content

Commit 240028b

Browse files
committed
Handle empty array case when unwindArrays option is true.
If an empty array was encountered when the unwindArrays option was set to true, the document was ignored and essentially was removed from the CSV output which is not the expected behavior. This commit addresses that bug by ensuring that the "unwound" version of the empty array is still pushed to the accumulator so the document is not excluded. Fixes #151.
1 parent 84c13aa commit 240028b

File tree

6 files changed

+31
-1
lines changed

6 files changed

+31
-1
lines changed

src/utils.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,11 +216,15 @@ function unwindItem(accumulator, item, fieldPath) {
216216
const valueToUnwind = path.evaluatePath(item, fieldPath);
217217
let cloned = deepCopy(item);
218218

219-
if (Array.isArray(valueToUnwind)) {
219+
if (Array.isArray(valueToUnwind) && valueToUnwind.length) {
220220
valueToUnwind.forEach((val) => {
221221
cloned = deepCopy(item);
222222
accumulator.push(path.setPath(cloned, fieldPath, val));
223223
});
224+
} else if (Array.isArray(valueToUnwind) && valueToUnwind.length === 0) {
225+
// Push an empty string so the value is empty since there are no values
226+
path.setPath(cloned, fieldPath, '');
227+
accumulator.push(cloned);
224228
} else {
225229
accumulator.push(cloned);
226230
}

test/config/testCsvFilesList.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const fs = require('fs'),
3030
{key: 'quotedEmptyFieldValue', file: '../data/csv/quotedEmptyFieldValue.csv'},
3131
{key: 'csvEmptyLastValue', file: '../data/csv/csvEmptyLastValue.csv'},
3232
{key: 'unwind', file: '../data/csv/unwind.csv'},
33+
{key: 'unwindEmptyArray', file: '../data/csv/unwindEmptyArray.csv'},
3334
{key: 'unwindWithSpecifiedKeys', file: '../data/csv/unwindWithSpecifiedKeys.csv'},
3435
{key: 'localeFormat', file: '../data/csv/localeFormat.csv'},
3536
{key: 'invalidParsedValues', file: '../data/csv/invalidParsedValues.csv'}

test/config/testJsonFilesList.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ module.exports = {
2323
quotedEmptyFieldValue: require('../data/json/quotedEmptyFieldValue'),
2424
csvEmptyLastValue: require('../data/json/csvEmptyLastValue'),
2525
unwind: require('../data/json/unwind'),
26+
unwindEmptyArray: require('../data/json/unwindEmptyArray'),
2627
localeFormat: require('../data/json/localeFormat'),
2728
invalidParsedValues: require('../data/json/invalidParsedValues')
2829
};

test/data/csv/unwindEmptyArray.csv

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
_id.$oid,data.category,data.options
2+
5cf7ca3616c91100018844af,Computers,
3+
5cf7ca3616c91100018844bf,Cars,Supercharger
4+
5cf7ca3616c91100018844bf,Cars,Turbocharger
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[
2+
{
3+
"_id": {"$oid": "5cf7ca3616c91100018844af"},
4+
"data": {"category": "Computers", "options": []}
5+
},
6+
{
7+
"_id": {"$oid": "5cf7ca3616c91100018844bf"},
8+
"data": {"category": "Cars", "options": ["Supercharger", "Turbocharger"]}
9+
}
10+
]

test/json2csv.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,16 @@ function runTests(jsonTestData, csvTestData) {
415415
});
416416
});
417417

418+
it('should handle unwinding empty array values when specified', (done) => {
419+
converter.json2csv(jsonTestData.unwindEmptyArray, (err, csv) => {
420+
if (err) done(err);
421+
csv.should.equal(csvTestData.unwindEmptyArray);
422+
done();
423+
}, {
424+
unwindArrays: true
425+
});
426+
});
427+
418428
it('should unwind array values when specified', (done) => {
419429
converter.json2csv(jsonTestData.unwind, (err, csv) => {
420430
if (err) done(err);

0 commit comments

Comments
 (0)