98da3ff0a2219d6c63a8ce0c6ee3093b37e7c62e
[yaffs-website] / node_modules / grunt-legacy-log-utils / node_modules / lodash / reduce.js
1 var arrayReduce = require('./_arrayReduce'),
2     baseEach = require('./_baseEach'),
3     baseIteratee = require('./_baseIteratee'),
4     baseReduce = require('./_baseReduce'),
5     isArray = require('./isArray');
6
7 /**
8  * Reduces `collection` to a value which is the accumulated result of running
9  * each element in `collection` through `iteratee`, where each successive
10  * invocation is supplied the return value of the previous. If `accumulator`
11  * is not given the first element of `collection` is used as the initial
12  * value. The iteratee is invoked with four arguments:
13  * (accumulator, value, index|key, collection).
14  *
15  * Many lodash methods are guarded to work as iteratees for methods like
16  * `_.reduce`, `_.reduceRight`, and `_.transform`.
17  *
18  * The guarded methods are:
19  * `assign`, `defaults`, `defaultsDeep`, `includes`, `merge`, `orderBy`,
20  * and `sortBy`
21  *
22  * @static
23  * @memberOf _
24  * @category Collection
25  * @param {Array|Object} collection The collection to iterate over.
26  * @param {Function} [iteratee=_.identity] The function invoked per iteration.
27  * @param {*} [accumulator] The initial value.
28  * @returns {*} Returns the accumulated value.
29  * @example
30  *
31  * _.reduce([1, 2], function(sum, n) {
32  *   return sum + n;
33  * }, 0);
34  * // => 3
35  *
36  * _.reduce({ 'a': 1, 'b': 2, 'c': 1 }, function(result, value, key) {
37  *   (result[value] || (result[value] = [])).push(key);
38  *   return result;
39  * }, {});
40  * // => { '1': ['a', 'c'], '2': ['b'] } (iteration order is not guaranteed)
41  */
42 function reduce(collection, iteratee, accumulator) {
43   var func = isArray(collection) ? arrayReduce : baseReduce,
44       initAccum = arguments.length < 3;
45
46   return func(collection, baseIteratee(iteratee, 4), accumulator, initAccum, baseEach);
47 }
48
49 module.exports = reduce;