Skip to content

Commit

Permalink
fix: unwind transform issue with nested arrays (#549)
Browse files Browse the repository at this point in the history
  • Loading branch information
juanjoDiaz committed Jan 4, 2022
1 parent f0ca2c2 commit 485ea77
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 16 deletions.
12 changes: 9 additions & 3 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ function getProp(obj, path, defaultValue) {
function setProp(obj, path, value) {
const pathArray = Array.isArray(path) ? path : path.split('.');
const [key, ...restPath] = pathArray;
const newValue = pathArray.length > 1 ? setProp(obj[key] || {}, restPath, value) : value;
return Object.assign({}, obj, { [key]: newValue });
return {
...obj,
[key]: pathArray.length > 1 ? setProp(obj[key] || {}, restPath, value) : value
};
}

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

return unsetProp(obj[key], restPath);
return Object.keys(obj)
.reduce((acc, prop) => ({
...acc,
[prop]: prop !== key ? obj[prop] : unsetProp(obj[key], restPath),
}), {});
}

function flattenReducer(acc, arr) {
Expand Down
2 changes: 1 addition & 1 deletion test/CLI.js
Original file line number Diff line number Diff line change
Expand Up @@ -843,7 +843,7 @@ module.exports = (testRunner, jsonFixtures, csvFixtures) => {
});

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

exec(`${cli} -i "${getFixturePath('/json/unwindComplexObject.json')}" ${opts}`, (err, stdout, stderr) => {
Expand Down
2 changes: 1 addition & 1 deletion test/JSON2CSVAsyncParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -1278,7 +1278,7 @@ module.exports = (testRunner, jsonFixtures, csvFixtures, inMemoryJsonFixtures) =

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

Expand Down
3 changes: 1 addition & 2 deletions test/JSON2CSVParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@ module.exports = (testRunner, jsonFixtures, csvFixtures) => {
t.end();
});

testRunner.add('should not modify the JSON object passed passed', (t) => {
testRunner.add('should not modify the JSON object passed', (t) => {
const opts = {
fields: ["carModel", "price", "extras.items.name", "extras.items.items.position", "extras.items.items.color", "extras.items.items", "name", "color", "extras.items.color"],
transforms: [unwind({ paths: ['extras.items', 'extras.items.items'] }), flatten()],
};
const parser = new Json2csvParser(opts);
Expand Down
4 changes: 2 additions & 2 deletions test/JSON2CSVTransform.js
Original file line number Diff line number Diff line change
Expand Up @@ -1317,9 +1317,9 @@ module.exports = (testRunner, jsonFixtures, csvFixtures, inMemoryJsonFixtures) =
});


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

Expand Down
12 changes: 6 additions & 6 deletions test/fixtures/csv/unwindComplexObject.csv
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"carModel","price","extras.items.name","extras.items.items.position","extras.items.items.color","extras.items.items","name","color","extras.items.color"
"Porsche",30000,"airbag","left","white",,,,
"Porsche",30000,"airbag","right","gray",,,,
"Porsche",30000,"dashboard",,,"none",,,
,,,,,,"airbag","white",
"BMW",15000,"dashboard",,,,,,"black"
"carModel","price","extras.items.name","extras.items.items.position","extras.items.items.color","extras.items.color"
"Porsche",30000,"airbag","left","white",
"Porsche",30000,"airbag","right","gray",
"Porsche",30000,"dashboard",,,
"BMW",15000,"airbag",,,"white"
"BMW",15000,"dashboard",,,"black"
2 changes: 1 addition & 1 deletion test/fixtures/json/unwindComplexObject.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
},
{
"name": "dashboard",
"items": "none"
"items": []
}
]
}
Expand Down

0 comments on commit 485ea77

Please sign in to comment.