[Belt] Stricter parse for Int and Float #5209
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The actual implementation of Belt.Int.fromString relies on the
Javascript
parseInt
function [0]. That function parses Strings in apermissive manner. For instance, if the function encounters an
alphabetical character, it returns the value up to that character:
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.fromStringuses the more specific
Math.trunc
function [3], that parses stringsimilarly 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:Consequently, Belt.Int.fromString("") will return
0
. Tests forBelt.(Int/Float).fromString have been updated in order to reflect that.
Fix #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