-
Couldn't load subscription status.
- Fork 1.3k
fix: NumberParser useGrouping false returned wrong value #9089
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
Closed
+22
−1
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for looking into it! @snowystinger
Could you clarify the fix a bit more
My expectation here would be more in this direction
expect(new NumberParser('en-US', {useGrouping: false}).parse('1234,7')).toBeNaN();
expect(new NumberParser('de-DE', {useGrouping: false}).parse('1234.7')).toBeNaN();
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, from the description
Can you say more as to why returning NaN would be your expectation though?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not really correct and could have serious consequences. From the perspective of a German locale,
1234.7is not a number. If a user enters it then it is incorrect (they probably entered using a different locale format, but we need to be sure). Excel etc would also not understand it as a number.If it is transformed to
12347then this could have very serious consequences for further calculations that depend on it. It would be safer to parse it asNaNso that the error can be caught and handled correctly before it propagates further.I would expect:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(bit of a stream of conscious as I was working through it while writing)
That makes sense. As a counter though, we don't want to just block out everything.
I know this particular case is more ambiguous than my example.
But hang on, let's back up, how are you getting to a point where this is coming up? Are you running
isValidPartialNumberbefore? that's how our NumberField stops invalid numbers/characters.For instance, this behaves as I believe you expect.
Parse will do its absolute best to convert something into a number. If you want to be strict about it, I suggest running isValidPartialNumber first. Hopefully this can get you unblocked in the meantime.
I'm going to close this PR as I've handled it over in #8592 now because I wanted to make sure I wasn't breaking any assumptions there and it was related to some work I was doing there to handle ambiguous vs non-ambiguous cases.
We can probably continue any discussion over there. Thanks for the feedback!
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sigh, had another thought, it's not necessarily obvious whether an input allows grouping symbols. So someone in english could be trying to paste a number from some other place which does include a grouping symbol into a field that doesn't allow it, this should still work I think.
For example, if I pasted
'1,024'in an en-US NumberField with useGrouping: false, then I think it should be parsed to 1024There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, added comments over there, at least the locales match in terms of handling now