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
New private function Util._checkNumber instead of checking over isNaN #7768
base: main
Are you sure you want to change the base?
Conversation
1. `isNaN` itself is not reliable way to check if value can be converted to number. There a lot of discussions, and I just point to these unit tests: https://run.plnkr.co/plunks/93FPpacuIcXqqKMecLdk/ 2. Just making sure that the value is convertible to number is not enough. Instead we should actually convert the value to number as early as possible (otherwise `'5'+5 = '55'`). There are some places in code vulnerable to this sort of bugs, and a number of open issues (too lazy to link them right now).
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.
LGTM
IMO, yes. But if it's going to be a common method/function, I'd rather name it without the leading underscore, and put some docstrings. |
// keep it private intentionally, because such function implementation | ||
// heavily depends on it's particular application and unable to suit all the cases | ||
export function _checkNumber(a) { |
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.
// keep it private intentionally, because such function implementation | |
// heavily depends on it's particular application and unable to suit all the cases | |
export function _checkNumber(a) { | |
// @function checkNumber(data: Number|String): Number | |
// Makes sure that passed `data` is finite number, and returns it. | |
// If string is passed - converts it to number trying to avoid coercion pitfalls. | |
// Throws error in case of any failure. | |
export function checkNumber(a) { |
@IvanSanchez
But I'm not sure, because fixing it in public API
- .. makes impossible future internal changes. E.g. what if later we'll decide to throw on strings like '1e9'
- .. opens the door to misusing (remember using
Browser.touch
)
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.
@IvanSanchez what do you think about that points? I personally would like to have a public function so it can be easy used in plugins. Not everyone should reinvent the wheel.
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.
@mourner ?
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.
@mourner what do you think is better? A public or private function for checking if the input is a number.
The open questions is should we make the The concerns against public was:
@mourner @IvanSanchez how shall we proceed? It would be good if we decide that. Around 5 PRs and 8 issues are waiting on that decision. |
I'd argue to make it private, if it creates added value for plugins devs we can always make it public in the future. Let it prove itself first. |
@mourner what do you think? |
isNaN
itself is not reliable way to check if value can be converted to number. There a lot of discussions, and I just point to these unit tests: https://run.plnkr.co/plunks/93FPpacuIcXqqKMecLdk/Just making sure that the value is convertible to number is not enough. Instead we should actually convert the value to number as early as possible (otherwise
'5'+5 = '55'
).There are some places in code vulnerable to this sort of bugs, and a number of open issues (too lazy to link them right now).
Ref: #7761, #7128.
Questions:
validateNumber
)?This PR contains only 1 fix, as an example.