Skip to content

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

Merged
quantizor merged 2 commits intostyled-components:masterfrom
keeganstreet:fix-style-objects-node-vm
Apr 7, 2020
Merged

Change isPlainObject to support objects created in a different context#3068
quantizor merged 2 commits intostyled-components:masterfrom
keeganstreet:fix-style-objects-node-vm

Conversation

@keeganstreet
Copy link
Copy Markdown
Contributor

To resolve #3066

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

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

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

@shadforth
Copy link
Copy Markdown

Looking forward to seeing this in action! 👏

Copy link
Copy Markdown
Contributor

@quantizor quantizor left a comment

Choose a reason for hiding this comment

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

LGTM

@quantizor quantizor merged commit 843b018 into styled-components:master Apr 7, 2020
This was referenced Apr 9, 2020
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.

Styles not generated for Style Objects in a Node VM context

4 participants