forked from rescript-lang/rescript-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCore__Nullable.res
55 lines (40 loc) · 1.15 KB
/
Core__Nullable.res
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
type t<'a> = Js.Nullable.t<'a>
external null: t<'a> = "#null"
external undefined: t<'a> = "#undefined"
external make: 'a => t<'a> = "%identity"
external toOption: t<'a> => option<'a> = "#nullable_to_opt"
let fromOption: option<'a> => t<'a> = option =>
switch option {
| Some(x) => make(x)
| None => undefined
}
let equal = (a, b, eq) => Core__Option.equal(a->toOption, b->toOption, eq)
let compare = (a, b, cmp) => Core__Option.compare(a->toOption, b->toOption, cmp)
let getOr = (value, default) =>
switch value->toOption {
| Some(x) => x
| None => default
}
let getWithDefault = getOr
let getExn: t<'a> => 'a = value =>
switch value->toOption {
| Some(x) => x
| None => raise(Invalid_argument("Nullable.getExn: value is null or undefined"))
}
external getUnsafe: t<'a> => 'a = "%identity"
let map = (value, f) =>
switch value->toOption {
| Some(x) => make(f(x))
| None => Obj.magic(value)
}
let mapOr = (value, default, f) =>
switch value->toOption {
| Some(x) => f(x)
| None => default
}
let mapWithDefault = mapOr
let flatMap = (value, f) =>
switch value->toOption {
| Some(x) => f(x)
| None => Obj.magic(value)
}