createBaseEach.js 913 B

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