Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

#### :boom: Breaking Change

- `Int.fromString` and `Float.fromString` use stricter number parsing and no longer uses an explicit radix argument, but instead supports parsing hexadecimal, binary and exponential notation.

#### :eyeglasses: Spec Compliance

#### :rocket: New Feature
Expand Down
10 changes: 5 additions & 5 deletions packages/@rescript/runtime/Stdlib_Float.res
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ external toPrecisionWithPrecision: (float, ~digits: int) => string = "toPrecisio
external toStringWithRadix: (float, ~radix: int) => string = "toString"
@send external toLocaleString: float => string = "toLocaleString"

let fromString = i =>
switch parseFloat(i) {
| i if isNaN(i) => None
| i => Some(i)
}
let fromString: string => option<float> = %raw(`str => {
if (!str || !str.trim()) return;
let num = +str; // Number coercion, see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number#number_coercion
return isNaN(num) ? undefined : num;
}`)

external toInt: float => int = "%intoffloat"
external fromInt: int => float = "%identity"
Expand Down
17 changes: 3 additions & 14 deletions packages/@rescript/runtime/Stdlib_Int.res
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,9 @@ external toStringWithRadix: (int, ~radix: int) => string = "toString"
external toFloat: int => float = "%identity"
external fromFloat: float => int = "%intoffloat"

let fromString = (x, ~radix=?) => {
let maybeInt = switch radix {
| Some(radix) => Stdlib_Float.parseInt(x, ~radix)
| None => Stdlib_Float.parseInt(x)
}

if Stdlib_Float.isNaN(maybeInt) {
None
} else if maybeInt > Constants.maxValue->toFloat || maybeInt < Constants.minValue->toFloat {
None
} else {
let asInt = fromFloat(maybeInt)
Some(asInt)
}
let fromString: string => option<int> = str => {
let? Some(num) = str->Stdlib_Float.fromString
num === num->fromFloat->toFloat && Stdlib_Float.isFinite(num) ? Some(num->fromFloat) : None
}

external mod: (int, int) => int = "%modint"
Expand Down
10 changes: 6 additions & 4 deletions packages/@rescript/runtime/Stdlib_Int.resi
Original file line number Diff line number Diff line change
Expand Up @@ -295,18 +295,20 @@ Int.fromFloat(0.9999) == 0
external fromFloat: float => int = "%intoffloat"

/**
`fromString(str, ~radix=?)` return an `option<int>` representing the given value
`str`. `~radix` specifies the radix base to use for the formatted number.
`fromString(str)` return an `option<int>` representing the given value
`str`. Hexadecimal, binary and exponential notation is supported.

## Examples

```rescript
Int.fromString("0") == Some(0)
Int.fromString("0x6") == Some(6)
Int.fromString("2.5e2") == Some(250)
Int.fromString("NaN") == None
Int.fromString("6", ~radix=2) == None
Int.fromString("0b6") == None
```
*/
let fromString: (string, ~radix: int=?) => option<int>
let fromString: string => option<int>

/**
`mod(n1, n2)` calculates the modulo (remainder after division) of two integers.
Expand Down
13 changes: 5 additions & 8 deletions packages/@rescript/runtime/lib/es6/Stdlib_Float.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@

let Constants = {};

function fromString(i) {
let i$1 = parseFloat(i);
if (Number.isNaN(i$1)) {
return;
} else {
return i$1;
}
}
let fromString = (str => {
if (!str || !str.trim()) return;
let num = +str; // Number coercion, see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number#number_coercion
return isNaN(num) ? undefined : num;
});

function clamp(min, max, value) {
let value$1 = max !== undefined && max < value ? max : value;
Expand Down
15 changes: 10 additions & 5 deletions packages/@rescript/runtime/lib/es6/Stdlib_Int.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@


import * as Stdlib_Array from "./Stdlib_Array.js";
import * as Stdlib_Float from "./Stdlib_Float.js";

function fromString(x, radix) {
let maybeInt = radix !== undefined ? parseInt(x, radix) : parseInt(x);
if (Number.isNaN(maybeInt) || maybeInt > 2147483647 || maybeInt < -2147483648) {
return;
function fromString(str) {
let num = Stdlib_Float.fromString(str);
if (num !== undefined) {
if (num === (num | 0) && isFinite(num)) {
return num | 0;
} else {
return;
}
} else {
return maybeInt | 0;
return num;
}
}

Expand Down
13 changes: 5 additions & 8 deletions packages/@rescript/runtime/lib/js/Stdlib_Float.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@

let Constants = {};

function fromString(i) {
let i$1 = parseFloat(i);
if (Number.isNaN(i$1)) {
return;
} else {
return i$1;
}
}
let fromString = (str => {
if (!str || !str.trim()) return;
let num = +str; // Number coercion, see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number#number_coercion
return isNaN(num) ? undefined : num;
});

function clamp(min, max, value) {
let value$1 = max !== undefined && max < value ? max : value;
Expand Down
15 changes: 10 additions & 5 deletions packages/@rescript/runtime/lib/js/Stdlib_Int.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
'use strict';

let Stdlib_Array = require("./Stdlib_Array.js");
let Stdlib_Float = require("./Stdlib_Float.js");

function fromString(x, radix) {
let maybeInt = radix !== undefined ? parseInt(x, radix) : parseInt(x);
if (Number.isNaN(maybeInt) || maybeInt > 2147483647 || maybeInt < -2147483648) {
return;
function fromString(str) {
let num = Stdlib_Float.fromString(str);
if (num !== undefined) {
if (num === (num | 0) && isFinite(num)) {
return num | 0;
} else {
return;
}
} else {
return maybeInt | 0;
return num;
}
}

Expand Down
3 changes: 3 additions & 0 deletions packages/@rescript/runtime/rescript.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"name": "@rescript/runtime",
"experimental-features": {
"LetUnwrap": true
},
"sources": [
{
"dir": "."
Expand Down
2 changes: 1 addition & 1 deletion tests/docstring_tests/DocTest.res.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tests/tests/src/field_flattening_opt.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ let group = {
};

function fn(str) {
group.nested.field = Stdlib_Int.fromString(str, undefined);
group.nested.field = Stdlib_Int.fromString(str);
}

let WithNestedMutableFields = {
Expand Down
12 changes: 6 additions & 6 deletions tests/tests/src/stdlib/Stdlib_TempTests.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ console.log((10.2).toFixed(2));

console.log((10).toFixed(2));

console.log(Stdlib_Int.fromString("0", undefined));
console.log(Stdlib_Int.fromString("0"));

console.log(Stdlib_Float.fromString("0.1"));

Expand Down Expand Up @@ -282,15 +282,15 @@ console.log({

let Bugfix = {};

console.log(Stdlib_Int.fromString("1231231", undefined));
console.log(Stdlib_Int.fromString("1231231"));

console.log(Stdlib_Int.fromString("12.22", undefined));
console.log(Stdlib_Int.fromString("12.22"));

console.log(Stdlib_Int.fromString("99999999999999999", undefined));
console.log(Stdlib_Int.fromString("99999999999999999"));

console.log(Stdlib_Int.fromString("99999999999999999", undefined));
console.log(Stdlib_Int.fromString("99999999999999999"));

console.log(Stdlib_Int.fromString("010101", 2));
console.log(Stdlib_Int.fromString("0b010101"));

let _collator = Stdlib_IntlTests._collator;

Expand Down
2 changes: 1 addition & 1 deletion tests/tests/src/stdlib/Stdlib_TempTests.res
Original file line number Diff line number Diff line change
Expand Up @@ -193,4 +193,4 @@ Console.log(Int.fromString("1231231"))
Console.log(Int.fromString("12.22"))
Console.log(Int.fromString("99999999999999999"))
Console.log(Int.fromString("99999999999999999"))
Console.log(Int.fromString(~radix=2, "010101"))
Console.log(Int.fromString("0b010101"))
Loading