mirror of
https://github.com/nodejs/node.git
synced 2025-08-18 15:18:46 +02:00

PR-URL: https://github.com/nodejs/io.js/pull/2286 Reviewed-By: Roman Reiss <me@silverwind.io>
24 lines
986 B
JavaScript
24 lines
986 B
JavaScript
/**
|
|
* The base implementation of `_.reduce` and `_.reduceRight` without support
|
|
* for callback shorthands and `this` binding, which iterates over `collection`
|
|
* using the provided `eachFunc`.
|
|
*
|
|
* @private
|
|
* @param {Array|Object|string} collection The collection to iterate over.
|
|
* @param {Function} iteratee The function invoked per iteration.
|
|
* @param {*} accumulator The initial value.
|
|
* @param {boolean} initFromCollection Specify using the first or last element
|
|
* of `collection` as the initial value.
|
|
* @param {Function} eachFunc The function to iterate over `collection`.
|
|
* @returns {*} Returns the accumulated value.
|
|
*/
|
|
function baseReduce(collection, iteratee, accumulator, initFromCollection, eachFunc) {
|
|
eachFunc(collection, function(value, index, collection) {
|
|
accumulator = initFromCollection
|
|
? (initFromCollection = false, value)
|
|
: iteratee(accumulator, value, index, collection);
|
|
});
|
|
return accumulator;
|
|
}
|
|
|
|
module.exports = baseReduce;
|