_createFind.js 910 B

123456789101112131415161718192021222324252627282930
  1. var baseIteratee = require('./_baseIteratee'),
  2. isArrayLike = require('./isArrayLike'),
  3. keys = require('./keys');
  4. /**
  5. * Creates a `_.find` or `_.findLast` function.
  6. *
  7. * @private
  8. * @param {Function} findIndexFunc The function to find the collection index.
  9. * @returns {Function} Returns the new find function.
  10. */
  11. function createFind(findIndexFunc) {
  12. return function(collection, predicate, fromIndex) {
  13. var iterable = Object(collection);
  14. predicate = baseIteratee(predicate, 3);
  15. if (!isArrayLike(collection)) {
  16. var props = keys(collection);
  17. }
  18. var index = findIndexFunc(props || collection, function(value, key) {
  19. if (props) {
  20. key = value;
  21. value = iterable[key];
  22. }
  23. return predicate(value, key, iterable);
  24. }, fromIndex);
  25. return index > -1 ? collection[props ? props[index] : index] : undefined;
  26. };
  27. }
  28. module.exports = createFind;