Initial commit
[yaffs-website] / node_modules / grunt-contrib-watch / node_modules / lodash / internal / createAggregator.js
1 var baseCallback = require('./baseCallback'),
2     baseEach = require('./baseEach'),
3     isArray = require('../lang/isArray');
4
5 /**
6  * Creates a `_.countBy`, `_.groupBy`, `_.indexBy`, or `_.partition` function.
7  *
8  * @private
9  * @param {Function} setter The function to set keys and values of the accumulator object.
10  * @param {Function} [initializer] The function to initialize the accumulator object.
11  * @returns {Function} Returns the new aggregator function.
12  */
13 function createAggregator(setter, initializer) {
14   return function(collection, iteratee, thisArg) {
15     var result = initializer ? initializer() : {};
16     iteratee = baseCallback(iteratee, thisArg, 3);
17
18     if (isArray(collection)) {
19       var index = -1,
20           length = collection.length;
21
22       while (++index < length) {
23         var value = collection[index];
24         setter(result, value, iteratee(value, index, collection), collection);
25       }
26     } else {
27       baseEach(collection, function(value, key, collection) {
28         setter(result, value, iteratee(value, key, collection), collection);
29       });
30     }
31     return result;
32   };
33 }
34
35 module.exports = createAggregator;