diff --git a/lib/reject.js b/lib/reject.js index 7e26734..9ec1b74 100644 --- a/lib/reject.js +++ b/lib/reject.js @@ -2,7 +2,12 @@ import _reject from 'lodash/reject'; import wrap from './wrap'; function reject(predicate, collection) { - return _reject(collection, predicate); + const result = _reject(collection, predicate); + const equal = collection.length === result.length; + + return equal ? + collection : + result; } export default wrap(reject); diff --git a/test/reject-spec.js b/test/reject-spec.js index b628cc2..80edade 100644 --- a/test/reject-spec.js +++ b/test/reject-spec.js @@ -14,6 +14,24 @@ describe('u.reject', () => { expect(result).to.eql([{ bad: false }]); }); + it('returns the same instance if reject doesn\'t make changes', () => { + const object = { foo: [1, 2, 3] }; + const result = u({ + foo: u.reject(x => x === 'Justin Bieber'), + }, object); + + expect(result).to.equal(object); + }); + + it('returns a different instance if reject makes changes', () => { + const object = { foo: [1, 2, 3, 4] }; + const result = u({ + foo: u.reject(x => x === 4), + }, object); + + expect(result).to.not.equal(object); + }); + it('freezes the result', () => { expect(Object.isFrozen(u.reject('a', []))).to.be.true; });