_createBaseEach.js 886 B

123456789101112131415161718192021222324252627282930313233
  1. var isArrayLike = require('./isArrayLike');
  2. /**
  3. * Creates a `baseEach` or `baseEachRight` function.
  4. *
  5. * @private
  6. * @param {Function} eachFunc The function to iterate over a collection.
  7. * @param {boolean} [fromRight] Specify iterating from right to left.
  8. * @returns {Function} Returns the new base function.
  9. */
  10. function createBaseEach(eachFunc, fromRight) {
  11. return function(collection, iteratee) {
  12. if (collection == null) {
  13. return collection;
  14. }
  15. if (!isArrayLike(collection)) {
  16. return eachFunc(collection, iteratee);
  17. }
  18. var length = collection.length,
  19. index = fromRight ? length : -1,
  20. iterable = Object(collection);
  21. while ((fromRight ? index-- : ++index < length)) {
  22. if (iteratee(iterable[index], index, iterable) === false) {
  23. break;
  24. }
  25. }
  26. return collection;
  27. };
  28. }
  29. module.exports = createBaseEach;