Skip to content

Commit 3136da6

Browse files
committed
feat(nullable/map+flatMap): return value unchanged if null or undefined
1 parent 568ad0c commit 3136da6

File tree

3 files changed

+14
-9
lines changed

3 files changed

+14
-9
lines changed

src/Core__Nullable.mjs

+6-4
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,11 @@ function getExn(value) {
3030
}
3131

3232
function map(value, f) {
33-
if (!(value == null)) {
33+
if (value == null) {
34+
return value;
35+
} else {
3436
return Curry._1(f, value);
3537
}
36-
3738
}
3839

3940
function mapWithDefault(value, $$default, f) {
@@ -45,10 +46,11 @@ function mapWithDefault(value, $$default, f) {
4546
}
4647

4748
function flatMap(value, f) {
48-
if (!(value == null)) {
49+
if (value == null) {
50+
return value;
51+
} else {
4952
return Curry._1(f, value);
5053
}
51-
5254
}
5355

5456
export {

src/Core__Nullable.res

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ external getUnsafe: t<'a> => 'a = "%identity"
3131
let map = (value, f) =>
3232
switch value->toOption {
3333
| Some(x) => make(f(x))
34-
| None => undefined
34+
| None => Obj.magic(value)
3535
}
3636

3737
let mapWithDefault = (value, default, f) =>
@@ -43,5 +43,5 @@ let mapWithDefault = (value, default, f) =>
4343
let flatMap = (value, f) =>
4444
switch value->toOption {
4545
| Some(x) => f(x)
46-
| None => undefined
46+
| None => Obj.magic(value)
4747
}

src/Core__Nullable.resi

+6-3
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,8 @@ Nullable.getUnsafe(Nullable.null) // Raises an error
132132
external getUnsafe: t<'a> => 'a = "%identity"
133133

134134
/**
135-
`map(value, f)` returns `f(value)` if `value` is not `null` or `undefined`, otherwise `undefined`.
135+
`map(value, f)` returns `f(value)` if `value` is not `null` or `undefined`,
136+
otherwise returns `value` unchanged.
136137

137138
## Examples
138139

@@ -144,7 +145,8 @@ Nullable.map(Nullable.undefined, x => x * x) // Nullable.undefined
144145
let map: (t<'a>, 'a => 'b) => t<'b>
145146

146147
/**
147-
`mapWithDefault(value, default, f)` returns `f(value)` if `value` is not `null` or `undefined`, otherwise `default`.
148+
`mapWithDefault(value, default, f)` returns `f(value)` if `value` is not `null`
149+
or `undefined`, otherwise returns `default`.
148150

149151
## Examples
150152

@@ -159,7 +161,8 @@ noneValue->Nullable.mapWithDefault(0, x => x + 5) // 0
159161
let mapWithDefault: (t<'a>, 'b, 'a => 'b) => 'b
160162

161163
/**
162-
`flatMap(value, f)` returns `f(value)` if `value` is not `null` or `undefined`, otherwise `undefined`.
164+
`flatMap(value, f)` returns `f(value)` if `value` is not `null` or `undefined`,
165+
otherwise returns `value` unchanged.
163166

164167
## Examples
165168

0 commit comments

Comments
 (0)