Consolidate aggregate modules.

This commit is contained in:
John-David Dalton
2017-01-11 12:26:35 -08:00
parent 3b4cbc70e7
commit d80077ed94
7 changed files with 36 additions and 93 deletions

View File

@@ -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;

View File

@@ -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;

View File

@@ -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;