baseFind.js 927 B

12345678910111213141516171819202122232425
  1. /**
  2. * The base implementation of `_.find`, `_.findLast`, `_.findKey`, and `_.findLastKey`,
  3. * without support for callback shorthands and `this` binding, which iterates
  4. * over `collection` using the provided `eachFunc`.
  5. *
  6. * @private
  7. * @param {Array|Object|string} collection The collection to search.
  8. * @param {Function} predicate The function invoked per iteration.
  9. * @param {Function} eachFunc The function to iterate over `collection`.
  10. * @param {boolean} [retKey] Specify returning the key of the found element
  11. * instead of the element itself.
  12. * @returns {*} Returns the found element or its key, else `undefined`.
  13. */
  14. function baseFind(collection, predicate, eachFunc, retKey) {
  15. var result;
  16. eachFunc(collection, function(value, key, collection) {
  17. if (predicate(value, key, collection)) {
  18. result = retKey ? key : value;
  19. return false;
  20. }
  21. });
  22. return result;
  23. }
  24. module.exports = baseFind;