createReduce.js 867 B

12345678910111213141516171819202122
  1. var baseCallback = require('./baseCallback'),
  2. baseReduce = require('./baseReduce'),
  3. isArray = require('../lang/isArray');
  4. /**
  5. * Creates a function for `_.reduce` or `_.reduceRight`.
  6. *
  7. * @private
  8. * @param {Function} arrayFunc The function to iterate over an array.
  9. * @param {Function} eachFunc The function to iterate over a collection.
  10. * @returns {Function} Returns the new each function.
  11. */
  12. function createReduce(arrayFunc, eachFunc) {
  13. return function(collection, iteratee, accumulator, thisArg) {
  14. var initFromArray = arguments.length < 3;
  15. return (typeof iteratee == 'function' && thisArg === undefined && isArray(collection))
  16. ? arrayFunc(collection, iteratee, accumulator, initFromArray)
  17. : baseReduce(collection, baseCallback(iteratee, thisArg, 4), accumulator, initFromArray, eachFunc);
  18. };
  19. }
  20. module.exports = createReduce;