-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathCore__Null.res
53 lines (39 loc) · 1.11 KB
/
Core__Null.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
type t<'a> = Js.Null.t<'a>
external asNullable: t<'a> => Core__Nullable.t<'a> = "%identity"
external null: t<'a> = "#null"
external make: 'a => t<'a> = "%identity"
external toOption: t<'a> => option<'a> = "#null_to_opt"
let fromOption: option<'a> => t<'a> = option =>
switch option {
| Some(x) => make(x)
| None => null
}
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 getWithDefault = (value, default) =>
switch value->toOption {
| Some(x) => x
| None => default
}
let getExn: t<'a> => 'a = value =>
switch value->toOption {
| Some(x) => x
| None => raise(Invalid_argument("Null.getExn: value is null"))
}
external getUnsafe: t<'a> => 'a = "%identity"
let map = (value, f) =>
switch value->toOption {
| Some(x) => make(f(x))
| None => null
}
let mapWithDefault = (value, default, f) =>
switch value->toOption {
| Some(x) => f(x)
| None => default
}
let flatMap = (value, f) =>
switch value->toOption {
| Some(x) => f(x)
| None => null
}
let isNull = i => i == null