-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathCore__Int.res
39 lines (32 loc) · 1.29 KB
/
Core__Int.res
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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"
@send external toFixed: int => string = "toFixed"
@send external toFixedWithPrecision: (int, ~digits: int) => string = "toFixed"
@send external toPrecision: int => string = "toPrecision"
@send external toPrecisionWithPrecision: (int, ~digits: int) => string = "toPrecision"
@send external toString: int => string = "toString"
@send external toStringWithRadix: (int, ~radix: int) => string = "toString"
@send external toLocaleString: int => string = "toLocaleString"
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) => parseIntWithRadix(x, ~radix)
| None => parseInt(x)
}
if isNaN(maybeInt) {
None
} else if maybeInt > Constants.maxValue || maybeInt < Constants.minValue {
None
} else {
Some(maybeInt)
}
}
external mod: (int, int) => int = "%modint"