@@ -27,7 +27,7 @@ See [`null`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/O
27
27
28
28
## Examples
29
29
```rescript
30
- Console.log(Null. null) // Logs `null` to the console.
30
+ Console.log(null) // Logs `null` to the console.
31
31
```
32
32
*/
33
33
external null : t <'a > = "#null"
@@ -67,7 +67,106 @@ Turns an `option` into a `Null.t`. `None` will be converted to `null`.
67
67
```rescript
68
68
let optString: option<string> = None
69
69
let asNull = optString->Null.fromOption // Null.t<string>
70
- Console.log(asNull == Null. null) // Logs `true` to the console.
70
+ Console.log(asNull == null) // Logs `true` to the console.
71
71
```
72
72
*/
73
73
let fromOption : option <'a > => t <'a >
74
+
75
+ /**
76
+ `getWithDefault(value, default)` returns `value` if not `null`, otherwise return
77
+ `default`.
78
+
79
+ ## Examples
80
+
81
+ ```rescript
82
+ Null.getWithDefault(null, "Banana") // Banana
83
+ Null.getWithDefault(Nulalble.make("Apple"), "Banana") // Apple
84
+
85
+ let greet = (firstName: option<string>) =>
86
+ "Greetings " ++ firstName->Null.getWithDefault("Anonymous")
87
+
88
+ Null.make("Jane")->greet // "Greetings Jane"
89
+ null->greet // "Greetings Anonymous"
90
+ ```
91
+ */
92
+ let getWithDefault : (t <'a >, 'a ) => 'a
93
+
94
+ /**
95
+ `getExn(value)` raises an exception if `null`, otherwise returns the value.
96
+
97
+ ```rescript
98
+ Null.getExn(Null.make(3)) // 3
99
+ Null.getExn(null) /* Raises an Error */
100
+ ```
101
+
102
+ ## Exceptions
103
+
104
+ - Raises `Invalid_argument` if `value` is `null`,
105
+ */
106
+ let getExn : t <'a > => 'a
107
+
108
+ /**
109
+ `getUnsafe(value)` returns `value`.
110
+
111
+ ## Examples
112
+
113
+ ```rescript
114
+ Null.getUnsafe(Null.make(3)) == 3
115
+ Null.getUnsafe(null) // Raises an error
116
+ ```
117
+
118
+ ## Important
119
+
120
+ - This is an unsafe operation, it assumes `value` is not `null`.
121
+ */
122
+ external getUnsafe : t <'a > => 'a = "%identity"
123
+
124
+ /**
125
+ `map(value, f)` returns `f(value)` if `value` is not `null`, otherwise returns
126
+ `value` unchanged.
127
+
128
+ ## Examples
129
+
130
+ ```rescript
131
+ Null.map(Null.make(3), x => x * x) // Null.make(9)
132
+ Null.map(null, x => x * x) // null
133
+ ```
134
+ */
135
+ let map : (t <'a >, 'a => 'b ) => t <'b >
136
+
137
+ /**
138
+ `mapWithDefault(value, default, f)` returns `f(value)` if `value` is not `null`,
139
+ otherwise returns `default`.
140
+
141
+ ## Examples
142
+
143
+ ```rescript
144
+ let someValue = Null.make(3)
145
+ someValue->Null.mapWithDefault(0, x => x + 5) // 8
146
+
147
+ let noneValue = null
148
+ noneValue->Null.mapWithDefault(0, x => x + 5) // 0
149
+ ```
150
+ */
151
+ let mapWithDefault : (t <'a >, 'b , 'a => 'b ) => 'b
152
+
153
+ /**
154
+ `flatMap(value, f)` returns `f(value)` if `value` is not `null`, otherwise
155
+ returns `value` unchanged.
156
+
157
+ ## Examples
158
+
159
+ ```rescript
160
+ let addIfAboveOne = value =>
161
+ if (value > 1) {
162
+ Null.make(value + 1)
163
+ } else {
164
+ null
165
+ }
166
+
167
+ Null.flatMap(Null.make(2), addIfAboveOne) // Null.make(3)
168
+ Null.flatMap(Null.make(-4), addIfAboveOne) // null
169
+ Null.flatMap(null, addIfAboveOne) // null
170
+ ```
171
+ */
172
+ let flatMap : (t <'a >, 'a => t <'b >) => t <'b >
0 commit comments