Skip to content

Commit bdcccdd

Browse files
authored
Merge branch 'main' into feat/panic
2 parents 0f6d116 + 847e916 commit bdcccdd

File tree

7 files changed

+365
-3
lines changed

7 files changed

+365
-3
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
- Remove `Date.valueOf` as it returns the same as `Date.getTime`. https://github.com/rescript-association/rescript-core/pull/61
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
20+
- 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
2022
- Add `panic`/`Error.panic` and use that where appropriate: https://github.com/rescript-association/rescript-core/pull/72
2123

2224
### Documentation

src/Core__Null.mjs

Lines changed: 49 additions & 0 deletions
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

Lines changed: 32 additions & 0 deletions
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

Lines changed: 101 additions & 2 deletions
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.mjs

Lines changed: 49 additions & 0 deletions
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) {
@@ -9,7 +10,55 @@ function fromOption(option) {
910

1011
}
1112

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

src/Core__Nullable.res

Lines changed: 32 additions & 0 deletions
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 => undefined
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("Nullable.getExn: value is null or undefined"))
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 => Obj.magic(value)
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 => Obj.magic(value)
47+
}

0 commit comments

Comments
 (0)