createForEach.js 702 B

1234567891011121314151617181920
  1. var bindCallback = require('./bindCallback'),
  2. isArray = require('../lang/isArray');
  3. /**
  4. * Creates a function for `_.forEach` or `_.forEachRight`.
  5. *
  6. * @private
  7. * @param {Function} arrayFunc The function to iterate over an array.
  8. * @param {Function} eachFunc The function to iterate over a collection.
  9. * @returns {Function} Returns the new each function.
  10. */
  11. function createForEach(arrayFunc, eachFunc) {
  12. return function(collection, iteratee, thisArg) {
  13. return (typeof iteratee == 'function' && thisArg === undefined && isArray(collection))
  14. ? arrayFunc(collection, iteratee)
  15. : eachFunc(collection, bindCallback(iteratee, thisArg, 3));
  16. };
  17. }
  18. module.exports = createForEach;