Skip to content

Commit

Permalink
fix(merge) - Merge should not mutate inputs with mismatches keys
Browse files Browse the repository at this point in the history
It retained a reference if you passed an object that didn't end up
matching a rule within the logic. Now it checks for that case and clones
instead so you get a new object.
  • Loading branch information
bebraw committed Nov 29, 2016
1 parent 0a8bffe commit fd39e5a
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
1.0.2 / 2016-11-29
==================

* Bug fix - `merge` should not mutate inputs with mismatched keys.

1.0.0 / 2016-11-28
==================

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"lib"
],
"dependencies": {
"lodash.clonedeep": "^4.5.0",
"lodash.differencewith": "^4.5.0",
"lodash.isequal": "^4.4.0",
"lodash.isfunction": "^3.0.8",
Expand Down
5 changes: 5 additions & 0 deletions src/join-arrays.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const cloneDeep = require('lodash.clonedeep');
const isFunction = require('lodash.isfunction');
const isPlainObject = require('lodash.isplainobject');
const mergeWith = require('lodash.mergewith');
Expand Down Expand Up @@ -30,6 +31,10 @@ module.exports = function joinArrays({
}));
}

if (isPlainObject(b)) {
return cloneDeep(b);
}

return b;
};
};
15 changes: 15 additions & 0 deletions tests/merge-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,21 @@ function mergeTests(merge) {

assert.deepEqual(merge(a, b).postcss('a'), expected);
});

it('should not mutate inputs with mismatched keys', function () {
const a = {
entry: {}
};

const b = {};

const aClone = JSON.parse(JSON.stringify(a));
const config = merge({}, a, b);

config.entry.main = 'src/index.js';

assert.deepEqual(a, aClone);
});
}

module.exports = mergeTests;

0 comments on commit fd39e5a

Please sign in to comment.