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

Still pass if constructor key is a primitive #27

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
21 changes: 20 additions & 1 deletion is-plain-object.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,33 @@ 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;

if (isObject(o) === false) return false;

// 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;
Expand Down
3 changes: 2 additions & 1 deletion test/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}));
Expand Down