Skip to content

Commit

Permalink
fix: don't mutate original object in unset (#499)
Browse files Browse the repository at this point in the history
  • Loading branch information
juanjoDiaz committed Nov 12, 2020
1 parent b071eb1 commit 6e4ea5e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ function unsetProp(obj, path) {
}

if (pathArray.length === 1) {
delete obj[key];
return obj;
return Object.keys(obj)
.filter(prop => prop !== key)
.reduce((acc, prop) => Object.assign(acc, { [prop]: obj[prop] }), {});
}

return unsetProp(obj[key], restPath);
Expand Down
15 changes: 15 additions & 0 deletions test/JSON2CSVParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,21 @@ module.exports = (testRunner, jsonFixtures, csvFixtures) => {
t.end();
});

testRunner.add('should not modify the JSON object passed 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);
const originalJson = JSON.parse(JSON.stringify(jsonFixtures.unwindComplexObject));
const csv = parser.parse(originalJson);

t.ok(typeof csv === 'string');
t.equal(csv, csvFixtures.unwindComplexObject);
t.deepEqual(jsonFixtures.unwindComplexObject, originalJson);
t.end();
});

testRunner.add('should error if input data is not an object', (t) => {
const input = 'not an object';
try {
Expand Down

0 comments on commit 6e4ea5e

Please sign in to comment.