main
Last change
on this file was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago |
Initial commit
|
-
Property mode
set to
100644
|
File size:
1.2 KB
|
Line | |
---|
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(2, 'a'))(xs); //=> {a: 2}
|
---|
26 | * R.find(R.propEq(4, 'a'))(xs); //=> undefined
|
---|
27 | */
|
---|
28 |
|
---|
29 | var find =
|
---|
30 | /*#__PURE__*/
|
---|
31 | _curry2(
|
---|
32 | /*#__PURE__*/
|
---|
33 | _dispatchable(['find'], _xfind, function find(fn, list) {
|
---|
34 | var idx = 0;
|
---|
35 | var len = list.length;
|
---|
36 |
|
---|
37 | while (idx < len) {
|
---|
38 | if (fn(list[idx])) {
|
---|
39 | return list[idx];
|
---|
40 | }
|
---|
41 |
|
---|
42 | idx += 1;
|
---|
43 | }
|
---|
44 | }));
|
---|
45 |
|
---|
46 | export default find; |
---|
Note:
See
TracBrowser
for help on using the repository browser.