diff --git a/src/Core__Float.res b/src/Core__Float.res index 8781324d..a040f90b 100644 --- a/src/Core__Float.res +++ b/src/Core__Float.res @@ -10,9 +10,6 @@ module Constants = { @val external isNaN: float => bool = "isNaN" @val external isFinite: float => bool = "isFinite" @val external parseFloat: 'a => float = "parseFloat" -// parseInt's return type is a float because it can be NaN -@val external parseInt: 'a => float = "parseInt" -@val external parseIntWithRadix: ('a, ~radix: int) => float = "parseInt" @send external toExponential: float => string = "toExponential" @send external toExponentialWithPrecision: (float, ~digits: int) => string = "toExponential" diff --git a/src/Core__Float.resi b/src/Core__Float.resi index cb3e835b..a35114cf 100644 --- a/src/Core__Float.resi +++ b/src/Core__Float.resi @@ -157,44 +157,6 @@ Float.parseFloat("error")->Float.isNaN // true @val external parseFloat: string => float = "parseFloat" -/** -`parseInt(v)` parse the given `v` and returns a float. Leading whitespace in -`v` is ignored. Returns `NaN` if `v` can't be parsed. -See [`parseInt`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt) on MDN. - -## Examples - -```rescript -Float.parseInt("1.0") // 1.0 -Float.parseInt(" 3.14 ") // 3.0 -Float.parseInt(3) // 3.0 -Float.parseInt("3.14some non-digit characters") // 3.0 -Float.parseInt("error")->Float.isNaN // true -``` -*/ -@val -external parseInt: 'a => float = "parseInt" - -/** -`parseIntWithRadix(v, ~radix)` parse the given `v` and returns a float. Leading -whitespace in this argument `v`is ignored. `radix` specifies the radix base to -use for the formatted number. The value must be in the range [2, 36] (inclusive). -Returns `NaN` if `v` can't be parsed and `radix` is smaller than 2 or bigger -than 36. -See [`parseInt`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt) on MDN. - -## Examples - -```rescript -Float.parseInt("10.0", ~radix=2) // 2.0 -Float.parseInt("15 * 3", ~radix=10) // 15.0 -Float.parseInt("12", ~radix=13) // 15.0 -Float.parseInt("17", ~radix=40)->Float.isNaN // true -``` -*/ -@val -external parseIntWithRadix: ('a, ~radix: int) => float = "parseInt" - /** `toExponential(v)` return a `string` representing the given value in exponential notation. diff --git a/src/Core__Int.mjs b/src/Core__Int.mjs index 98b0dc45..55dd2604 100644 --- a/src/Core__Int.mjs +++ b/src/Core__Int.mjs @@ -6,7 +6,7 @@ function fromString(radix, x) { if (isNaN(maybeInt) || maybeInt > 2147483647 || maybeInt < -2147483648) { return ; } else { - return maybeInt | 0; + return maybeInt; } } diff --git a/src/Core__Int.res b/src/Core__Int.res index 29172623..5b4ac076 100644 --- a/src/Core__Int.res +++ b/src/Core__Int.res @@ -2,7 +2,7 @@ module Constants = { @inline let minValue = -2147483648 @inline let maxValue = 2147483647 } - +@val external isNaN: int => bool = "isNaN" @send external toExponential: int => string = "toExponential" @send external toExponentialWithPrecision: (int, ~digits: int) => string = "toExponential" @@ -19,18 +19,20 @@ module Constants = { external toFloat: int => float = "%identity" external fromFloat: float => int = "%intoffloat" +@val external parseInt: 'a => int = "parseInt" +@val external parseIntWithRadix: ('a, ~radix: int) => int = "parseInt" + let fromString = (~radix=?, x) => { let maybeInt = switch radix { - | Some(radix) => Core__Float.parseIntWithRadix(x, ~radix) - | None => Core__Float.parseInt(x) + | Some(radix) => parseIntWithRadix(x, ~radix) + | None => parseInt(x) } - if Core__Float.isNaN(maybeInt) { + if isNaN(maybeInt) { None - } else if maybeInt > Constants.maxValue->toFloat || maybeInt < Constants.minValue->toFloat { + } else if maybeInt > Constants.maxValue || maybeInt < Constants.minValue { None } else { - let asInt = fromFloat(maybeInt) - Some(asInt) + Some(maybeInt) } } diff --git a/src/Core__Int.resi b/src/Core__Int.resi index 68498f18..43662b76 100644 --- a/src/Core__Int.resi +++ b/src/Core__Int.resi @@ -56,6 +56,20 @@ module Constants: { let maxValue: int } +/** +`isNaN(v)` tests if the given `v` is `NaN`. +See [`NaN`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NaN) on MDN. + +## Examples + +```rescript +Int.isNaN(3) // false +Int.isNaN(Int.parseInt("error")) // true +``` +*/ +@val +external isNaN: int => bool = "isNaN" + /** `toExponential(n)` return a `string` representing the given value in exponential notation. @@ -244,6 +258,44 @@ Int.fromFloat(0.9999) == 0 */ external fromFloat: float => int = "%intoffloat" +/** +`parseInt(v)` parse the given `v` and returns an int. Leading whitespace in +`v` is ignored. Returns `NaN` if `v` can't be parsed. +See [`parseInt`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt) on MDN. + +## Examples + +```rescript +Int.parseInt("1.0") // 1 +Int.parseInt(" 3.14 ") // 3 +Int.parseInt(3) // 3 +Int.parseInt("3.14some non-digit characters") // 3 +Int.parseInt("error")->Int.isNaN // true +``` +*/ +@val +external parseInt: 'a => int = "parseInt" + +/** +`parseIntWithRadix(v, ~radix)` parse the given `v` and returns an int. Leading +whitespace in this argument `v` is ignored. `radix` specifies the radix base to +use for the formatted number. The value must be in the range [2, 36] (inclusive). +Returns `NaN` if `v` can't be parsed and `radix` is smaller than 2 or bigger +than 36. +See [`parseInt`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt) on MDN. + +## Examples + +```rescript +Int.parseInt("10.0", ~radix=2) // 2 +Int.parseInt("15 * 3", ~radix=10) // 15 +Int.parseInt("12", ~radix=13) // 15 +Int.parseInt("17", ~radix=40)->Int.isNaN // true +``` +*/ +@val +external parseIntWithRadix: ('a, ~radix: int) => int = "parseInt" + /** `fromString(~radix?, str)` return an `option` representing the given value `str`. `~radix` specifies the radix base to use for the formatted number. diff --git a/test/TestSuite.mjs b/test/TestSuite.mjs index 64ebfbc1..f3e3bfee 100644 --- a/test/TestSuite.mjs +++ b/test/TestSuite.mjs @@ -1,7 +1,7 @@ // Generated by ReScript, PLEASE EDIT WITH CARE -import * as ErrorTests from "./ErrorTests.mjs"; import * as ArrayTests from "./ArrayTests.mjs"; +import * as ErrorTests from "./ErrorTests.mjs"; import * as PromiseTest from "./PromiseTest.mjs"; var TestError = PromiseTest.TestError;