Skip to content

Commit 0320152

Browse files
authored
Fix for single length arrays not unwinding nested arrays (#165)
* Add a finalPass to unwindRecordsIfNecessary
1 parent 05cb677 commit 0320152

File tree

6 files changed

+37
-1
lines changed

6 files changed

+37
-1
lines changed

src/json2csv.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ const Json2Csv = function(options) {
159159
* @param params {Object}
160160
* @returns {Promise}
161161
*/
162-
function unwindRecordsIfNecessary(params) {
162+
function unwindRecordsIfNecessary(params, finalPass = false) {
163163
if (options.unwindArrays) {
164164
const originalRecordsLength = params.records.length;
165165

@@ -178,6 +178,12 @@ const Json2Csv = function(options) {
178178
}
179179
// Otherwise, we didn't unwind any additional arrays, so continue...
180180

181+
// Run a final time in case the earlier unwinding exposed additional
182+
// arrays to unwind...
183+
if (!finalPass) {
184+
return unwindRecordsIfNecessary(params, true);
185+
}
186+
181187
// If keys were provided, set the headerFields to the provided keys:
182188
if (options.keys) {
183189
params.headerFields = options.keys;

test/config/testCsvFilesList.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const fs = require('fs'),
77
{key: 'array', file: '../data/csv/array.csv'},
88
{key: 'arrayObjects', file: '../data/csv/arrayObjects.csv'},
99
{key: 'arrayMixedObjNonObj', file: '../data/csv/arrayMixedObjNonObj.csv'},
10+
{key: 'arraySingleArray', file: '../data/csv/arraySingleArray.csv'},
1011
{key: 'date', file: '../data/csv/date.csv'},
1112
{key: 'null', file: '../data/csv/null.csv'},
1213
{key: 'undefined', file: '../data/csv/undefined.csv'},

test/config/testJsonFilesList.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ module.exports = {
44
array: require('../data/json/array'),
55
arrayObjects: require('../data/json/arrayObjects'),
66
arrayMixedObjNonObj: require('../data/json/arrayMixedObjNonObj'),
7+
arraySingleArray: require('../data/json/arraySingleArray'),
78
date: require('../data/json/date'),
89
null: require('../data/json/null'),
910
undefined: require('../data/json/undefined'),

test/data/csv/arraySingleArray.csv

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
list.name,list.features.name,list.features.price.currency,list.features.price.amount
2+
Shopping List,Television,USD,100
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"list": [
3+
{
4+
"name": "Shopping List",
5+
"features": [
6+
{
7+
"name": "Television",
8+
"price": {
9+
"currency": "USD",
10+
"amount": 100
11+
}
12+
}
13+
]
14+
}
15+
]
16+
}

test/json2csv.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,16 @@ function runTests(jsonTestData, csvTestData) {
435435
});
436436
});
437437

438+
it('should unwind nested array values when the earlier array has a length of 1', (done) => {
439+
converter.json2csv(jsonTestData.arraySingleArray, (err, csv) => {
440+
if (err) done(err);
441+
csv.should.equal(csvTestData.arraySingleArray);
442+
done();
443+
}, {
444+
unwindArrays: true
445+
});
446+
});
447+
438448
it('should unwind array values when specified - with keys specified', (done) => {
439449
converter.json2csv(jsonTestData.unwind, (err, csv) => {
440450
if (err) done(err);

0 commit comments

Comments
 (0)