From d80077ed94720f85f26b617066c16b966aa74867 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Wed, 11 Jan 2017 12:26:35 -0800 Subject: [PATCH] Consolidate aggregate modules. --- .internal/arrayAggregator.js | 22 ---------------------- .internal/baseAggregator.js | 21 --------------------- .internal/createAggregator.js | 20 -------------------- countBy.js | 19 +++++++++++-------- groupBy.js | 19 +++++++++++-------- keyBy.js | 17 +++++++---------- partition.js | 11 +++++++---- 7 files changed, 36 insertions(+), 93 deletions(-) delete mode 100644 .internal/arrayAggregator.js delete mode 100644 .internal/baseAggregator.js delete mode 100644 .internal/createAggregator.js diff --git a/.internal/arrayAggregator.js b/.internal/arrayAggregator.js deleted file mode 100644 index eb0f3edf8..000000000 --- a/.internal/arrayAggregator.js +++ /dev/null @@ -1,22 +0,0 @@ -/** - * A specialized version of `baseAggregator` for arrays. - * - * @private - * @param {Array} [array] The array to iterate over. - * @param {Function} setter The function to set `accumulator` values. - * @param {Function} iteratee The iteratee to transform keys. - * @param {Object} accumulator The initial aggregated object. - * @returns {Function} Returns `accumulator`. - */ -function arrayAggregator(array, setter, iteratee, accumulator) { - let index = -1; - const length = array == null ? 0 : array.length; - - while (++index < length) { - const value = array[index]; - setter(accumulator, value, iteratee(value), array); - } - return accumulator; -} - -export default arrayAggregator; diff --git a/.internal/baseAggregator.js b/.internal/baseAggregator.js deleted file mode 100644 index 59c4ee2e3..000000000 --- a/.internal/baseAggregator.js +++ /dev/null @@ -1,21 +0,0 @@ -import baseEach from './baseEach.js'; - -/** - * Aggregates elements of `collection` on `accumulator` with keys transformed - * by `iteratee` and values set by `setter`. - * - * @private - * @param {Array|Object} collection The collection to iterate over. - * @param {Function} setter The function to set `accumulator` values. - * @param {Function} iteratee The iteratee to transform keys. - * @param {Object} accumulator The initial aggregated object. - * @returns {Function} Returns `accumulator`. - */ -function baseAggregator(collection, setter, iteratee, accumulator) { - baseEach(collection, (value, key, collection) => { - setter(accumulator, value, iteratee(value), collection); - }); - return accumulator; -} - -export default baseAggregator; diff --git a/.internal/createAggregator.js b/.internal/createAggregator.js deleted file mode 100644 index 8c41312c7..000000000 --- a/.internal/createAggregator.js +++ /dev/null @@ -1,20 +0,0 @@ -import arrayAggregator from './.internal/arrayAggregator.js'; -import baseAggregator from './.internal/baseAggregator.js'; - -/** - * Creates a function like `groupBy`. - * - * @private - * @param {Function} setter The function to set accumulator values. - * @param {Function} [initializer] The accumulator object initializer. - * @returns {Function} Returns the new aggregator function. - */ -function createAggregator(setter, initializer) { - return (collection, iteratee) => { - const func = Array.isArray(collection) ? arrayAggregator : baseAggregator; - const accumulator = initializer ? initializer() : {}; - return func(collection, setter, iteratee, accumulator); - }; -} - -export default createAggregator; diff --git a/countBy.js b/countBy.js index 11b6aa858..4b3938951 100644 --- a/countBy.js +++ b/countBy.js @@ -1,5 +1,5 @@ import baseAssignValue from './.internal/baseAssignValue.js'; -import createAggregator from './.internal/createAggregator.js'; +import reduce from './reduce.js'; /** Used to check objects for own properties. */ const hasOwnProperty = Object.prototype.hasOwnProperty; @@ -20,12 +20,15 @@ const hasOwnProperty = Object.prototype.hasOwnProperty; * countBy([6.1, 4.2, 6.3], Math.floor); * // => { '4': 1, '6': 2 } */ -const countBy = createAggregator((result, value, key) => { - if (hasOwnProperty.call(result, key)) { - ++result[key]; - } else { - baseAssignValue(result, key, 1); - } -}); +function countBy(collection, iteratee) { + return reduce(collection, (result, value, key) => { + if (hasOwnProperty.call(result, key)) { + ++result[key]; + } else { + baseAssignValue(result, key, 1); + } + return result; + }, {}); +} export default countBy; diff --git a/groupBy.js b/groupBy.js index f57e71e1c..376e6c1e3 100644 --- a/groupBy.js +++ b/groupBy.js @@ -1,5 +1,5 @@ import baseAssignValue from './.internal/baseAssignValue.js'; -import createAggregator from './.internal/createAggregator.js'; +import reduce from './reduce.js'; /** Used to check objects for own properties. */ const hasOwnProperty = Object.prototype.hasOwnProperty; @@ -21,12 +21,15 @@ const hasOwnProperty = Object.prototype.hasOwnProperty; * groupBy([6.1, 4.2, 6.3], Math.floor); * // => { '4': [4.2], '6': [6.1, 6.3] } */ -const groupBy = createAggregator((result, value, key) => { - if (hasOwnProperty.call(result, key)) { - result[key].push(value); - } else { - baseAssignValue(result, key, [value]); - } -}); +function groupBy(collection, iteratee) { + return reduce(collection, (result, value, key) => { + if (hasOwnProperty.call(result, key)) { + result[key].push(value); + } else { + baseAssignValue(result, key, [value]); + } + return result; + }, {}); +} export default groupBy; diff --git a/keyBy.js b/keyBy.js index 239c206f9..5259b0b71 100644 --- a/keyBy.js +++ b/keyBy.js @@ -1,5 +1,5 @@ import baseAssignValue from './.internal/baseAssignValue.js'; -import createAggregator from './.internal/createAggregator.js'; +import reduce from './reduce.js'; /** * Creates an object composed of keys generated from the results of running @@ -19,16 +19,13 @@ import createAggregator from './.internal/createAggregator.js'; * { 'dir': 'right', 'code': 100 } * ]; * - * keyBy(array, function(o) { - * return String.fromCharCode(o.code); - * }); + * keyBy(array, ({ code }) => String.fromCharCode(code)); * // => { 'a': { 'dir': 'left', 'code': 97 }, 'd': { 'dir': 'right', 'code': 100 } } - * - * keyBy(array, 'dir'); - * // => { 'left': { 'dir': 'left', 'code': 97 }, 'right': { 'dir': 'right', 'code': 100 } } */ -const keyBy = createAggregator((result, value, key) => { - baseAssignValue(result, key, value); -}); +function keyBy(collection, iteratee) { + return reduce(collection, (result, value, key) => ( + baseAssignValue(result, key, value) + ), {}); +} export default keyBy; diff --git a/partition.js b/partition.js index 921636901..72cf8c0f7 100644 --- a/partition.js +++ b/partition.js @@ -1,4 +1,4 @@ -import createAggregator from './.internal/createAggregator.js'; +import reduce from './reduce.js'; /** * Creates an array of elements split into two groups, the first of which @@ -19,10 +19,13 @@ import createAggregator from './.internal/createAggregator.js'; * { 'user': 'pebbles', 'age': 1, 'active': false } * ]; * - * partition(users, o => o.active); + * partition(users, ({ active }) => active); * // => objects for [['fred'], ['barney', 'pebbles']] */ -const partition = createAggregator((result, value, key) => - result[key ? 0 : 1].push(value), () => [[], []]); +function partition(collection, predicate) { + return reduce(collection, (result, value, key) => ( + result[key ? 0 : 1].push(value) + ), [[], []]); +} export default partition;