diff --git a/index.js b/index.js index 4265c69..9b0fe6c 100644 --- a/index.js +++ b/index.js @@ -14,10 +14,8 @@ */ exports = module.exports = function(a, b){ - if (a && b) { - for (var key in b) { - a[key] = b[key]; - } - } - return a; + return { + ...(a ?? {}), + ...(b ?? {}) + }; }; diff --git a/test/index.test.js b/test/index.test.js index 7241855..0202d3b 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -47,5 +47,23 @@ describe('merge', function() { expect(o).to.be.equal(a); }); }); - + + it('should not mutate first parameter', function() { + const foo = { foo: "foo" }; + const bar = { bar: "bar" }; + const result = merge(foo, bar); + expect(Object.keys(foo)).to.have.length(1); + expect(result.foo).to.be.equal('foo'); + expect(result.bar).to.be.equal('bar'); + }); + + + it('should not mutate second parameter', function() { + const foo = { foo: "foo" }; + const bar = { bar: "bar" }; + const result = merge(foo, bar); + expect(Object.keys(bar)).to.have.length(1); + expect(result.foo).to.be.equal('foo'); + expect(result.bar).to.be.equal('bar'); + }); });