UNPKG

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