Skip to content

Commit 25b7fce

Browse files
committed
Int, Float, BigInt: use optional args and deprecate xxxWithRadix, xxxWithPrecision etc.
1 parent 5d675f7 commit 25b7fce

12 files changed

+332
-243
lines changed

scripts/DocTests.mjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ async function main() {
306306
var id = example.id.replaceAll(".", "_");
307307
var codes = getCodeBlocks(example);
308308
var results = await Promise.all(codes.map(async function (code, $$int) {
309-
var id$1 = id + "_" + $$int.toString();
309+
var id$1 = id + "_" + $$int.toString(undefined);
310310
return await testCode(id$1, code);
311311
}));
312312
return [

src/Core__BigInt.res

+4-2
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,10 @@ See [`toString`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referen
5050
Js.BigInt.toString(123n)->Js.log
5151
```
5252
*/
53-
external toString: bigint => string = "toString"
54-
@send external toStringWithRadix: (bigint, ~radix: int) => string = "toString"
53+
external toString: (bigint, ~radix: int=?) => string = "toString"
54+
55+
@deprecated("Use `toString` with `~radix` instead") @send
56+
external toStringWithRadix: (bigint, ~radix: int) => string = "toString"
5557

5658
@send
5759
/**

src/Core__Float.res

+15-10
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,25 @@ let compare = (a: float, b: float) =>
1616
@val external isFinite: float => bool = "isFinite"
1717
@val external parseFloat: 'a => float = "parseFloat"
1818
// parseInt's return type is a float because it can be NaN
19-
@val external parseInt: 'a => float = "parseInt"
20-
@val external parseIntWithRadix: ('a, ~radix: int) => float = "parseInt"
19+
@val external parseInt: ('a, ~radix: int=?) => float = "parseInt"
20+
@deprecated("Use `parseInt` instead") @val
21+
external parseIntWithRadix: ('a, ~radix: int) => float = "parseInt"
2122

22-
@send external toExponential: float => string = "toExponential"
23-
@send external toExponentialWithPrecision: (float, ~digits: int) => string = "toExponential"
23+
@send external toExponential: (float, ~digits: int=?) => string = "toExponential"
24+
@deprecated("Use `toExponential` instead") @send
25+
external toExponentialWithPrecision: (float, ~digits: int) => string = "toExponential"
2426

25-
@send external toFixed: float => string = "toFixed"
26-
@send external toFixedWithPrecision: (float, ~digits: int) => string = "toFixed"
27+
@send external toFixed: (float, ~digits: int=?) => string = "toFixed"
28+
@deprecated("Use `toFixed` instead") @send
29+
external toFixedWithPrecision: (float, ~digits: int) => string = "toFixed"
2730

28-
@send external toPrecision: float => string = "toPrecision"
29-
@send external toPrecisionWithPrecision: (float, ~digits: int) => string = "toPrecision"
31+
@send external toPrecision: (float, ~digits: int=?) => string = "toPrecision"
32+
@deprecated("Use `toPrecision` instead") @send
33+
external toPrecisionWithPrecision: (float, ~digits: int) => string = "toPrecision"
3034

31-
@send external toString: float => string = "toString"
32-
@send external toStringWithRadix: (float, ~radix: int) => string = "toString"
35+
@send external toString: (float, ~radix: int=?) => string = "toString"
36+
@deprecated("Use `toString` instead") @send
37+
external toStringWithRadix: (float, ~radix: int) => string = "toString"
3338
@send external toLocaleString: float => string = "toLocaleString"
3439

3540
let fromString = i =>

src/Core__Float.resi

+48-14
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,11 @@ Float.parseFloat("error")->Float.isNaN // true
162162
external parseFloat: string => float = "parseFloat"
163163

164164
/**
165-
`parseInt(v)` parse the given `v` and returns a float. Leading whitespace in
166-
`v` is ignored. Returns `NaN` if `v` can't be parsed.
165+
`parseInt(v, ~radix=?)` parse the given `v` and returns a float. Leading
166+
whitespace in this argument `v`is ignored. `radix` specifies the radix base to
167+
use for the formatted number. The value must be in the range [2, 36] (inclusive).
168+
Returns `NaN` if `v` can't be parsed and `radix` is smaller than 2 or bigger
169+
than 36.
167170
See [`parseInt`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt) on MDN.
168171

169172
## Examples
@@ -174,10 +177,14 @@ Float.parseInt(" 3.14 ") // 3.0
174177
Float.parseInt(3) // 3.0
175178
Float.parseInt("3.14some non-digit characters") // 3.0
176179
Float.parseInt("error")->Float.isNaN // true
180+
Float.parseInt("10.0", ~radix=2) // 2.0
181+
Float.parseInt("15 * 3", ~radix=10) // 15.0
182+
Float.parseInt("12", ~radix=13) // 15.0
183+
Float.parseInt("17", ~radix=40)->Float.isNaN // true
177184
```
178185
*/
179186
@val
180-
external parseInt: 'a => float = "parseInt"
187+
external parseInt: ('a, ~radix: int=?) => float = "parseInt"
181188

182189
/**
183190
`parseIntWithRadix(v, ~radix)` parse the given `v` and returns a float. Leading
@@ -196,23 +203,32 @@ Float.parseIntWithRadix("12", ~radix=13) // 15.0
196203
Float.parseIntWithRadix("17", ~radix=40)->Float.isNaN // true
197204
```
198205
*/
206+
@deprecated("Use `parseInt` instead")
199207
@val
200208
external parseIntWithRadix: ('a, ~radix: int) => float = "parseInt"
201209

202210
/**
203-
`toExponential(v)` return a `string` representing the given value in exponential
204-
notation.
211+
`toExponential(v, ~digits=?)` return a `string` representing the given value in
212+
exponential notation. `digits` specifies how many digits should appear after
213+
the decimal point.
205214
See [`Number.toExponential`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toExponential) on MDN.
206215

207216
## Examples
208217

209218
```rescript
210219
Float.toExponential(1000.0) // "1e+3"
211220
Float.toExponential(-1000.0) // "-1e+3"
221+
Float.toExponential(77.0, ~digits=2) // "7.70e+1"
222+
Float.toExponential(5678.0, ~digits=2) // "5.68e+3"
223+
```
224+
225+
## Exceptions
226+
227+
- `RangeError`: If `digits` less than 0 or greater than 10.
212228
```
213229
*/
214230
@send
215-
external toExponential: float => string = "toExponential"
231+
external toExponential: (float, ~digits: int=?) => string = "toExponential"
216232

217233
/**
218234
`toExponential(v, ~digits)` return a `string` representing the given value in
@@ -231,23 +247,31 @@ Float.toExponentialWithPrecision(5678.0, ~digits=2) // "5.68e+3"
231247

232248
- `RangeError`: If `digits` less than 0 or greater than 10.
233249
*/
250+
@deprecated("Use `toExponential` instead")
234251
@send
235252
external toExponentialWithPrecision: (float, ~digits: int) => string = "toExponential"
236253

237254
/**
238-
`toFixed(v)` return a `string` representing the given value using fixed-point
239-
notation.
255+
`toFixed(v, ~digits=?)` return a `string` representing the given
256+
value using fixed-point notation. `digits` specifies how many digits should
257+
appear after the decimal point.
240258
See [`Number.toFixed`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed) on MDN.
241259

242260
## Examples
243261

244262
```rescript
245263
Float.toFixed(123456.0) // "123456.00"
246264
Float.toFixed(10.0) // "10.00"
265+
Float.toFixed(300.0, ~digits=4) // "300.0000"
266+
Float.toFixed(300.0, ~digits=1) // "300.0"
247267
```
268+
269+
## Exceptions
270+
271+
- `RangeError`: If `digits` is less than 0 or larger than 100.
248272
*/
249273
@send
250-
external toFixed: float => string = "toFixed"
274+
external toFixed: (float, ~digits: int=?) => string = "toFixed"
251275

252276
/**
253277
`toFixedWithPrecision(v, ~digits)` return a `string` representing the given
@@ -266,24 +290,32 @@ Float.toFixedWithPrecision(300.0, ~digits=1) // "300.0"
266290

267291
- `RangeError`: If `digits` is less than 0 or larger than 100.
268292
*/
293+
@deprecated("Use `toFixed` instead")
269294
@send
270295
external toFixedWithPrecision: (float, ~digits: int) => string = "toFixed"
271296

272297
/**
273-
`toPrecision(v)` return a `string` representing the giver value with precision.
274-
This function omits the argument that controls precision, so it behaves like
275-
`toString`. See `toPrecisionWithPrecision` to control precision.
298+
`toPrecision(v, ~digits=?)` return a `string` representing the giver value with
299+
precision. `digits` specifies the number of significant digits.
276300
See [`Number.toPrecision`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toPrecision) on MDN.
277301

278302
## Examples
279303

280304
```rescript
281305
Float.toPrecision(100.0) // "100"
282306
Float.toPrecision(1.0) // "1"
307+
Float.toPrecision(100.0, ~digits=2) // "1.0e+2"
308+
Float.toPrecision(1.0, ~digits=1) // "1"
283309
```
310+
311+
## Exceptions
312+
313+
- `RangeError`: If `digits` is not between 1 and 100 (inclusive).
314+
Implementations are allowed to support larger and smaller values as well.
315+
ECMA-262 only requires a precision of up to 21 significant digits.
284316
*/
285317
@send
286-
external toPrecision: float => string = "toPrecision"
318+
external toPrecision: (float, ~digits: int=?) => string = "toPrecision"
287319

288320
/**
289321
`toPrecisionWithPrecision(v, ~digits)` return a `string` representing the giver value with
@@ -304,6 +336,7 @@ Implementations are allowed to support larger and smaller values as well.
304336
ECMA-262 only requires a precision of up to 21 significant digits.
305337

306338
*/
339+
@deprecated("Use `toPrecision` instead")
307340
@send
308341
external toPrecisionWithPrecision: (float, ~digits: int) => string = "toPrecision"
309342

@@ -319,7 +352,7 @@ Float.toString(-1000.0) // "-1000"
319352
```
320353
*/
321354
@send
322-
external toString: float => string = "toString"
355+
external toString: (float, ~radix: int=?) => string = "toString"
323356

324357
/**
325358
`toStringWithRadix(v, ~radix)` return a `string` representing the given value.
@@ -338,6 +371,7 @@ Float.toStringWithRadix(123456.0, ~radix=36) // "2n9c"
338371

339372
`RangeError`: if `radix` is less than 2 or greater than 36.
340373
*/
374+
@deprecated("Use `toString` with `~radix` instead")
341375
@send
342376
external toStringWithRadix: (float, ~radix: int) => string = "toString"
343377

src/Core__Int.mjs

+8-7
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ function compare(a, b) {
1717
}
1818
}
1919

20-
function fromString(radix, x) {
20+
function fromString(x, radix) {
2121
var maybeInt = radix !== undefined ? parseInt(x, radix) : parseInt(x);
2222
if (isNaN(maybeInt) || maybeInt > 2147483647 || maybeInt < -2147483648) {
2323
return ;
@@ -26,7 +26,8 @@ function fromString(radix, x) {
2626
}
2727
}
2828

29-
function rangeWithOptions(start, end, options) {
29+
function range(start, end, optionsOpt) {
30+
var options = optionsOpt !== undefined ? optionsOpt : ({});
3031
var isInverted = start > end;
3132
var n = options.step;
3233
var step;
@@ -48,17 +49,17 @@ function rangeWithOptions(start, end, options) {
4849
} else if (step === 0) {
4950
length = options.inclusive === true ? 1 : 0;
5051
} else {
51-
var range = isInverted ? start - end | 0 : end - start | 0;
52-
var range$1 = options.inclusive === true ? range + 1 | 0 : range;
53-
length = Math.ceil(range$1 / PervasivesU.abs(step)) | 0;
52+
var range$1 = isInverted ? start - end | 0 : end - start | 0;
53+
var range$2 = options.inclusive === true ? range$1 + 1 | 0 : range$1;
54+
length = Math.ceil(range$2 / PervasivesU.abs(step)) | 0;
5455
}
5556
return Core__Array.fromInitializer(length, (function (i) {
5657
return start + Math.imul(i, step) | 0;
5758
}));
5859
}
5960

60-
function range(start, end) {
61-
return rangeWithOptions(start, end, {});
61+
function rangeWithOptions(start, end, options) {
62+
return range(start, end, options);
6263
}
6364

6465
function clamp(min, max, value) {

src/Core__Int.res

+19-12
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,32 @@ let equal = (a: int, b: int) => a === b
88
let compare = (a: int, b: int) =>
99
a < b ? Core__Ordering.less : a > b ? Core__Ordering.greater : Core__Ordering.equal
1010

11-
@send external toExponential: int => string = "toExponential"
12-
@send external toExponentialWithPrecision: (int, ~digits: int) => string = "toExponential"
11+
@send external toExponential: (int, ~digits: int=?) => string = "toExponential"
12+
@deprecated("Use `toExponential` instead") @send
13+
external toExponentialWithPrecision: (int, ~digits: int) => string = "toExponential"
1314

14-
@send external toFixed: int => string = "toFixed"
15-
@send external toFixedWithPrecision: (int, ~digits: int) => string = "toFixed"
15+
@send external toFixed: (int, ~digits: int=?) => string = "toFixed"
16+
@deprecated("Use `toFixed` instead") @send
17+
external toFixedWithPrecision: (int, ~digits: int) => string = "toFixed"
1618

17-
@send external toPrecision: int => string = "toPrecision"
18-
@send external toPrecisionWithPrecision: (int, ~digits: int) => string = "toPrecision"
19+
@send external toPrecision: (int, ~digits: int=?) => string = "toPrecision"
20+
@deprecated("Use `toPrecision` instead") @send
21+
external toPrecisionWithPrecision: (int, ~digits: int) => string = "toPrecision"
1922

20-
@send external toString: int => string = "toString"
21-
@send external toStringWithRadix: (int, ~radix: int) => string = "toString"
23+
@send external toString: (int, ~radix: int=?) => string = "toString"
24+
@deprecated("Use `toString` instead") @send
25+
external toStringWithRadix: (int, ~radix: int) => string = "toString"
2226
@send external toLocaleString: int => string = "toLocaleString"
2327

2428
external toFloat: int => float = "%identity"
2529
external fromFloat: float => int = "%intoffloat"
2630

27-
let fromString = (~radix=?, x) => {
31+
let fromString = (x, ~radix=?) => {
2832
let maybeInt = switch radix {
29-
| Some(radix) => Core__Float.parseIntWithRadix(x, ~radix)
33+
| Some(radix) => Core__Float.parseInt(x, ~radix)
3034
| None => Core__Float.parseInt(x)
3135
}
36+
3237
if Core__Float.isNaN(maybeInt) {
3338
None
3439
} else if maybeInt > Constants.maxValue->toFloat || maybeInt < Constants.minValue->toFloat {
@@ -42,7 +47,8 @@ let fromString = (~radix=?, x) => {
4247
external mod: (int, int) => int = "%modint"
4348

4449
type rangeOptions = {step?: int, inclusive?: bool}
45-
let rangeWithOptions = (start, end, options) => {
50+
51+
let range = (start, end, ~options: rangeOptions={}) => {
4652
let isInverted = start > end
4753

4854
let step = switch options.step {
@@ -65,7 +71,8 @@ let rangeWithOptions = (start, end, options) => {
6571
Core__Array.fromInitializer(~length, i => start + i * step)
6672
}
6773

68-
let range = (start, end) => rangeWithOptions(start, end, {})
74+
@deprecated("Use `range` instead") @send
75+
let rangeWithOptions = (start, end, options) => range(start, end, ~options)
6976

7077
let clamp = (~min=?, ~max=?, value): int => {
7178
let value = switch max {

0 commit comments

Comments
 (0)