Refactor update.js and remove some lodash
This commit is contained in:
parent
f8ea61ede0
commit
8e82692be2
@ -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)
|
||||
|
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user