Use lodash assign instead

main
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 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);

View File

@ -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: {} });
});
});