6ec544251dbb59c20674e17fb20cefd4f1a58be6
[yaffs-website] / baseReduce.js
1 /**
2  * The base implementation of `_.reduce` and `_.reduceRight`, without support
3  * for iteratee shorthands, which iterates over `collection` using `eachFunc`.
4  *
5  * @private
6  * @param {Array|Object} collection The collection to iterate over.
7  * @param {Function} iteratee The function invoked per iteration.
8  * @param {*} accumulator The initial value.
9  * @param {boolean} initAccum Specify using the first or last element of `collection` as the initial value.
10  * @param {Function} eachFunc The function to iterate over `collection`.
11  * @returns {*} Returns the accumulated value.
12  */
13 function baseReduce(collection, iteratee, accumulator, initAccum, eachFunc) {
14   eachFunc(collection, function(value, index, collection) {
15     accumulator = initAccum
16       ? (initAccum = false, value)
17       : iteratee(accumulator, value, index, collection);
18   });
19   return accumulator;
20 }
21
22 module.exports = baseReduce;