1 var baseCallback = require('./baseCallback'),
2 baseEach = require('./baseEach'),
3 isArray = require('../lang/isArray');
6 * Creates a function that aggregates a collection, creating an accumulator
7 * object composed from the results of running each element in the collection
10 * **Note:** This function is used to create `_.countBy`, `_.groupBy`, `_.indexBy`,
14 * @param {Function} setter The function to set keys and values of the accumulator object.
15 * @param {Function} [initializer] The function to initialize the accumulator object.
16 * @returns {Function} Returns the new aggregator function.
18 function createAggregator(setter, initializer) {
19 return function(collection, iteratee, thisArg) {
20 var result = initializer ? initializer() : {};
21 iteratee = baseCallback(iteratee, thisArg, 3);
23 if (isArray(collection)) {
25 length = collection.length;
27 while (++index < length) {
28 var value = collection[index];
29 setter(result, value, iteratee(value, index, collection), collection);
32 baseEach(collection, function(value, key, collection) {
33 setter(result, value, iteratee(value, key, collection), collection);
40 module.exports = createAggregator;