-
Notifications
You must be signed in to change notification settings - Fork 7
/
eqProps.ts
42 lines (34 loc) · 1.33 KB
/
eqProps.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
// Copyright (c) 2020 Jozty. All rights reserved. MIT license.
import type { Obj, PH } from './utils/types.ts';
import curryN from './utils/curry_n.ts';
import { equals } from './equals.ts';
import type { Prop } from './prop.ts';
// @types
type EqProps_1<T> = (prop: Prop) => boolean;
type EqProps_3<T> = (obj2: Obj<T>) => boolean;
type EqProps_2_3 =
& (<T>(obj1: Obj<T>) => EqProps_3<T>)
& (<T>(obj1: Obj<T>, obj2: Obj<T>) => boolean);
type EqProps_1_3<T> =
& ((prop: Prop) => EqProps_3<T>)
& ((prop: PH, obj2: Obj<T>) => EqProps_1<T>)
& ((prop: Prop, obj2: Obj<T>) => boolean);
type EqProps =
& ((prop: Prop) => EqProps_2_3)
& (<T>(prop: PH, obj1: Obj<T>) => EqProps_1_3<T>)
& (<T>(prop: Prop, obj1: Obj<T>) => EqProps_3<T>)
& (<T>(prop: PH, obj1: Obj<T>, obj2: Obj<T>) => EqProps_1<T>)
& (<T>(prop: Prop, obj1: Obj<T>, obj2: Obj<T>) => boolean);
function _eqProps<T>(prop: Prop, obj1: Obj<T>, obj2: Obj<T>) {
return equals(obj1[prop], obj2[prop]);
}
/**
* Reports whether two objects have the same value, for the specified property.
* Useful as a curried predicate.
*
* const obj1 = { a: 1, b: 2, c: 3, d: 4 }
* const obj2 = { a: 10, b: 20, c: 3, d: 40 }
* Fae.eqProps('a', obj1, obj2) //=> false
* Fae.eqProps('c', obj1, obj2) //=> true
*/
export const eqProps = curryN(3, _eqProps) as EqProps;