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 keeganstreet commented Mar 19, 2020

To resolve #3066

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

@kitten kitten Mar 19, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Contributor Author

@keeganstreet keeganstreet Mar 19, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Contributor Author

@keeganstreet keeganstreet commented Mar 23, 2020

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

@shadforth
Copy link

@shadforth shadforth commented Mar 27, 2020

Looking forward to seeing this in action! 👏

Copy link
Contributor

@probablyup probablyup left a comment

LGTM

@probablyup probablyup merged commit 843b018 into styled-components:master Apr 7, 2020
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants