@@ -6,39 +6,89 @@ type t = bigint
6
6
@val external asIntN : (~width : int , bigint ) => bigint = "BigInt.asIntN"
7
7
@val external asUintN : (~width : int , bigint ) => bigint = "BigInt.asUintN"
8
8
9
- @val external fromString : string => bigint = "BigInt"
10
-
11
9
/**
12
10
Parses the given `string` into a `bigint` using JavaScript semantics. Return the
13
- number as a `bigint` if successfully parsed. Uncaught syntax exception otherwise.
11
+ number as a `bigint` if successfully parsed. Throws a syntax exception otherwise.
14
12
15
13
## Examples
16
14
17
15
```rescript
18
- BigInt.fromStringExn ("123")->assertEqual(123n)
16
+ BigInt.fromStringOrThrow ("123")->assertEqual(123n)
19
17
20
- BigInt.fromStringExn ("")->assertEqual(0n)
18
+ BigInt.fromStringOrThrow ("")->assertEqual(0n)
21
19
22
- BigInt.fromStringExn ("0x11")->assertEqual(17n)
20
+ BigInt.fromStringOrThrow ("0x11")->assertEqual(17n)
23
21
24
- BigInt.fromStringExn ("0b11")->assertEqual(3n)
22
+ BigInt.fromStringOrThrow ("0b11")->assertEqual(3n)
25
23
26
- BigInt.fromStringExn ("0o11")->assertEqual(9n)
24
+ BigInt.fromStringOrThrow ("0o11")->assertEqual(9n)
27
25
28
26
/* catch exception */
29
- switch BigInt.fromStringExn ("a") {
27
+ switch BigInt.fromStringOrThrow ("a") {
30
28
| exception JsExn(_error) => assert(true)
31
29
| _bigInt => assert(false)
32
30
}
33
31
```
34
32
*/
35
33
@val
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
36
63
external fromStringExn : string => bigint = "BigInt"
64
+
37
65
@val external fromInt : int => bigint = "BigInt"
38
- @val external fromFloat : float => bigint = "BigInt"
66
+
67
+ /**
68
+ Converts a `float` to a `bigint` using JavaScript semantics.
69
+ Throws an exception if the float is not an integer or is infinite/NaN.
70
+
71
+ ## Examples
72
+
73
+ ```rescript
74
+ BigInt.fromFloatOrThrow(123.0)->assertEqual(123n)
75
+
76
+ BigInt.fromFloatOrThrow(0.0)->assertEqual(0n)
77
+
78
+ BigInt.fromFloatOrThrow(-456.0)->assertEqual(-456n)
79
+
80
+ /* This will throw an exception */
81
+ switch BigInt.fromFloatOrThrow(123.5) {
82
+ | exception JsExn(_error) => assert(true)
83
+ | _bigInt => assert(false)
84
+ }
85
+ ```
86
+ */
87
+ @val
88
+ external fromFloatOrThrow : float => bigint = "BigInt"
39
89
40
90
let fromFloat = (value : float ) => {
41
- try Some (fromFloat (value )) catch {
91
+ try Some (fromFloatOrThrow (value )) catch {
42
92
| _ => None
43
93
}
44
94
}
0 commit comments