Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: unwind transform issue with nested arrays #548

Merged
merged 1 commit into from
Jan 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 { ...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) => ({ ...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),
}), {});
}

module.exports = {
Expand Down
2 changes: 1 addition & 1 deletion test/CLI.js
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,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 @@ -905,7 +905,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 @@ -31,9 +31,8 @@ module.exports = (testRunner, jsonFixtures, csvFixtures) => {
});


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
2 changes: 1 addition & 1 deletion test/JSON2CSVTransform.js
Original file line number Diff line number Diff line change
Expand Up @@ -1010,7 +1010,7 @@ module.exports = (testRunner, jsonFixtures, csvFixtures, inMemoryJsonFixtures) =

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