baseReduce.js 986 B

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