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:
parent
01c45603cb
commit
a887d5f929
@ -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);
|
||||
|
@ -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;
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user