diff --git a/CHANGELOG.md b/CHANGELOG.md index 7e5d913..5aeaa5d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## [unreleased] * Add `u.if` to conditionally update objects. * Add `u.map` to update all values in an array or object. +* Replace object outright if null or constant givens as `updates`. ## [0.3.1] * Actually expose `u.in`. diff --git a/lib/update.js b/lib/update.js index e723409..36f1308 100644 --- a/lib/update.js +++ b/lib/update.js @@ -48,6 +48,10 @@ function update(updates, object) { return updates(object); } + if (updates === null || typeof updates !== 'object') { + return updates; + } + const resolvedUpdates = resolveUpdates(updates, object); if (isEmpty(resolvedUpdates)) { diff --git a/test/updeep-spec.js b/test/updeep-spec.js index 00c0a1c..3ef8306 100644 --- a/test/updeep-spec.js +++ b/test/updeep-spec.js @@ -37,6 +37,11 @@ describe('updeep', () => { expect(result).to.deep.equal([1, 7, 3]); }); + it('replaces the object outright if updates are a constant', () => { + expect(u(3, {})).to.equal(3); + expect(u(null, {})).to.be.null; + }); + it('can add an element to an array', () => { const object = []; const result = u({ 0: 3 }, object);