-
-
Notifications
You must be signed in to change notification settings - Fork 894
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
Fix parseInt and parseFloat #402
Conversation
@@ -1980,9 +1980,15 @@ | |||
parseInt = (function (origParseInt) { | |||
var hexRegex = /^[\-+]?0[xX]/; | |||
return function parseInt(str, radix) { | |||
var string = trim(str); | |||
var defaultedRadix = $Number(radix) || (hexRegex.test(string) ? 16 : 10); | |||
return origParseInt(string, defaultedRadix); |
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.
Since this calls into the original function, shouldn't it already return NaN
?
In which browsers/engines does the existing shim not do this?
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.
Also, parseInt('undefined')
should also be NaN
, so since parseInt
changes all input to strings, checking the typeof
isn't very helpful.
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.
In firefox 17 parseInt(null) and parseInt(undefined) are throw "can't convert undefined to object".
But parseInt(NaN) is ok. Need some testing.
Probably that is because of
var trim = call.bind(String.prototype.trim);
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.
aha, ok - in that case, I probably just need trim(String(str))
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.
Yes, this will work.
Also, this problem is for undefined and null only.
Function.prototype.call.bind(String.prototype.trim)(undefined)
throws Uncaught TypeError: String.prototype.trim called on null or undefined
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.
Trim will convert any other variable to String, anyway. So, trim(String(str))
seems to be nice solution.
If you want to change your implementation diff to be just what we discussed, and then rebase everything down to a commit on latest master, I'd love to merge it :-) |
convert argument to String to prevent errors when functions called with undefined or null
52aeb66
to
406f0ff
Compare
Done. |
@ValeryVS hmm, I'm checking in FF 17, and even on master, all of your added tests are passing. Which version of es5-shim are you using? Can you compare the latest? Can you create a jsfiddle or equivalent that reproduces the failure? |
Here is plunk es5-shim file from master, but "if" conditions near parseInt and parseFloat are commented out. |
@ljharb I wrote you about this bug ~ half a year ago. One new report - zloirock/core-js#208 (comment) |
FF 17 throws |
@ljharb This link to plunk from my first comment works
To see the error, you must open browser console and ther click the "run" button. |
@ValeryVS aha, got it :-) thank you! |
parsing with undefined, null or NaN should return NaN