Use lodash assign instead

This commit is contained in:
Aaron Jensen 2015-08-01 09:09:52 -07:00
parent c78b6e4803
commit 0e48adc468
2 changed files with 23 additions and 16 deletions

View File

@ -1,6 +1,7 @@
import reduce from 'lodash/collection/reduce'; import reduce from 'lodash/collection/reduce';
import isEmpty from 'lodash/lang/isEmpty'; import isEmpty from 'lodash/lang/isEmpty';
import curry from 'lodash/function/curry'; import curry from 'lodash/function/curry';
import assign from 'lodash/object/assign';
function resolveUpdates(updates, obj = {}) { function resolveUpdates(updates, obj = {}) {
return reduce(updates, (acc, value, key) => { return reduce(updates, (acc, value, key) => {
@ -53,7 +54,7 @@ function update(updates, obj) {
return updateArray(resolvedUpdates, obj); return updateArray(resolvedUpdates, obj);
} }
return Object.assign({}, obj, resolvedUpdates); return assign({}, obj, resolvedUpdates);
} }
export default curry(update); export default curry(update);

View File

@ -1,52 +1,52 @@
import { expect } from 'chai'; import { expect } from 'chai';
import updeep from '../lib'; import u from '../lib';
describe('updeep', () => { describe('updeep', () => {
it('does not change anything if no updates are specified', () => { it('does not change anything if no updates are specified', () => {
const obj = { foo: 3, bar: [7, 5] }; const obj = { foo: 3, bar: [7, 5] };
const result = updeep({}, obj); const result = u({}, obj);
expect(result).to.equal(obj); expect(result).to.equal(obj);
}); });
it('can update with fixed values', () => { it('can update with fixed values', () => {
const obj = { foo: 3, bar: [7, 5] }; 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] }); expect(result).to.deep.equal({ foo: 4, bar: [7, 5] });
}); });
it('returns the same instance if an update doesn\'t make changes', () => { it('returns the same instance if an update doesn\'t make changes', () => {
const obj = { foo: 3 }; const obj = { foo: 3 };
const result = updeep({ foo: 3 }, obj); const result = u({ foo: 3 }, obj);
expect(result).to.equal(obj); expect(result).to.equal(obj);
}); });
it('can update a nested structure', () => { it('can update a nested structure', () => {
const obj = { foo: { bar: 7, bam: 3 }, baz: 32 }; 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 }); expect(result).to.deep.equal({ foo: { bar: 8, bam: 3 }, baz: 32 });
}); });
it('can update arrays', () => { it('can update arrays', () => {
const obj = [1, 2, 3]; 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]); expect(result).to.deep.equal([1, 7, 3]);
}); });
it('can add an element to an array', () => { it('can add an element to an array', () => {
const obj = []; const obj = [];
const result = updeep({ 0: 3 }, obj); const result = u({ 0: 3 }, obj);
expect(result).to.eql([3]); expect(result).to.eql([3]);
}); });
it('can update nested arrays', () => { it('can update nested arrays', () => {
const obj = { foo: [1, 2, 3], bar: 9 }; 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 }); expect(result).to.deep.equal({ foo: [1, 7, 3], bar: 9 });
}); });
@ -54,7 +54,7 @@ describe('updeep', () => {
it('can use functions to update values', () => { it('can use functions to update values', () => {
const inc = (i) => i + 1; const inc = (i) => i + 1;
const obj = { foo: 3, bar: 4, baz: 7 }; 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 }); expect(result).to.deep.equal({ foo: 4, bar: 5, baz: 7 });
}); });
@ -62,7 +62,7 @@ describe('updeep', () => {
it('is curryable', () => { it('is curryable', () => {
const inc = (i) => i + 1; const inc = (i) => i + 1;
const obj = { foo: 3 }; const obj = { foo: 3 };
const incFoo = updeep({ foo: inc }); const incFoo = u({ foo: inc });
const result = incFoo(obj); const result = incFoo(obj);
@ -71,15 +71,15 @@ describe('updeep', () => {
it('can update if the value is an array', () => { it('can update if the value is an array', () => {
const obj = {}; const obj = {};
const result = updeep({ foo: [0, 1] }, obj); const result = u({ foo: [0, 1] }, obj);
expect(result).to.deep.equal({ foo: [0, 1] }); expect(result).to.deep.equal({ foo: [0, 1] });
}); });
it('can use withDefault to default things', () => { it('can use withDefault to default things', () => {
const obj = {}; const obj = {};
const result = updeep({ const result = u({
foo: updeep.withDefault([], { foo: u.withDefault([], {
0: 'bar' 0: 'bar'
}) })
}, obj); }, obj);
@ -88,14 +88,20 @@ describe('updeep', () => {
}); });
it('can update when original object is undefined', () => { 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] }); expect(result).to.deep.equal({ foo: [0, 1] });
}); });
it('can take a function as the updater', () => { 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); 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: {} });
});
}); });