Refactor update.js and remove some lodash
This commit is contained in:
parent
f8ea61ede0
commit
8e82692be2
@ -1,6 +1,7 @@
|
|||||||
# Change Log
|
# Change Log
|
||||||
|
|
||||||
## [unreleased]
|
## [unreleased]
|
||||||
|
* Removed a couple lodash dependencies.
|
||||||
|
|
||||||
## [0.5.0]
|
## [0.5.0]
|
||||||
* Add `u.is` to test predicates in a single path. (https://github.com/substantial/updeep/issues/13)
|
* 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';
|
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 = {}) {
|
function resolveUpdates(updates, object = {}) {
|
||||||
return reduce(updates, (acc, value, key) => {
|
return reduce(updates, (acc, value, key) => {
|
||||||
let updatedValue = value;
|
let updatedValue = value;
|
||||||
|
|
||||||
if (!Array.isArray(value) && value !== null && typeof value === 'object') {
|
if (!Array.isArray(value) && value !== null && typeof value === 'object') {
|
||||||
updatedValue = update(value, object[key]);
|
updatedValue = update(value, object[key]);
|
||||||
} else if (typeof value === 'function') {
|
} else if (typeof value === 'function') {
|
||||||
@ -21,12 +29,13 @@ function resolveUpdates(updates, object = {}) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function updateArray(updates, object) {
|
function updateArray(updates, object) {
|
||||||
const newObj = [...object];
|
const newArray = [...object];
|
||||||
|
|
||||||
return reduce(updates, (acc, value, index) => {
|
Object.keys(updates).forEach((key) => {
|
||||||
acc[index] = value;
|
newArray[key] = updates[key];
|
||||||
return acc;
|
});
|
||||||
}, newObj);
|
|
||||||
|
return newArray;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -63,7 +72,7 @@ function update(updates, object, ...args) {
|
|||||||
return updateArray(resolvedUpdates, object);
|
return updateArray(resolvedUpdates, object);
|
||||||
}
|
}
|
||||||
|
|
||||||
return assign({}, object, resolvedUpdates);
|
return { ...object, ...resolvedUpdates };
|
||||||
}
|
}
|
||||||
|
|
||||||
export default wrap(update);
|
export default wrap(update);
|
||||||
|
Loading…
Reference in New Issue
Block a user