-
Notifications
You must be signed in to change notification settings - Fork 7
/
propIs.ts
51 lines (41 loc) · 1.59 KB
/
propIs.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
// 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 { is } from './utils/is.ts';
// @types
type PropIs_1<T> = (type: string) => boolean;
type PropIs_2<T> = (name: Prop) => boolean;
type PropIs_3 = <T>(obj: Obj<T>) => boolean;
type PropIs_2_3 =
& ((name: Prop) => PropIs_3)
& (<T>(name: PH, obj: Obj<T>) => PropIs_2<T>)
& (<T>(name: Prop, obj: Obj<T>) => boolean);
type PropIs_1_3 =
& ((type: string) => PropIs_3)
& (<T>(type: PH, obj: Obj<T>) => PropIs_1<T>)
& (<T>(type: string, obj: Obj<T>) => boolean);
type PropIs_1_2<T> =
& ((type: string) => PropIs_2<T>)
& ((type: PH, name: Prop) => PropIs_1<T>)
& ((type: string, name: Prop) => boolean);
type PropIs =
& ((type: string) => PropIs_2_3)
& ((type: PH, name: Prop) => PropIs_1_3)
& (<T>(type: PH, name: PH, obj: Obj<T>) => PropIs_1_2<T>)
& ((type: string, name: Prop) => PropIs_3)
& (<T>(type: string, name: PH, obj: Obj<T>) => PropIs_2<T>)
& (<T>(type: PH, name: Prop, obj: Obj<T>) => PropIs_1<T>)
& (<T>(type: string, name: Prop, obj: Obj<T>) => boolean);
function _propIs<T>(type: string, name: Prop, obj: Obj<T>) {
return is(obj[name], type);
}
/**
* Returns `true` if the specified object property is of the given type;
* `false` otherwise.
*
* Fae.propIs('Number', 'a', {a: 1, y: 2}); //=> true
* Fae.propIs('String', 'a', {a: 'foo'}); //=> true
* Fae.propIs('Number', 'a', {}); //=> false
*/
export const propIs = curryN(3, _propIs) as PropIs;