You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

45 lines
1.2 KiB

4 years ago
  1. import _curry2 from "./internal/_curry2.js";
  2. import _dispatchable from "./internal/_dispatchable.js";
  3. import _xfind from "./internal/_xfind.js";
  4. /**
  5. * Returns the first element of the list which matches the predicate, or
  6. * `undefined` if no element matches.
  7. *
  8. * Dispatches to the `find` method of the second argument, if present.
  9. *
  10. * Acts as a transducer if a transformer is given in list position.
  11. *
  12. * @func
  13. * @memberOf R
  14. * @since v0.1.0
  15. * @category List
  16. * @sig (a -> Boolean) -> [a] -> a | undefined
  17. * @param {Function} fn The predicate function used to determine if the element is the
  18. * desired one.
  19. * @param {Array} list The array to consider.
  20. * @return {Object} The element found, or `undefined`.
  21. * @see R.transduce
  22. * @example
  23. *
  24. * const xs = [{a: 1}, {a: 2}, {a: 3}];
  25. * R.find(R.propEq('a', 2))(xs); //=> {a: 2}
  26. * R.find(R.propEq('a', 4))(xs); //=> undefined
  27. */
  28. var find =
  29. /*#__PURE__*/
  30. _curry2(
  31. /*#__PURE__*/
  32. _dispatchable(['find'], _xfind, function find(fn, list) {
  33. var idx = 0;
  34. var len = list.length;
  35. while (idx < len) {
  36. if (fn(list[idx])) {
  37. return list[idx];
  38. }
  39. idx += 1;
  40. }
  41. }));
  42. export default find;