Skip to content

Commit 9a60245

Browse files
authored
Merge pull request #5442 from nkrkv/sync-belt-doc-headers-js-option
Sync docs for js_option.ml
2 parents 8d7850c + 19d74fd commit 9a60245

File tree

1 file changed

+117
-1
lines changed

1 file changed

+117
-1
lines changed

jscomp/others/js_option.ml

+117-1
Original file line numberDiff line numberDiff line change
@@ -22,29 +22,85 @@
2222
* along with this program; if not, write to the Free Software
2323
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
2424

25+
(** Provide utilities for handling `option`. *)
26+
27+
(**
28+
`Js.Option.t` is an alias for `option`
29+
*)
2530
type 'a t = 'a option
2631

32+
(**
33+
Wraps the given value in `Some()`
34+
35+
```res example
36+
Js.Option.some(1066) == Some(1066)
37+
```
38+
*)
2739
let some x = Some x
2840

41+
(**
42+
Returns `true` if the argument is `Some(value)`; `false` if the argument is
43+
`None`.
44+
*)
2945
let isSome = function
3046
| None -> false
3147
| Some _ -> true
3248

49+
(**
50+
The first argument to `isSomeValue` is an uncurried function `eq()` that
51+
takes two arguments and returns `true` if they are considered to be equal. It
52+
is used to compare a plain value `v1`(the second argument) with an `option`
53+
value. If the `option` value is `None`, `isSomeValue()` returns `false`; if
54+
the third argument is `Some(v2)`, `isSomeValue()` returns the result of
55+
calling `eq(v1, v2)`.
56+
57+
```res example
58+
let clockEqual = (. a, b) => mod(a, 12) == mod(b, 12)
59+
Js.Option.isSomeValue(clockEqual, 3, Some(15)) == true
60+
Js.Option.isSomeValue(clockEqual, 3, Some(4)) == false
61+
Js.Option.isSomeValue(clockEqual, 3, None) == false
62+
```
63+
*)
3364
let isSomeValue eq v x =
3465
match x with
3566
| None -> false
3667
| Some x -> eq v x [@bs]
3768

38-
69+
(** Returns `true` if the argument is `None`; `false` otherwise. *)
3970
let isNone = function
4071
| None -> true
4172
| Some _ -> false
4273

74+
(**
75+
If the argument to `getExn()` is of the form `Some(value)`, returns `value`.
76+
If given `None`, it throws a `getExn` exception.
77+
*)
4378
let getExn x =
4479
match x with
4580
| None -> Js_exn.raiseError "getExn"
4681
| Some x -> x
4782

83+
(**
84+
The first argument to `equal` is an uncurried function `eq()` that takes two
85+
arguments and returns `true` if they are considered to be equal. The second
86+
and third arguments are `option` values.
87+
88+
If the second and third arguments are of the form:
89+
90+
* `Some(v1)` and `Some(v2)`: returns `eq(v1, v2)`
91+
* `Some(v1)` and `None`: returns `false`
92+
* `None` and `Some(v2)`: returns `false`
93+
* `None` and `None`: returns `true`
94+
95+
```res example
96+
let clockEqual = (. a, b) => mod(a, 12) == mod(b, 12)
97+
Js.Option.equal(clockEqual, Some(3), Some(15)) == true
98+
Js.Option.equal(clockEqual, Some(3), Some(16)) == false
99+
Js.Option.equal(clockEqual, Some(3), None) == false
100+
Js.Option.equal(clockEqual, None, Some(15)) == false
101+
Js.Option.equal(clockEqual, None, None) == true
102+
```
103+
*)
48104
let equal eq a b =
49105
match a with
50106
| None -> b = None
@@ -54,23 +110,73 @@ let equal eq a b =
54110
| Some y -> eq x y [@bs]
55111
end
56112

113+
(**
114+
The first argument to `andThen()` is an uncurried function `f()` that takes a
115+
plain value and returns an `option` result. The second argument is an
116+
`option` value. If the second argument is `None`, the return value is `None`.
117+
If the second argument is `Some(v)`, the return value is `f(v)`.
118+
119+
```res example
120+
let reciprocal = (. x) => x == 0 ? None : Some(1.0 /. Belt.Int.toFloat(x))
121+
Js.Option.andThen(reciprocal, Some(5)) == Some(0.2)
122+
Js.Option.andThen(reciprocal, Some(0)) == None
123+
Js.Option.andThen(reciprocal, None) == None
124+
```
125+
*)
57126
let andThen f x =
58127
match x with
59128
| None -> None
60129
| Some x -> f x [@bs]
61130

131+
(**
132+
The first argument to `map()` is an uncurried function `f()` that takes a
133+
plain value and returns a plain result. The second argument is an `option`
134+
value. If it is of the form `Some(v)`, `map()` returns `Some(f(v))`; if it is
135+
`None`, the return value is `None`, and function `f()` is not called.
136+
137+
```res example
138+
let square = (. x) => x * x
139+
Js.Option.map(square, Some(3)) == Some(9)
140+
Js.Option.map(square, None) == None
141+
```
142+
*)
62143
let map f x =
63144
match x with
64145
| None -> None
65146
| Some x -> Some (f x [@bs])
66147

148+
(**
149+
The first argument to `getWithDefault()` is a default value. If the second
150+
argument is of the form `Some(v)`, `getWithDefault()` returns `v`; if the
151+
second argument is `None`, the return value is the default value.
152+
153+
```res example
154+
Js.Option.getWithDefault(1066, Some(15)) == 15
155+
Js.Option.getWithDefault(1066, None) == 1066
156+
```
157+
*)
67158
let getWithDefault a x =
68159
match x with
69160
| None -> a
70161
| Some x -> x
71162

163+
(** **See:** [getWithDefault](#getWithDefault) *)
72164
let default = getWithDefault
73165

166+
(**
167+
The first argument to `filter()` is an uncurried function that takes a plain
168+
value and returns a boolean. The second argument is an `option` value.
169+
170+
If the second argument is of the form `Some(v)` and `f(v)` is `true`,
171+
the return value is `Some(v)`. Otherwise, the return value is `None`.
172+
173+
```res example
174+
let isEven = (. x) => mod(x, 2) == 0
175+
Js.Option.filter(isEven, Some(2)) == Some(2)
176+
Js.Option.filter(isEven, Some(3)) == None
177+
Js.Option.filter(isEven, None) == None
178+
```
179+
*)
74180
let filter f x =
75181
match x with
76182
| None -> None
@@ -80,6 +186,16 @@ let filter f x =
80186
else
81187
None
82188

189+
(**
190+
The `firstSome()` function takes two `option` values; if the first is of the form `Some(v1)`, that is the return value. Otherwise, `firstSome()` returns the second value.
191+
192+
```res example
193+
Js.Option.firstSome(Some("one"), Some("two")) == Some("one")
194+
Js.Option.firstSome(Some("one"), None) == Some("one")
195+
Js.Option.firstSome(None, Some("two")) == Some("two")
196+
Js.Option.firstSome(None, None) == None
197+
```
198+
*)
83199
let firstSome a b =
84200
match (a, b) with
85201
| (Some _, _) -> a

0 commit comments

Comments
 (0)