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

[Belt] Stricter parse for Int and Float #5209

Closed
wants to merge 1 commit into from
Closed
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: 1 addition & 1 deletion jscomp/others/belt_Float.ml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ external toInt: float -> int = "%intoffloat"

external fromInt: int -> float = "%identity"

external fromString: string -> float = "parseFloat" [@@bs.val]
external fromString: string -> float = "Number" [@@bs.val]

let fromString i =
match (fromString i) with
Expand Down
2 changes: 1 addition & 1 deletion jscomp/others/belt_Int.ml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ external toFloat: int -> float = "%identity"

external fromFloat: float -> int = "%intoffloat"

external fromString: string -> (_ [@bs.as 10]) -> int = "parseInt" [@@bs.val]
external fromString: string -> (_ [@bs.as 10]) -> int = "Math.trunc" [@@bs.val]

let fromString i =
match fromString i with
Expand Down
11 changes: 10 additions & 1 deletion jscomp/test/bs_float_test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,16 @@ let () =
eq __LOC__ (F.fromString "-1.0") (Some (-1.0));
eq __LOC__ (F.fromString "-1.5") (Some (-1.5));
eq __LOC__ (F.fromString "-1.7") (Some (-1.7));
eq __LOC__ (F.fromString "not a float") None
eq __LOC__ (F.fromString "17e-1") (Some (1.7));
eq __LOC__ (F.fromString "-17e-1") (Some (-1.7));
eq __LOC__ (F.fromString " -17e-1 ") (Some (-1.7));
eq __LOC__ (F.fromString "0x11") (Some (17.));
eq __LOC__ (F.fromString "0b11") (Some (3.));
eq __LOC__ (F.fromString "0o11") (Some (9.));
eq __LOC__ (F.fromString "") (Some (0.));
eq __LOC__ (F.fromString "not a float") None;
eq __LOC__ (F.fromString "100.0abcdef") None;
eq __LOC__ (F.fromString "123_456.7") None

let () =
eq __LOC__ (F.toString 1.0) "1";
Expand Down
11 changes: 10 additions & 1 deletion jscomp/test/bs_int_test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,16 @@ let () =
eq __LOC__ (I.fromString "-1.0") (Some (-1));
eq __LOC__ (I.fromString "-1.5") (Some (-1));
eq __LOC__ (I.fromString "-1.7") (Some (-1));
eq __LOC__ (I.fromString "not an int") None
eq __LOC__ (I.fromString "17e-1") (Some (1));
eq __LOC__ (I.fromString "-17e-1") (Some (-1));
eq __LOC__ (I.fromString " -17e-1 ") (Some (-1));
eq __LOC__ (I.fromString "0x11") (Some (17));
eq __LOC__ (I.fromString "0b11") (Some (3));
eq __LOC__ (I.fromString "0o11") (Some (9));
eq __LOC__ (I.fromString "") (Some (0));
eq __LOC__ (I.fromString "not an int") None;
eq __LOC__ (I.fromString "100abcdef") None;
eq __LOC__ (I.fromString "123_456") None

let () =
eq __LOC__ (I.toString 1) "1";
Expand Down