_createFind.js 853B

1234567891011121314151617181920212223242526
  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. if (!isArrayLike(collection)) {
  15. var iteratee = baseIteratee(predicate, 3);
  16. collection = keys(collection);
  17. predicate = function(key) { return iteratee(iterable[key], key, iterable); };
  18. }
  19. var index = findIndexFunc(collection, predicate, fromIndex);
  20. return index > -1 ? iterable[iteratee ? collection[index] : index] : undefined;
  21. };
  22. }
  23. module.exports = createFind;