Files
meteor/packages/check/isPlainObject.js
James Burgess 20e89b9009 Modernize check package (#9638)
I've reverted match properties that return class instances back to normal functions, so that they can be called as constructors.

Also, I added tests to make sure we catch this issue if someone else gets the same idea that I had!
2018-03-07 11:36:48 -05:00

37 lines
945 B
JavaScript

// Copy of jQuery.isPlainObject for the server side from jQuery v3.1.1.
const class2type = {};
const toString = class2type.toString;
const hasOwn = Object.prototype.hasOwnProperty;
const fnToString = hasOwn.toString;
const ObjectFunctionString = fnToString.call(Object);
const getProto = Object.getPrototypeOf;
export const isPlainObject = obj => {
let proto;
let Ctor;
// Detect obvious negatives
// Use toString instead of jQuery.type to catch host objects
if (!obj || toString.call(obj) !== '[object Object]') {
return false;
}
proto = getProto(obj);
// Objects with no prototype (e.g., `Object.create( null )`) are plain
if (!proto) {
return true;
}
// Objects with prototype are plain iff they were constructed by a global Object function
Ctor = hasOwn.call(proto, 'constructor') && proto.constructor;
return typeof Ctor === 'function' &&
fnToString.call(Ctor) === ObjectFunctionString;
};