-
Notifications
You must be signed in to change notification settings - Fork 7
/
propOr.ts
57 lines (47 loc) · 1.73 KB
/
propOr.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
54
55
56
57
// 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 { pathOr } from './pathOr.ts';
// @types
type PropOr_1<T> = <R>(d: R) => R;
type PropOr_2<T, R> = (p: Prop) => R;
type PropOr_3<R> = <T>(obj: Obj<T> | null) => R;
type PropOr_2_3<R> =
& ((p: Prop) => PropOr_3<R>)
& (<T>(p: PH, obj: Obj<T> | null) => PropOr_2<T, R>)
& (<T>(p: Prop, obj: Obj<T> | null) => R);
type PropOr_1_3 =
& (<R>(d: R) => PropOr_3<R>)
& (<T>(d: PH, obj: Obj<T> | null) => PropOr_1<T>)
& (<T, R>(d: R, obj: Obj<T> | null) => R);
type PropOr_1_2<T> =
& (<R>(d: R) => PropOr_2<T, R>)
& ((d: PH, p: Prop) => PropOr_1<T>)
& (<R>(d: R, p: Prop) => R);
type PropOr =
& (<R>(d: R) => PropOr_2_3<R>)
& ((d: PH, p: Prop) => PropOr_1_3)
& (<T>(d: PH, p: PH, obj: Obj<T> | null) => PropOr_1_2<T>)
& (<R>(d: R, p: Prop) => PropOr_3<R>)
& (<T, R>(d: R, p: PH, obj: Obj<T> | null) => PropOr_2<T, R>)
& (<T>(d: PH, p: Prop, obj: Obj<T> | null) => PropOr_1<T>)
& (<T, R extends T>(d: R, p: Prop, obj: Obj<T> | null) => R);
function _propOr<T, R>(d: R, p: Prop, obj: Obj<T> | null) {
return pathOr<T, R, T>(d, [p], obj);
}
/**
* If the given, non-null object has an own property with the specified name,
* returns the value of that property. Otherwise returns the provided default
* value.
* const fae = {
* name: 'Fae',
* age: 15
* }
* const Great = Fae.prop('GreatLibrary')
* const GreatWithDefault = Fae.propOr('FaeModule', 'GreatLibrary')
*
* Great(fae) //=> undefined
* GreatWithDefault(fae) //=> 'FaeModule'
*/
export const propOr = curryN(3, _propOr) as PropOr;