Skip to content

Commit d8d416e

Browse files
committed
Use better isPlainObject
1 parent ca47a0a commit d8d416e

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

src/utils/isPlainObject.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
1+
const fnToString = (fn) => Function.prototype.toString.call(fn);
2+
3+
/**
4+
* @param {any} obj The object to inspect.
5+
* @returns {boolean} True if the argument appears to be a plain object.
6+
*/
17
export default function isPlainObject(obj) {
2-
return obj ? typeof obj === 'object' && Object.getPrototypeOf(obj) === Object.prototype : false;
8+
if (!obj || typeof obj !== 'object') {
9+
return false;
10+
}
11+
12+
const proto = typeof obj.constructor === 'function' ?
13+
Object.getPrototypeOf(obj) :
14+
Object.prototype;
15+
16+
if (proto === null) {
17+
return true;
18+
}
19+
20+
const constructor = proto.constructor;
21+
22+
return typeof constructor === 'function'
23+
&& constructor instanceof constructor
24+
&& fnToString(constructor) === fnToString(Object);
325
}

0 commit comments

Comments
 (0)