u.reject should return same instance if no changes. (#62)

* Added failing test - u.reject should return same instance if no changes.

* `u.reject` now returns the same instance if nothing rejected
This commit is contained in:
Anson Kao 2016-06-06 23:51:11 -04:00 committed by Aaron Jensen
parent 01c45603cb
commit a887d5f929
2 changed files with 24 additions and 1 deletions

View File

@ -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);

View File

@ -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;
});