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

Make it work across realms #14

Merged
merged 2 commits into from
Jun 14, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ export default function isPlainObject(value) {
}

const prototype = Object.getPrototypeOf(value);
return prototype === null || prototype === Object.prototype;
return prototype === null || Object.getPrototypeOf(prototype) === null;
}
5 changes: 5 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ $ npm install is-plain-obj

```js
import isPlainObject from 'is-plain-obj';
import {runInNewContext} from 'vm';

isPlainObject({foo: 'bar'});
//=> true
Expand All @@ -24,6 +25,10 @@ isPlainObject(new Object());
isPlainObject(Object.create(null));
//=> true

// This works across realms
isPlainObject(runInNewContext('({})'));
//=> true
Copy link
Owner

Choose a reason for hiding this comment

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

Can you update index.d.ts too?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed 👍


isPlainObject([1, 2, 3]);
//=> false

Expand Down
2 changes: 2 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import test from 'ava';
import isPlainObject from './index.js';
import {runInNewContext} from 'vm';

function Foo(x) {
this.x = x;
Expand All @@ -16,6 +17,7 @@ test('main', t => {
t.true(isPlainObject({valueOf: 0}));
t.true(isPlainObject(Object.create(null)));
t.true(isPlainObject(new Object())); // eslint-disable-line no-new-object
t.true(isPlainObject(runInNewContext('({})')));
t.false(isPlainObject(['foo', 'bar']));
t.false(isPlainObject(new Foo(1)));
t.false(isPlainObject(Math));
Expand Down