From 04c9cb49de04e17517fc32c903d7775828eefd53 Mon Sep 17 00:00:00 2001 From: Carlos Mostek Date: Thu, 25 Mar 2021 11:22:07 -0500 Subject: [PATCH] Still pass if constructor key is a primitive --- is-plain-object.js | 21 ++++++++++++++++++++- test/server.js | 3 ++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/is-plain-object.js b/is-plain-object.js index 7c1a1d2..eefc953 100644 --- a/is-plain-object.js +++ b/is-plain-object.js @@ -9,6 +9,25 @@ function isObject(o) { return Object.prototype.toString.call(o) === '[object Object]'; } +/** + * Returns if the encountered value is a native value that we can parse + * or process. This is basically the javascript typeof but with added + * support for null. + * + * @param {*} value + * + * @returns {boolean} + */ +function isPrimitive (value) { + if (value === null) { + return true; + } + return ['undefined', 'boolean', 'number', 'string', 'bigint'].includes( + typeof value + ); +} + + export function isPlainObject(o) { var ctor,prot; @@ -16,7 +35,7 @@ export function isPlainObject(o) { // If has modified constructor ctor = o.constructor; - if (ctor === undefined) return true; + if (ctor === undefined || isPrimitive(ctor)) return true; // If has modified prototype prot = ctor.prototype; diff --git a/test/server.js b/test/server.js index c87143a..baa4537 100644 --- a/test/server.js +++ b/test/server.js @@ -13,12 +13,13 @@ describe('Same-Realm Server Tests', function() { assert(isPlainObject(Object.create({}))); assert(isPlainObject(Object.create(Object.prototype))); assert(isPlainObject({foo: 'bar'})); + assert(isPlainObject({constructor: 'not actually a constructor'})); assert(isPlainObject({})); assert(isPlainObject(Object.create(null))); }); it('should return `false` if the object is not created by the `Object` constructor.', function() { - function Foo() {this.abc = {};}; + function Foo() {this.abc = {};} assert(!isPlainObject(/foo/)); assert(!isPlainObject(function() {}));