From 8e82692be260018b6d3d376654da83f50b062276 Mon Sep 17 00:00:00 2001 From: Aaron Jensen Date: Sat, 8 Aug 2015 23:32:52 -0700 Subject: [PATCH] Refactor update.js and remove some lodash --- .babelrc | 1 + CHANGELOG.md | 1 + lib/update.js | 27 ++++++++++++++++++--------- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/.babelrc b/.babelrc index 7f389c8..86a1a91 100644 --- a/.babelrc +++ b/.babelrc @@ -1,3 +1,4 @@ { + stage: "1", loose: "all" } diff --git a/CHANGELOG.md b/CHANGELOG.md index 9020984..de485c2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/lib/update.js b/lib/update.js index 9db9271..5640197 100644 --- a/lib/update.js +++ b/lib/update.js @@ -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);