From 71cadd1fa549937c1a2e9d8d7577361d0dfde321 Mon Sep 17 00:00:00 2001 From: ehmicky Date: Mon, 13 Jun 2022 20:38:28 +0200 Subject: [PATCH 1/2] Make it work across realms --- index.js | 2 +- readme.md | 5 +++++ test.js | 2 ++ 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 418b079..e4deb75 100644 --- a/index.js +++ b/index.js @@ -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; } diff --git a/readme.md b/readme.md index d63a165..db3e540 100644 --- a/readme.md +++ b/readme.md @@ -14,6 +14,7 @@ $ npm install is-plain-obj ```js import isPlainObject from 'is-plain-obj'; +import {runInNewContext} from 'vm'; isPlainObject({foo: 'bar'}); //=> true @@ -24,6 +25,10 @@ isPlainObject(new Object()); isPlainObject(Object.create(null)); //=> true +// This works across realms +isPlainObject(runInNewContext('({})')); +//=> true + isPlainObject([1, 2, 3]); //=> false diff --git a/test.js b/test.js index 993e9a8..913cebe 100644 --- a/test.js +++ b/test.js @@ -1,5 +1,6 @@ import test from 'ava'; import isPlainObject from './index.js'; +import {runInNewContext} from 'vm'; function Foo(x) { this.x = x; @@ -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)); From c564b3b8336834ef89e39f2fb498154fc068b8f5 Mon Sep 17 00:00:00 2001 From: ehmicky Date: Tue, 14 Jun 2022 15:24:04 +0200 Subject: [PATCH 2/2] Fix index.d.ts --- index.d.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/index.d.ts b/index.d.ts index 810df73..84c0277 100644 --- a/index.d.ts +++ b/index.d.ts @@ -6,6 +6,7 @@ An object is plain if it's created by either `{}`, `new Object()`, or `Object.cr @example ``` import isPlainObject from 'is-plain-obj'; +import {runInNewContext} from 'vm'; isPlainObject({foo: 'bar'}); //=> true @@ -16,6 +17,10 @@ isPlainObject(new Object()); isPlainObject(Object.create(null)); //=> true +// This works across realms +isPlainObject(runInNewContext('({})')); +//=> true + isPlainObject([1, 2, 3]); //=> false