From 0e48adc4685fd62aabd8fa1dcacd3be092823343 Mon Sep 17 00:00:00 2001 From: Aaron Jensen Date: Sat, 1 Aug 2015 09:09:52 -0700 Subject: [PATCH] Use lodash assign instead --- lib/update.js | 3 ++- test/index.js | 36 +++++++++++++++++++++--------------- 2 files changed, 23 insertions(+), 16 deletions(-) diff --git a/lib/update.js b/lib/update.js index 3fa7025..06e8e90 100644 --- a/lib/update.js +++ b/lib/update.js @@ -1,6 +1,7 @@ import reduce from 'lodash/collection/reduce'; import isEmpty from 'lodash/lang/isEmpty'; import curry from 'lodash/function/curry'; +import assign from 'lodash/object/assign'; function resolveUpdates(updates, obj = {}) { return reduce(updates, (acc, value, key) => { @@ -53,7 +54,7 @@ function update(updates, obj) { return updateArray(resolvedUpdates, obj); } - return Object.assign({}, obj, resolvedUpdates); + return assign({}, obj, resolvedUpdates); } export default curry(update); diff --git a/test/index.js b/test/index.js index caacc89..4a5cba3 100644 --- a/test/index.js +++ b/test/index.js @@ -1,52 +1,52 @@ import { expect } from 'chai'; -import updeep from '../lib'; +import u from '../lib'; describe('updeep', () => { it('does not change anything if no updates are specified', () => { const obj = { foo: 3, bar: [7, 5] }; - const result = updeep({}, obj); + const result = u({}, obj); expect(result).to.equal(obj); }); it('can update with fixed values', () => { const obj = { foo: 3, bar: [7, 5] }; - const result = updeep({ foo: 4 }, obj); + const result = u({ foo: 4 }, obj); expect(result).to.deep.equal({ foo: 4, bar: [7, 5] }); }); it('returns the same instance if an update doesn\'t make changes', () => { const obj = { foo: 3 }; - const result = updeep({ foo: 3 }, obj); + const result = u({ foo: 3 }, obj); expect(result).to.equal(obj); }); it('can update a nested structure', () => { const obj = { foo: { bar: 7, bam: 3 }, baz: 32 }; - const result = updeep({ foo: { bar: 8 } }, obj); + const result = u({ foo: { bar: 8 } }, obj); expect(result).to.deep.equal({ foo: { bar: 8, bam: 3 }, baz: 32 }); }); it('can update arrays', () => { const obj = [1, 2, 3]; - const result = updeep({ 1: 7 }, obj); + const result = u({ 1: 7 }, obj); expect(result).to.deep.equal([1, 7, 3]); }); it('can add an element to an array', () => { const obj = []; - const result = updeep({ 0: 3 }, obj); + const result = u({ 0: 3 }, obj); expect(result).to.eql([3]); }); it('can update nested arrays', () => { const obj = { foo: [1, 2, 3], bar: 9 }; - const result = updeep({ foo: { 1: 7 } }, obj); + const result = u({ foo: { 1: 7 } }, obj); expect(result).to.deep.equal({ foo: [1, 7, 3], bar: 9 }); }); @@ -54,7 +54,7 @@ describe('updeep', () => { it('can use functions to update values', () => { const inc = (i) => i + 1; const obj = { foo: 3, bar: 4, baz: 7 }; - const result = updeep({ foo: inc, bar: inc }, obj); + const result = u({ foo: inc, bar: inc }, obj); expect(result).to.deep.equal({ foo: 4, bar: 5, baz: 7 }); }); @@ -62,7 +62,7 @@ describe('updeep', () => { it('is curryable', () => { const inc = (i) => i + 1; const obj = { foo: 3 }; - const incFoo = updeep({ foo: inc }); + const incFoo = u({ foo: inc }); const result = incFoo(obj); @@ -71,15 +71,15 @@ describe('updeep', () => { it('can update if the value is an array', () => { const obj = {}; - const result = updeep({ foo: [0, 1] }, obj); + const result = u({ foo: [0, 1] }, obj); expect(result).to.deep.equal({ foo: [0, 1] }); }); it('can use withDefault to default things', () => { const obj = {}; - const result = updeep({ - foo: updeep.withDefault([], { + const result = u({ + foo: u.withDefault([], { 0: 'bar' }) }, obj); @@ -88,14 +88,20 @@ describe('updeep', () => { }); it('can update when original object is undefined', () => { - const result = updeep({ foo: [0, 1] }, undefined); + const result = u({ foo: [0, 1] }, undefined); expect(result).to.deep.equal({ foo: [0, 1] }); }); it('can take a function as the updater', () => { - const result = updeep(i => i + 1, 7); + const result = u(i => i + 1, 7); expect(result).to.eql(8); }); + + it('can omit a key', () => { + const result = u({ foo: u.omit('bar') }, { foo: { bar: 7 } }); + + expect(result).to.eql({ foo: {} }); + }); });