-
Notifications
You must be signed in to change notification settings - Fork 30k
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
util: fixed behavior of isObject() #822
Conversation
This fixes issue nodejs#743. Brought the behavior of util.isObject() in line with expected behavior, inspired by utility libraries like underscore & lodash. In particular, it now returns true for functions, as expected. Added eight tests for isObject() to ensure it behaves as expected for a wide variety of input, including cases where it is expected to return false.
LGTM |
@@ -543,7 +543,8 @@ function isRegExp(re) { | |||
exports.isRegExp = isRegExp; | |||
|
|||
function isObject(arg) { | |||
return arg !== null && typeof arg === 'object'; | |||
var type = typeof arg; | |||
return type === 'function' || type === 'object' && !!arg; |
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.
I think it might still be faster to do arg !== null
.
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.
That doesn't change the behavior, so if it's faster, happy to make the change.
Double negation considered slower than a straight null check.
Thanks for the contribution! As @vkurchatkin noted, this change is potentially breaking, and will likely have to go out in a major version release. I will chat with the TC about doing a major-version release, so I'll leave this PR open until there's a decision made. I'll comment on this PR with updates. |
resolution from last TC meeting on this was:
|
Actually, I'm -1 on changing |
To be entirely honest, I have no idea why these utility functions are in core at all if they're going to be wrong-- I wasn't aware this existed until I saw the bug, and now that I know it exists I will never, ever be using it. The feature shouldn't have been added when it was so poorly specced. That, however, is ancient history and you're stuck with it. Doc & deprecate if fixing is impossible. |
And neither do I... |
What's the verdict? Semver-major or reject and update the documentation? |
Since we already have +1 docs |
Reject & update the docs. Too much breakage from io.js right now to risk it. I would also deprecate things like this that (arguably) do not belong in node core but are instead much better provided in userland. |
Closing. Doc PR open at #1295. |
Proposed functionality fix containing prior discussion: nodejs#822 Fixes: nodejs#743 PR-URL: nodejs#1295 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Brendan Ashworth <brendan.ashworth@me.com>
This fixes issue #743.
Brought the behavior of util.isObject() in line with expected
behavior, inspired by utility libraries like underscore & lodash.
In particular, it now returns true for functions.
Added eight tests for isObject() to ensure it behaves as
expected for a wide variety of input, including cases where it
is expected to return false.