Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Belt] Stricter parse for Int and Float
The actual implementation of Belt.Int.fromString relies on the Javascript `parseInt` function [0]. That function parses strings in a permissive manner. For instance, if the function encounters an alphabetical character, it returns the value up to that character: parseInt("123a456") // output: 123 The same applies for Belt.Float.fromString that relies on `parseFloat` [1] and have a similar interpretation. This commit proposes a stricter implementation of Int and Float parsing using the Javascript `Number` constructor [2]. The Belt.Int.fromString uses the more specific `Math.trunc` function [3], that parses string similarly to `Number` and then returns the Int part of that number. Relying only on the `Number` constructor keeps the implementation simple. However, there exists one last quirk while using it with an empty string. It returns the `0` value: Number("") // ouptput: 0 Consequently, Belt.Int.fromString("") will return `0`. Tests for Belt.(Int/Float).fromString have been updated in order to reflect that. Fix rescript-lang#3732 [0] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt [1] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseFloat [2] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/Number [3] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/trunc Signed-Off-By: Ronan-A. Cherrueau <[email protected]>
- Loading branch information