Skip to content
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

Change isPlainObject to support objects created in a different context #3068

Merged

Conversation

@keeganstreet
Copy link
Contributor

keeganstreet commented Mar 19, 2020

To resolve #3066

export default (x: any): boolean =>
x !== null &&
typeof x === 'object' &&
x.toString() === '[object Object]' &&

This comment has been minimized.

Copy link
@kitten

kitten Mar 19, 2020

Member

Like indicated in the PR, this will need to be Object.prototype.toString to support objects with toStrung, including Object.create(null) 🙌

This comment has been minimized.

Copy link
@keeganstreet

keeganstreet Mar 19, 2020

Author Contributor

Ah yes of course, we can't call toString()if it doesn't exist on that object.

The problem I was trying to address by using x.toString() instead of Object.prototype.toString.call(x) was to call a custom toString when it exists:

class MyClass {
  toString() {
    return "[object MyClass]"
  }
}
const m = new MyClass();
Object.prototype.toString.call(m); // "[object Object]" <-- does not call the custom toString method
m.toString(); // "[object MyClass]"

I can solve this by using: (x.toString ? x.toString() : Object.prototype.toString.call(x)) === '[object Object]'

@keeganstreet

This comment has been minimized.

Copy link
Contributor Author

keeganstreet commented Mar 23, 2020

Hi @kitten, are you happy with this now that it has been updated?

@shadforth

This comment has been minimized.

Copy link

shadforth commented Mar 27, 2020

Looking forward to seeing this in action! 👏

Copy link
Contributor

probablyup left a comment

LGTM

@probablyup probablyup merged commit 843b018 into styled-components:master Apr 7, 2020
1 check passed
1 check passed
continuous-integration/travis-ci/pr The Travis CI build passed
Details
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

4 participants
You can’t perform that action at this time.