mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-10 10:57:49 +00:00
Consolidate aggregate modules.
This commit is contained in:
@@ -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;
|
||||
@@ -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;
|
||||
@@ -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;
|
||||
Reference in New Issue
Block a user