-
Notifications
You must be signed in to change notification settings - Fork 7
/
propSatisfies.ts
52 lines (42 loc) · 1.72 KB
/
propSatisfies.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Copyright (c) 2020 Jozty. All rights reserved. MIT license.
import type { Obj, PH, Predicate1 } from './utils/types.ts';
import type { Prop } from './prop.ts';
import curryN from './utils/curry_n.ts';
// @types
type PropSatisfies_1<T> = (pred: Predicate1<T>) => boolean;
type PropSatisfies_2<T> = (name: Prop) => boolean;
type PropSatisfies_3<T> = (obj: Obj<T>) => boolean;
type PropSatisfies_2_3<T> =
& ((name: Prop) => PropSatisfies_3<T>)
& ((name: PH, obj: Obj<T>) => PropSatisfies_2<T>)
& ((name: Prop, obj: Obj<T>) => boolean);
type PropSatisfies_1_3 =
& (<T>(pred: Predicate1<T>) => PropSatisfies_3<T>)
& (<T>(pred: PH, obj: Obj<T>) => PropSatisfies_1<T>)
& (<T>(pred: Predicate1<T>, obj: Obj<T>) => boolean);
type PropSatisfies_1_2<T> =
& ((pred: Predicate1<T>) => PropSatisfies_2<T>)
& ((pred: PH, name: Prop) => PropSatisfies_1<T>)
& ((pred: Predicate1<T>, name: Prop) => boolean);
type PropSatisfies =
& (<T>(pred: Predicate1<T>) => PropSatisfies_2_3<T>)
& ((pred: PH, name: Prop) => PropSatisfies_1_3)
& (<T>(pred: PH, name: PH, obj: Obj<T>) => PropSatisfies_1_2<T>)
& (<T>(pred: Predicate1<T>, name: Prop) => PropSatisfies_3<T>)
& (<T>(pred: Predicate1<T>, name: PH, obj: Obj<T>) => PropSatisfies_2<T>)
& (<T>(pred: PH, name: Prop, obj: Obj<T>) => PropSatisfies_1<T>)
& (<T>(pred: Predicate1<T>, name: Prop, obj: Obj<T>) => boolean);
function _propSatisfies<T>(
pred: Predicate1<T>,
name: Prop,
obj: Obj<T>,
) {
return pred(obj[name]);
}
/**
* Returns `true` if the specified object property satisfies the given
* predicate; `false` otherwise.
*
* Fae.propSatisfies(x => x > 0, 'x', {x: 1, y: 2}); //=> true
*/
export const propSatisfies = curryN(3, _propSatisfies) as PropSatisfies;