5137861a0d36aadece82e4c65eb227125cff7823
[platform/framework/web/crosswalk-tizen.git] /
1 var baseCallback = require('./baseCallback'),
2     baseEach = require('./baseEach'),
3     isArray = require('../lang/isArray');
4
5 /**
6  * Creates a function that aggregates a collection, creating an accumulator
7  * object composed from the results of running each element in the collection
8  * through an iteratee.
9  *
10  * **Note:** This function is used to create `_.countBy`, `_.groupBy`, `_.indexBy`,
11  * and `_.partition`.
12  *
13  * @private
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.
17  */
18 function createAggregator(setter, initializer) {
19   return function(collection, iteratee, thisArg) {
20     var result = initializer ? initializer() : {};
21     iteratee = baseCallback(iteratee, thisArg, 3);
22
23     if (isArray(collection)) {
24       var index = -1,
25           length = collection.length;
26
27       while (++index < length) {
28         var value = collection[index];
29         setter(result, value, iteratee(value, index, collection), collection);
30       }
31     } else {
32       baseEach(collection, function(value, key, collection) {
33         setter(result, value, iteratee(value, key, collection), collection);
34       });
35     }
36     return result;
37   };
38 }
39
40 module.exports = createAggregator;