Skip to content

Commit 847e916

Browse files
authored
Null additions (#73)
* feat(null): add getWithDefault, getExn, getUnsafe, map, mapWithDefault, flatMap * docs(null): Null.null -> null * docs(nullable): Nullable.undefined -> undefined * docs(changelog): add changelog entry for Null additions
1 parent 3a8de61 commit 847e916

File tree

5 files changed

+187
-6
lines changed

5 files changed

+187
-6
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
- Change `Float.fromString` signature. Now accepts only string. https://github.com/rescript-association/rescript-core/pull/54
1919
- Change `Float.parseFloat` signature. Now accepts only string. https://github.com/rescript-association/rescript-core/pull/54
2020
- Add `getExn`, `getUnsafe`, `getWithDefault`, `map`, `mapWithDefault` and `flatMap` to `Nullable`. https://github.com/rescript-association/rescript-core/pull/67
21+
- Add `getExn`, `getUnsafe`, `getWithDefault`, `map`, `mapWithDefault` and `flatMap` to `Null`. https://github.com/rescript-association/rescript-core/pull/73
2122

2223
### Documentation
2324

src/Core__Null.mjs

+49
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Generated by ReScript, PLEASE EDIT WITH CARE
22

3+
import * as Curry from "rescript/lib/es6/curry.js";
34
import * as Caml_option from "rescript/lib/es6/caml_option.js";
45

56
function fromOption(option) {
@@ -10,7 +11,55 @@ function fromOption(option) {
1011
}
1112
}
1213

14+
function getWithDefault(value, $$default) {
15+
if (value !== null) {
16+
return value;
17+
} else {
18+
return $$default;
19+
}
20+
}
21+
22+
function getExn(value) {
23+
if (value !== null) {
24+
return value;
25+
}
26+
throw {
27+
RE_EXN_ID: "Invalid_argument",
28+
_1: "Null.getExn: value is null",
29+
Error: new Error()
30+
};
31+
}
32+
33+
function map(value, f) {
34+
if (value !== null) {
35+
return Curry._1(f, value);
36+
} else {
37+
return null;
38+
}
39+
}
40+
41+
function mapWithDefault(value, $$default, f) {
42+
if (value !== null) {
43+
return Curry._1(f, value);
44+
} else {
45+
return $$default;
46+
}
47+
}
48+
49+
function flatMap(value, f) {
50+
if (value !== null) {
51+
return Curry._1(f, value);
52+
} else {
53+
return null;
54+
}
55+
}
56+
1357
export {
1458
fromOption ,
59+
getWithDefault ,
60+
getExn ,
61+
map ,
62+
mapWithDefault ,
63+
flatMap ,
1564
}
1665
/* No side effect */

src/Core__Null.res

+32
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,35 @@ let fromOption: option<'a> => t<'a> = option =>
1313
| Some(x) => make(x)
1414
| None => null
1515
}
16+
17+
let getWithDefault = (value, default) =>
18+
switch value->toOption {
19+
| Some(x) => x
20+
| None => default
21+
}
22+
23+
let getExn: t<'a> => 'a = value =>
24+
switch value->toOption {
25+
| Some(x) => x
26+
| None => raise(Invalid_argument("Null.getExn: value is null"))
27+
}
28+
29+
external getUnsafe: t<'a> => 'a = "%identity"
30+
31+
let map = (value, f) =>
32+
switch value->toOption {
33+
| Some(x) => make(f(x))
34+
| None => null
35+
}
36+
37+
let mapWithDefault = (value, default, f) =>
38+
switch value->toOption {
39+
| Some(x) => f(x)
40+
| None => default
41+
}
42+
43+
let flatMap = (value, f) =>
44+
switch value->toOption {
45+
| Some(x) => f(x)
46+
| None => null
47+
}

src/Core__Null.resi

+101-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ See [`null`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/O
2727

2828
## Examples
2929
```rescript
30-
Console.log(Null.null) // Logs `null` to the console.
30+
Console.log(null) // Logs `null` to the console.
3131
```
3232
*/
3333
external null: t<'a> = "#null"
@@ -67,7 +67,106 @@ Turns an `option` into a `Null.t`. `None` will be converted to `null`.
6767
```rescript
6868
let optString: option<string> = None
6969
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.
7171
```
7272
*/
7373
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>

src/Core__Nullable.resi

+4-4
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ See [`undefined`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refere
2929

3030
## Examples
3131
```rescript
32-
Console.log(Nullable.undefined) // Logs `undefined` to the console.
32+
Console.log(undefined) // Logs `undefined` to the console.
3333
```
3434
*/
3535
external undefined: t<'a> = "#undefined"
@@ -139,7 +139,7 @@ otherwise returns `value` unchanged.
139139

140140
```rescript
141141
Nullable.map(Nullable.make(3), x => x * x) // Nullable.make(9)
142-
Nullable.map(Nullable.undefined, x => x * x) // Nullable.undefined
142+
Nullable.map(undefined, x => x * x) // undefined
143143
```
144144
*/
145145
let map: (t<'a>, 'a => 'b) => t<'b>
@@ -175,8 +175,8 @@ let addIfAboveOne = value =>
175175
}
176176

177177
Nullable.flatMap(Nullable.make(2), addIfAboveOne) // Nullable.make(3)
178-
Nullable.flatMap(Nullable.make(-4), addIfAboveOne) // Nullable.undefined
179-
Nullable.flatMap(Nullable.null, addIfAboveOne) // Nullable.undefined
178+
Nullable.flatMap(Nullable.make(-4), addIfAboveOne) // undefined
179+
Nullable.flatMap(Nullable.null, addIfAboveOne) // undefined
180180
```
181181
*/
182182
let flatMap: (t<'a>, 'a => t<'b>) => t<'b>

0 commit comments

Comments
 (0)