Skip to content

Commit 485ea77

Browse files
authored
fix: unwind transform issue with nested arrays (#549)
1 parent f0ca2c2 commit 485ea77

File tree

7 files changed

+21
-16
lines changed

7 files changed

+21
-16
lines changed

lib/utils.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ function getProp(obj, path, defaultValue) {
77
function setProp(obj, path, value) {
88
const pathArray = Array.isArray(path) ? path : path.split('.');
99
const [key, ...restPath] = pathArray;
10-
const newValue = pathArray.length > 1 ? setProp(obj[key] || {}, restPath, value) : value;
11-
return Object.assign({}, obj, { [key]: newValue });
10+
return {
11+
...obj,
12+
[key]: pathArray.length > 1 ? setProp(obj[key] || {}, restPath, value) : value
13+
};
1214
}
1315

1416
function unsetProp(obj, path) {
@@ -27,7 +29,11 @@ function unsetProp(obj, path) {
2729
.reduce((acc, prop) => Object.assign(acc, { [prop]: obj[prop] }), {});
2830
}
2931

30-
return unsetProp(obj[key], restPath);
32+
return Object.keys(obj)
33+
.reduce((acc, prop) => ({
34+
...acc,
35+
[prop]: prop !== key ? obj[prop] : unsetProp(obj[key], restPath),
36+
}), {});
3137
}
3238

3339
function flattenReducer(acc, arr) {

test/CLI.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -843,7 +843,7 @@ module.exports = (testRunner, jsonFixtures, csvFixtures) => {
843843
});
844844

845845
testRunner.add('should unwind complex objects using the unwind transform', (t) => {
846-
const opts = '--fields carModel,price,extras.items.name,extras.items.items.position,extras.items.items.color,extras.items.items,name,color,extras.items.color'
846+
const opts = '--fields carModel,price,extras.items.name,extras.items.items.position,extras.items.items.color,extras.items.color'
847847
+ ' --unwind extras.items,extras.items.items --flatten-objects --flatten-arrays';
848848

849849
exec(`${cli} -i "${getFixturePath('/json/unwindComplexObject.json')}" ${opts}`, (err, stdout, stderr) => {

test/JSON2CSVAsyncParser.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1278,7 +1278,7 @@ module.exports = (testRunner, jsonFixtures, csvFixtures, inMemoryJsonFixtures) =
12781278

12791279
testRunner.add('should unwind complex objects using the unwind transform', async (t) => {
12801280
const opts = {
1281-
fields: ["carModel", "price", "extras.items.name", "extras.items.items.position", "extras.items.items.color", "extras.items.items", "name", "color", "extras.items.color"],
1281+
fields: ["carModel", "price", "extras.items.name", "extras.items.items.position", "extras.items.items.color", "extras.items.color"],
12821282
transforms: [unwind({ paths: ['extras.items', 'extras.items.items'] }), flatten()],
12831283
};
12841284

test/JSON2CSVParser.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,8 @@ module.exports = (testRunner, jsonFixtures, csvFixtures) => {
2525
t.end();
2626
});
2727

28-
testRunner.add('should not modify the JSON object passed passed', (t) => {
28+
testRunner.add('should not modify the JSON object passed', (t) => {
2929
const opts = {
30-
fields: ["carModel", "price", "extras.items.name", "extras.items.items.position", "extras.items.items.color", "extras.items.items", "name", "color", "extras.items.color"],
3130
transforms: [unwind({ paths: ['extras.items', 'extras.items.items'] }), flatten()],
3231
};
3332
const parser = new Json2csvParser(opts);

test/JSON2CSVTransform.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1317,9 +1317,9 @@ module.exports = (testRunner, jsonFixtures, csvFixtures, inMemoryJsonFixtures) =
13171317
});
13181318

13191319

1320-
testRunner.add('should unwind complex objects using the unwind transform', async (t) => {
1320+
testRunner.add('should unwind complex objects using the unwind transform', (t) => {
13211321
const opts = {
1322-
fields: ["carModel", "price", "extras.items.name", "extras.items.items.position", "extras.items.items.color", "extras.items.items", "name", "color", "extras.items.color"],
1322+
fields: ["carModel", "price", "extras.items.name", "extras.items.items.position", "extras.items.items.color", "extras.items.color"],
13231323
transforms: [unwind({ paths: ['extras.items', 'extras.items.items'] }), flatten()],
13241324
};
13251325

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
"carModel","price","extras.items.name","extras.items.items.position","extras.items.items.color","extras.items.items","name","color","extras.items.color"
2-
"Porsche",30000,"airbag","left","white",,,,
3-
"Porsche",30000,"airbag","right","gray",,,,
4-
"Porsche",30000,"dashboard",,,"none",,,
5-
,,,,,,"airbag","white",
6-
"BMW",15000,"dashboard",,,,,,"black"
1+
"carModel","price","extras.items.name","extras.items.items.position","extras.items.items.color","extras.items.color"
2+
"Porsche",30000,"airbag","left","white",
3+
"Porsche",30000,"airbag","right","gray",
4+
"Porsche",30000,"dashboard",,,
5+
"BMW",15000,"airbag",,,"white"
6+
"BMW",15000,"dashboard",,,"black"

test/fixtures/json/unwindComplexObject.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
},
1919
{
2020
"name": "dashboard",
21-
"items": "none"
21+
"items": []
2222
}
2323
]
2424
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy