From f90f5420da80e67e39e8ca22813a17702f5257d1 Mon Sep 17 00:00:00 2001 From: Howard Dean Watts Date: Mon, 12 Oct 2020 12:14:28 -0400 Subject: [PATCH 1/4] Add a finalPass to unwindRecordsIfNecessary --- src/json2csv.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/json2csv.js b/src/json2csv.js index 08c0dd4..c58b61e 100755 --- a/src/json2csv.js +++ b/src/json2csv.js @@ -159,7 +159,7 @@ const Json2Csv = function(options) { * @param params {Object} * @returns {Promise} */ - function unwindRecordsIfNecessary(params) { + function unwindRecordsIfNecessary(params, finalPass = false) { if (options.unwindArrays) { const originalRecordsLength = params.records.length; @@ -178,6 +178,12 @@ const Json2Csv = function(options) { } // Otherwise, we didn't unwind any additional arrays, so continue... + // Run a final time in case the earlier unwinding exposed additional + // arrays to unwind... + if (!finalPass) { + return unwindRecordsIfNecessary(params, true) + } + // If keys were provided, set the headerFields to the provided keys: if (options.keys) { params.headerFields = options.keys; From 42332e3a147c692699ecdfd83ac0571d8f419184 Mon Sep 17 00:00:00 2001 From: Dean Watts Date: Mon, 12 Oct 2020 12:26:13 -0400 Subject: [PATCH 2/4] add test --- test/config/testCsvFilesList.js | 1 + test/config/testJsonFilesList.js | 1 + test/data/csv/arraySingleArray.csv | 2 ++ test/data/json/arraySingleArray.json | 16 ++++++++++++++++ 4 files changed, 20 insertions(+) create mode 100644 test/data/csv/arraySingleArray.csv create mode 100644 test/data/json/arraySingleArray.json diff --git a/test/config/testCsvFilesList.js b/test/config/testCsvFilesList.js index 87fd153..3c6c5fe 100644 --- a/test/config/testCsvFilesList.js +++ b/test/config/testCsvFilesList.js @@ -7,6 +7,7 @@ const fs = require('fs'), {key: 'array', file: '../data/csv/array.csv'}, {key: 'arrayObjects', file: '../data/csv/arrayObjects.csv'}, {key: 'arrayMixedObjNonObj', file: '../data/csv/arrayMixedObjNonObj.csv'}, + {key: 'arraySingleArray', file: '../data/csv/arraySingleArray.csv'}, {key: 'date', file: '../data/csv/date.csv'}, {key: 'null', file: '../data/csv/null.csv'}, {key: 'undefined', file: '../data/csv/undefined.csv'}, diff --git a/test/config/testJsonFilesList.js b/test/config/testJsonFilesList.js index 49308bf..7a577b6 100644 --- a/test/config/testJsonFilesList.js +++ b/test/config/testJsonFilesList.js @@ -4,6 +4,7 @@ module.exports = { array: require('../data/json/array'), arrayObjects: require('../data/json/arrayObjects'), arrayMixedObjNonObj: require('../data/json/arrayMixedObjNonObj'), + arraySingleArray: require('../data/json/arraySingleArray'), date: require('../data/json/date'), null: require('../data/json/null'), undefined: require('../data/json/undefined'), diff --git a/test/data/csv/arraySingleArray.csv b/test/data/csv/arraySingleArray.csv new file mode 100644 index 0000000..a41adb4 --- /dev/null +++ b/test/data/csv/arraySingleArray.csv @@ -0,0 +1,2 @@ +list.name,list.features.name,list.features.price.currency,list.features.price.amount +Shopping List,Television,USD,100 \ No newline at end of file diff --git a/test/data/json/arraySingleArray.json b/test/data/json/arraySingleArray.json new file mode 100644 index 0000000..7bb3df3 --- /dev/null +++ b/test/data/json/arraySingleArray.json @@ -0,0 +1,16 @@ +{ + "list": [ + { + "name": "Shopping List", + "features": [ + { + "name": "Television", + "price": { + "currency": "USD", + "amount": 100 + } + } + ] + } + ] +} \ No newline at end of file From 06dcd620f15c06b6b3f7ac253accab3578cd5bdd Mon Sep 17 00:00:00 2001 From: Dean Watts Date: Mon, 12 Oct 2020 12:34:45 -0400 Subject: [PATCH 3/4] add test --- test/json2csv.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/json2csv.js b/test/json2csv.js index c7ceb35..4516db0 100644 --- a/test/json2csv.js +++ b/test/json2csv.js @@ -435,6 +435,16 @@ function runTests(jsonTestData, csvTestData) { }); }); + it('should unwind nested array values when the earlier array has a length of 1', (done) => { + converter.json2csv(jsonTestData.arraySingleArray, (err, csv) => { + if (err) done(err); + csv.should.equal(csvTestData.arraySingleArray); + done(); + }, { + unwindArrays: true + }); + }); + it('should unwind array values when specified - with keys specified', (done) => { converter.json2csv(jsonTestData.unwind, (err, csv) => { if (err) done(err); From a792934621114790997b3b447fe94b482b87ff96 Mon Sep 17 00:00:00 2001 From: Dean Watts Date: Tue, 13 Oct 2020 12:15:50 -0400 Subject: [PATCH 4/4] appease travisci --- src/json2csv.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/json2csv.js b/src/json2csv.js index c58b61e..8785023 100755 --- a/src/json2csv.js +++ b/src/json2csv.js @@ -181,9 +181,9 @@ const Json2Csv = function(options) { // Run a final time in case the earlier unwinding exposed additional // arrays to unwind... if (!finalPass) { - return unwindRecordsIfNecessary(params, true) + return unwindRecordsIfNecessary(params, true); } - + // If keys were provided, set the headerFields to the provided keys: if (options.keys) { params.headerFields = options.keys;