-
Notifications
You must be signed in to change notification settings - Fork 7
/
findLast.ts
37 lines (30 loc) · 1.23 KB
/
findLast.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// Copyright (c) 2020 Jozty. All rights reserved. MIT license.
import type { PH, Predicate1 } from './utils/types.ts';
import curryN from './utils/curry_n.ts';
import { dispatch } from './utils/dispatch.ts';
import FindLastTransformer from './utils/Transformers/findLast.ts';
// @types
type FindLast_2<T> = (list: T[]) => T | undefined;
type FindLast_1<T> = (predicate: Predicate1<T>) => T | undefined;
type FindLast =
& (<T>(predicate: Predicate1<T>) => FindLast_2<T>)
& (<T>(predicate: PH, list: T[]) => FindLast_1<T>)
& (<T>(predicate: Predicate1<T>, list: T[]) => T | undefined);
function _findLast<T>(predicate: Predicate1<T>, list: T[]) {
for (let i = list.length - 1; i >= 0; i--) {
if (predicate(list[i])) return list[i];
}
return void 0;
}
const dispatched = dispatch(FindLastTransformer, _findLast);
/**
* Returns the last element of the list which matches the predicate, or
* `undefined` if no element matches.
*
* Acts as a transducer if a transformer is passed in place of `list`
*
* const xs = [{a: 1, b: 0}, {a:1, b: 1}]
* Fae.findLast(Fae.propEq('a', 1))(xs) //=> {a: 1, b: 1}
* Fae.findLast(Fae.propEq('a', 4))(xs) //=> undefined
*/
export const findLast = curryN(2, dispatched) as FindLast;