Refactor update.js and remove some lodash

main
Aaron Jensen 2015-08-08 23:32:52 -07:00
parent f8ea61ede0
commit 8e82692be2
3 changed files with 20 additions and 9 deletions

View File

@ -1,3 +1,4 @@
{
stage: "1",
loose: "all"
}

View File

@ -1,6 +1,7 @@
# Change Log
## [unreleased]
* Removed a couple lodash dependencies.
## [0.5.0]
* Add `u.is` to test predicates in a single path. (https://github.com/substantial/updeep/issues/13)

View File

@ -1,11 +1,19 @@
import reduce from 'lodash/collection/reduce';
import isEmpty from 'lodash/lang/isEmpty';
import assign from 'lodash/object/assign';
import wrap from './wrap';
function isEmpty(object) {
return !Object.keys(object).length;
}
function reduce(object, callback, initialValue) {
return Object.keys(object).reduce((acc, key) => {
return callback(acc, object[key], key);
}, initialValue);
}
function resolveUpdates(updates, object = {}) {
return reduce(updates, (acc, value, key) => {
let updatedValue = value;
if (!Array.isArray(value) && value !== null && typeof value === 'object') {
updatedValue = update(value, object[key]);
} else if (typeof value === 'function') {
@ -21,12 +29,13 @@ function resolveUpdates(updates, object = {}) {
}
function updateArray(updates, object) {
const newObj = [...object];
const newArray = [...object];
return reduce(updates, (acc, value, index) => {
acc[index] = value;
return acc;
}, newObj);
Object.keys(updates).forEach((key) => {
newArray[key] = updates[key];
});
return newArray;
}
/**
@ -63,7 +72,7 @@ function update(updates, object, ...args) {
return updateArray(resolvedUpdates, object);
}
return assign({}, object, resolvedUpdates);
return { ...object, ...resolvedUpdates };
}
export default wrap(update);