-
Notifications
You must be signed in to change notification settings - Fork 7
/
propEq.ts
53 lines (43 loc) · 1.75 KB
/
propEq.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
53
// Copyright (c) 2020 Jozty. All rights reserved. MIT license.
import curryN from './utils/curry_n.ts';
import type { Obj, PH } from './utils/types.ts';
import type { Prop } from './prop.ts';
import { equals } from './equals.ts';
// @types
type PropEq_1 = (name: Prop) => boolean;
type PropEq_2<T> = (val: T) => boolean;
type PropEq_3<T1> = <T>(obj: Obj<T | T1>) => boolean;
type PropEq_2_3 =
& (<T>(val: T) => PropEq_3<T>)
& (<T>(val: PH, obj: Obj<T>) => PropEq_2<T>)
& (<T>(val: T, obj: Obj<T>) => boolean);
type PropEq_1_3<T1> =
& ((name: Prop) => PropEq_3<T1>)
& (<T>(name: PH, obj: Obj<T | T1>) => PropEq_1)
& (<T>(name: Prop, obj: Obj<T | T1>) => boolean);
type PropEq_1_2<T> =
& ((name: Prop) => PropEq_2<T>)
& ((name: PH, val: T) => PropEq_1)
& ((name: Prop, val: T) => boolean);
type PropEq =
& ((name: Prop) => PropEq_2_3)
& (<T>(name: PH, val: T) => PropEq_1_3<T>)
& (<T>(name: PH, val: PH, obj: Obj<T>) => PropEq_1_2<T>)
& (<T>(name: Prop, val: T) => PropEq_3<T>)
& (<T>(name: Prop, val: PH, obj: Obj<T>) => PropEq_2<T>)
& (<T>(name: PH, val: T, obj: Obj<T>) => PropEq_1)
& (<T>(name: Prop, val: T, obj: Obj<T>) => boolean);
function _propEq<T>(name: Prop, val: T, obj: Obj<T>) {
return equals(val, obj[name]);
}
/**
* Returns `true` if the specified object property is equal, to the given value; `false` otherwise.
*
* const shivam = {name: 'shivam', age: 20, hair: 'brown'}
* const shubham = {name: 'shubham', age: 22, hair: 'black'}
* const Krish = {name: 'krish', age: 25, hair: 'black'}
* const students = [shivam, shubham, krish]
* const hasBrownHair = Fae.propEq('hair', 'brown')
* Fae.filter(hasBrownHair, students) //=> [shubham]
*/
export const propEq = curryN(3, _propEq) as PropEq;