@@ -81,3 +81,99 @@ let asNullable = optString->Nullable.fromOption // Nullable.t<string>
81
81
```
82
82
*/
83
83
let fromOption: option<'a> => t<'a>
84
+
85
+ /**
86
+ `getWithDefault(value, default)` returns `value` if not `null` or `undefined`,
87
+ otherwise return `default`.
88
+
89
+ ## Examples
90
+
91
+ ```rescript
92
+ Nullable.getWithDefault(Nullable.null, "Banana") // Banana
93
+ Nullable.getWithDefault(Nulalble.make("Apple"), "Banana") // Apple
94
+
95
+ let greet = (firstName: option<string>) =>
96
+ "Greetings " ++ firstName->Nullable.getWithDefault("Anonymous")
97
+
98
+ Nullable.make("Jane")->greet // "Greetings Jane"
99
+ Nullable.null->greet // "Greetings Anonymous"
100
+ ```
101
+ */
102
+ let getWithDefault: (t<'a>, 'a) => 'a
103
+
104
+ /**
105
+ `getExn(value)` raises an exception if `null` or `undefined`, otherwise returns the value.
106
+
107
+ ```rescript
108
+ Nullable.getExn(Nullable.make(3)) // 3
109
+ Nullable.getExn(Nullable.null) /* Raises an Error */
110
+ ```
111
+
112
+ ## Exceptions
113
+
114
+ - Raises `Invalid_argument` if `value` is `null` or `undefined`
115
+ */
116
+ let getExn: t<'a> => 'a
117
+
118
+ /**
119
+ `getUnsafe(value)` returns `value`.
120
+
121
+ ## Examples
122
+
123
+ ```rescript
124
+ Nullable.getUnsafe(Nullable.make(3)) == 3
125
+ Nullable.getUnsafe(Nullable.null) // Raises an error
126
+ ```
127
+
128
+ ## Important
129
+
130
+ - This is an unsafe operation, it assumes `value` is not `null` or `undefined`.
131
+ */
132
+ external getUnsafe: t<'a> => 'a = "%identity"
133
+
134
+ /**
135
+ `map(value, f)` returns `f(value)` if `value` is not `null` or `undefined`, otherwise `undefined`.
136
+
137
+ ## Examples
138
+
139
+ ```rescript
140
+ Nullable.map(Nullable.make(3), x => x * x) // Nullable.make(9)
141
+ Nullable.map(Nullable.undefined, x => x * x) // Nullable.undefined
142
+ ```
143
+ */
144
+ let map: (t<'a>, 'a => 'b) => t<'b>
145
+
146
+ /**
147
+ `mapWithDefault(value, default, f)` returns `f(value)` if `value` is not `null` or `undefined`, otherwise `default`.
148
+
149
+ ## Examples
150
+
151
+ ```rescript
152
+ let someValue = Nullable.make(3)
153
+ someValue->Nullable.mapWithDefault(0, x => x + 5) // 8
154
+
155
+ let noneValue = Nullable.null
156
+ noneValue->Nullable.mapWithDefault(0, x => x + 5) // 0
157
+ ```
158
+ */
159
+ let mapWithDefault: (t<'a>, 'b, 'a => 'b) => 'b
160
+
161
+ /**
162
+ `flatMap(value, f)` returns `f(x)` if `value` is not `null` or `undefined`, otherwise `undefined`.
163
+
164
+ ## Examples
165
+
166
+ ```rescript
167
+ let addIfAboveOne = value =>
168
+ if (value > 1) {
169
+ Nullable.make(value + 1)
170
+ } else {
171
+ Nullable.null
172
+ }
173
+
174
+ Nullable.flatMap(Nullable.make(2), addIfAboveOne) // Nullable.make(3)
175
+ Nullable.flatMap(Nullable.make(-4), addIfAboveOne) // Nullable.undefined
176
+ Nullable.flatMap(Nullable.null, addIfAboveOne) // Nullable.undefined
177
+ ```
178
+ */
179
+ let flatMap: (t<'a>, 'a => t<'b>) => t<'b>
0 commit comments