Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move parseInt and parseIntWithRadix from Float to Int module #83

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 0 additions & 3 deletions src/Core__Float.res
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
38 changes: 0 additions & 38 deletions src/Core__Float.resi
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/Core__Int.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ function fromString(radix, x) {
if (isNaN(maybeInt) || maybeInt > 2147483647 || maybeInt < -2147483648) {
return ;
} else {
return maybeInt | 0;
return maybeInt;
}
}

Expand Down
16 changes: 9 additions & 7 deletions src/Core__Int.res
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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)
}
}

Expand Down
52 changes: 52 additions & 0 deletions src/Core__Int.resi
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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<int>` representing the given value
`str`. `~radix` specifies the radix base to use for the formatted number.
Expand Down
2 changes: 1 addition & 1 deletion test/TestSuite.mjs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down