Skip to content

Commit 8bc2cf4

Browse files
committed
Rename functions ending with Exn to OrThrow
1 parent 66723ba commit 8bc2cf4

File tree

15 files changed

+174
-60
lines changed

15 files changed

+174
-60
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,16 @@
1212
1313
# 12.0.0-alpha.14 (Unreleased)
1414

15+
#### :boom: Breaking Change
16+
17+
- Rename functions ending with `Exn` to end with `OrThrow`. The old `Exn` functions are now deprecated:
18+
- `Bool.fromStringExn``Bool.fromStringOrThrow`
19+
- `BigInt.fromStringExn``BigInt.fromStringOrThrow`
20+
- `JSON.parseExn``JSON.parseOrThrow`
21+
- Changed `BigInt.fromFloat` to return an option rather than throwing an error.
22+
- Added `BigInt.fromFloatOrThrow`
23+
- Old functions remain available but are marked as deprecated with guidance to use the new `OrThrow` variants.
24+
1525
#### :rocket: New Feature
1626

1727
- Add `RegExp.flags`. https://github.com/rescript-lang/rescript/pull/7461

lib/es6/Stdlib_BigInt.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11

22

33

4+
function fromString(value) {
5+
try {
6+
return BigInt(value);
7+
} catch (exn) {
8+
return;
9+
}
10+
}
11+
412
function fromFloat(value) {
513
try {
614
return BigInt(value);
@@ -14,6 +22,7 @@ function toInt(t) {
1422
}
1523

1624
export {
25+
fromString,
1726
fromFloat,
1827
toInt,
1928
}

lib/es6/Stdlib_Bool.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ function fromString(s) {
2020
}
2121
}
2222

23-
function fromStringExn(param) {
23+
function fromStringOrThrow(param) {
2424
switch (param) {
2525
case "false" :
2626
return false;
@@ -29,15 +29,18 @@ function fromStringExn(param) {
2929
default:
3030
throw {
3131
RE_EXN_ID: "Invalid_argument",
32-
_1: "Bool.fromStringExn: value is neither \"true\" nor \"false\"",
32+
_1: "Bool.fromStringOrThrow: value is neither \"true\" nor \"false\"",
3333
Error: new Error()
3434
};
3535
}
3636
}
3737

38+
let fromStringExn = fromStringOrThrow;
39+
3840
export {
3941
toString,
4042
fromString,
43+
fromStringOrThrow,
4144
fromStringExn,
4245
}
4346
/* No side effect */

lib/js/Stdlib_BigInt.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
'use strict';
22

33

4+
function fromString(value) {
5+
try {
6+
return BigInt(value);
7+
} catch (exn) {
8+
return;
9+
}
10+
}
11+
412
function fromFloat(value) {
513
try {
614
return BigInt(value);
@@ -13,6 +21,7 @@ function toInt(t) {
1321
return Number(t) | 0;
1422
}
1523

24+
exports.fromString = fromString;
1625
exports.fromFloat = fromFloat;
1726
exports.toInt = toInt;
1827
/* No side effect */

lib/js/Stdlib_Bool.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ function fromString(s) {
2020
}
2121
}
2222

23-
function fromStringExn(param) {
23+
function fromStringOrThrow(param) {
2424
switch (param) {
2525
case "false" :
2626
return false;
@@ -29,13 +29,16 @@ function fromStringExn(param) {
2929
default:
3030
throw {
3131
RE_EXN_ID: "Invalid_argument",
32-
_1: "Bool.fromStringExn: value is neither \"true\" nor \"false\"",
32+
_1: "Bool.fromStringOrThrow: value is neither \"true\" nor \"false\"",
3333
Error: new Error()
3434
};
3535
}
3636
}
3737

38+
let fromStringExn = fromStringOrThrow;
39+
3840
exports.toString = toString;
3941
exports.fromString = fromString;
42+
exports.fromStringOrThrow = fromStringOrThrow;
4043
exports.fromStringExn = fromStringExn;
4144
/* No side effect */

runtime/Stdlib_BigInt.res

Lines changed: 61 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,39 +6,89 @@ type t = bigint
66
@val external asIntN: (~width: int, bigint) => bigint = "BigInt.asIntN"
77
@val external asUintN: (~width: int, bigint) => bigint = "BigInt.asUintN"
88

9-
@val external fromString: string => bigint = "BigInt"
10-
119
@val
1210
/**
1311
Parses the given `string` into a `bigint` using JavaScript semantics. Return the
14-
number as a `bigint` if successfully parsed. Uncaught syntax exception otherwise.
12+
number as a `bigint` if successfully parsed. Throws a syntax exception otherwise.
1513
1614
## Examples
1715
1816
```rescript
19-
BigInt.fromStringExn("123")->assertEqual(123n)
17+
BigInt.fromStringOrThrow("123")->assertEqual(123n)
2018
21-
BigInt.fromStringExn("")->assertEqual(0n)
19+
BigInt.fromStringOrThrow("")->assertEqual(0n)
2220
23-
BigInt.fromStringExn("0x11")->assertEqual(17n)
21+
BigInt.fromStringOrThrow("0x11")->assertEqual(17n)
2422
25-
BigInt.fromStringExn("0b11")->assertEqual(3n)
23+
BigInt.fromStringOrThrow("0b11")->assertEqual(3n)
2624
27-
BigInt.fromStringExn("0o11")->assertEqual(9n)
25+
BigInt.fromStringOrThrow("0o11")->assertEqual(9n)
2826
2927
/* catch exception */
30-
switch BigInt.fromStringExn("a") {
28+
switch BigInt.fromStringOrThrow("a") {
3129
| exception JsExn(_error) => assert(true)
3230
| _bigInt => assert(false)
3331
}
3432
```
3533
*/
34+
external fromStringOrThrow: string => bigint = "BigInt"
35+
36+
/**
37+
Parses the given `string` into a `bigint` using JavaScript semantics. Returns
38+
`Some(bigint)` if the string can be parsed, `None` otherwise.
39+
40+
## Examples
41+
42+
```rescript
43+
BigInt.fromString("123")->assertEqual(Some(123n))
44+
45+
BigInt.fromString("")->assertEqual(Some(0n))
46+
47+
BigInt.fromString("0x11")->assertEqual(Some(17n))
48+
49+
BigInt.fromString("0b11")->assertEqual(Some(3n))
50+
51+
BigInt.fromString("0o11")->assertEqual(Some(9n))
52+
53+
BigInt.fromString("invalid")->assertEqual(None)
54+
```
55+
*/
56+
let fromString = (value: string) => {
57+
try Some(fromStringOrThrow(value)) catch {
58+
| _ => None
59+
}
60+
}
61+
62+
@deprecated("Use `fromStringOrThrow` instead") @val
3663
external fromStringExn: string => bigint = "BigInt"
64+
3765
@val external fromInt: int => bigint = "BigInt"
38-
@val external fromFloat: float => bigint = "BigInt"
66+
67+
@val
68+
/**
69+
Converts a `float` to a `bigint` using JavaScript semantics.
70+
Throws an exception if the float is not an integer or is infinite/NaN.
71+
72+
## Examples
73+
74+
```rescript
75+
BigInt.fromFloatOrThrow(123.0)->assertEqual(123n)
76+
77+
BigInt.fromFloatOrThrow(0.0)->assertEqual(0n)
78+
79+
BigInt.fromFloatOrThrow(-456.0)->assertEqual(-456n)
80+
81+
/* This will throw an exception */
82+
switch BigInt.fromFloatOrThrow(123.5) {
83+
| exception JsExn(_error) => assert(true)
84+
| _bigInt => assert(false)
85+
}
86+
```
87+
*/
88+
external fromFloatOrThrow: float => bigint = "BigInt"
3989

4090
let fromFloat = (value: float) => {
41-
try Some(fromFloat(value)) catch {
91+
try Some(fromFloatOrThrow(value)) catch {
4292
| _ => None
4393
}
4494
}

runtime/Stdlib_Bool.res

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,16 @@ let fromString = s => {
1515
}
1616
}
1717

18-
let fromStringExn = param =>
18+
let fromStringOrThrow = param =>
1919
switch param {
2020
| "true" => true
2121
| "false" => false
22-
| _ => throw(Invalid_argument(`Bool.fromStringExn: value is neither "true" nor "false"`))
22+
| _ => throw(Invalid_argument(`Bool.fromStringOrThrow: value is neither "true" nor "false"`))
2323
}
2424

25+
@deprecated("Use `fromStringOrThrow` instead")
26+
let fromStringExn = fromStringOrThrow
27+
2528
external compare: (bool, bool) => Stdlib_Ordering.t = "%compare"
2629

2730
external equal: (bool, bool) => bool = "%equal"

runtime/Stdlib_Bool.resi

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,22 @@ Bool.fromString("notAValidBoolean")->assertEqual(None)
3131
*/
3232
let fromString: string => option<bool>
3333

34+
/**
35+
Converts a string to a boolean.
36+
Throws an `Invalid_argument` exception if the string is not a valid boolean.
37+
38+
## Examples
39+
```rescript
40+
Bool.fromStringOrThrow("true")->assertEqual(true)
41+
Bool.fromStringOrThrow("false")->assertEqual(false)
42+
switch Bool.fromStringOrThrow("notAValidBoolean") {
43+
| exception Invalid_argument(_) => assert(true)
44+
| _ => assert(false)
45+
}
46+
```
47+
*/
48+
let fromStringOrThrow: string => bool
49+
3450
/**
3551
Converts a string to a boolean.
3652
Beware, this function will throw an `Invalid_argument` exception
@@ -46,6 +62,7 @@ switch Bool.fromStringExn("notAValidBoolean") {
4662
}
4763
```
4864
*/
65+
@deprecated("Use `fromStringOrThrow` instead")
4966
let fromStringExn: string => bool
5067

5168
external compare: (bool, bool) => Stdlib_Ordering.t = "%compare"

runtime/Stdlib_JSON.res

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ type rec t =
1010
@unboxed
1111
type replacer = Keys(array<string>) | Replacer((string, t) => t)
1212

13-
@raises @val external parseExn: (string, ~reviver: (string, t) => t=?) => t = "JSON.parse"
14-
@deprecated("Use `parseExn` with optional parameter instead") @raises @val
15-
external parseExnWithReviver: (string, (string, t) => t) => t = "JSON.parse"
13+
@raises @val external parseOrThrow: (string, ~reviver: (string, t) => t=?) => t = "JSON.parse"
14+
15+
@deprecated("Use `parseOrThrow` instead") @raises @val
16+
external parseExn: (string, ~reviver: (string, t) => t=?) => t = "JSON.parse"
1617

1718
@val external stringify: (t, ~replacer: replacer=?, ~space: int=?) => string = "JSON.stringify"
1819
@deprecated("Use `stringify` with optional parameter instead") @val

0 commit comments

Comments
 (0)