Fix handling null differently than undefined.
`u` and friends will now coerce null to an object if there are updates for it. Fixes #20
This commit is contained in:
parent
e99d575972
commit
5b58b8cfc8
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
## [unreleased]
|
## [unreleased]
|
||||||
* Add `u._` placeholder for curried functions.
|
* Add `u._` placeholder for curried functions.
|
||||||
|
* Fix handling null differently than undefined. `u` and friends will now coerce null to an object if there are updates for it. (https://github.com/substantial/updeep/issues/20)
|
||||||
|
|
||||||
## [0.6.0]
|
## [0.6.0]
|
||||||
* Remove support for `_.placeholder` in curried methods. This may come back, but it was necessary for the next item.
|
* Remove support for `_.placeholder` in curried methods. This may come back, but it was necessary for the next item.
|
||||||
|
@ -10,7 +10,7 @@ function reduce(object, callback, initialValue) {
|
|||||||
}, initialValue);
|
}, initialValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
function resolveUpdates(updates, object = {}) {
|
function resolveUpdates(updates, object) {
|
||||||
return reduce(updates, (acc, value, key) => {
|
return reduce(updates, (acc, value, key) => {
|
||||||
let updatedValue = value;
|
let updatedValue = value;
|
||||||
|
|
||||||
@ -62,17 +62,21 @@ function update(updates, object, ...args) {
|
|||||||
return updates;
|
return updates;
|
||||||
}
|
}
|
||||||
|
|
||||||
const resolvedUpdates = resolveUpdates(updates, object);
|
const defaultedObject = (typeof object === 'undefined' || object === null) ?
|
||||||
|
{} :
|
||||||
|
object;
|
||||||
|
|
||||||
|
const resolvedUpdates = resolveUpdates(updates, defaultedObject);
|
||||||
|
|
||||||
if (isEmpty(resolvedUpdates)) {
|
if (isEmpty(resolvedUpdates)) {
|
||||||
return object;
|
return object;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Array.isArray(object)) {
|
if (Array.isArray(defaultedObject)) {
|
||||||
return updateArray(resolvedUpdates, object);
|
return updateArray(resolvedUpdates, defaultedObject);
|
||||||
}
|
}
|
||||||
|
|
||||||
return { ...object, ...resolvedUpdates };
|
return { ...defaultedObject, ...resolvedUpdates };
|
||||||
}
|
}
|
||||||
|
|
||||||
export default wrap(update, 2);
|
export default wrap(update, 2);
|
||||||
|
@ -118,4 +118,15 @@ describe('updeep', () => {
|
|||||||
|
|
||||||
expect(result).to.eql({ name: 'Joe Merrill', age: 22 });
|
expect(result).to.eql({ name: 'Joe Merrill', age: 22 });
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('defaults to an empty object when null or undefined', () => {
|
||||||
|
let result = u({ a: { b: 0 } }, { a: null });
|
||||||
|
expect(result).to.eql({ a: { b: 0 } });
|
||||||
|
|
||||||
|
result = u({ a: { b: 0 } }, { a: undefined });
|
||||||
|
expect(result).to.eql({ a: { b: 0 } });
|
||||||
|
|
||||||
|
result = u({ a: { b: 0 } }, { });
|
||||||
|
expect(result).to.eql({ a: { b: 0 } });
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user