Skip to content

Commit 60dda55

Browse files
committed
feat(nullable): add getWithDefault
1 parent 43d7baf commit 60dda55

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

src/Core__Nullable.mjs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@ function fromOption(option) {
99

1010
}
1111

12+
function getWithDefault(value, $$default) {
13+
if (value == null) {
14+
return $$default;
15+
} else {
16+
return value;
17+
}
18+
}
19+
1220
function getExn(value) {
1321
if (!(value == null)) {
1422
return value;
@@ -22,6 +30,7 @@ function getExn(value) {
2230

2331
export {
2432
fromOption ,
33+
getWithDefault ,
2534
getExn ,
2635
}
2736
/* No side effect */

src/Core__Nullable.res

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ let fromOption: option<'a> => t<'a> = option =>
1414
| None => undefined
1515
}
1616

17+
let getWithDefault = (value, default) =>
18+
switch value->toOption {
19+
| Some(x) => x
20+
| None => default
21+
}
22+
1723
let getExn: t<'a> => 'a = value =>
1824
switch value->toOption {
1925
| Some(x) => x

0 commit comments

Comments
 (0)