/
isPlainObject.ts
35 lines (28 loc) · 971 Bytes
/
isPlainObject.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
function isObject(o: unknown): o is Record<string, unknown> {
return o != null && typeof o === "object" && Array.isArray(o) === false;
}
function isObjectObject(o: unknown): o is Object & Record<string, unknown> {
var type = Object.prototype.toString.call(o);
return isObject(o) === true &&
(type === "[object Object]" || type === "[object Null]");
}
export default function isPlainObject(o: unknown): o is Object {
let constructor, prototype;
if (!isObjectObject(o)) {
return false;
}
// If has modified constructor
constructor = o.constructor;
if (constructor === undefined) return true;
// If has modified prototype
prototype = constructor.prototype;
if (isObjectObject(prototype) === false) return false;
// If constructor does not have an Object-specific method
if (
Object.prototype.hasOwnProperty.call(prototype, "isPrototypeOf") === false
) {
return false;
}
// Most likely a plain Object
return true;
}