UNPKG

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