-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
33 lines (27 loc) · 1.03 KB
/
index.js
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
/*
* In vanilla JavaScript a POJO (Plain Old JavaScript Object) is the simplest
* kind of object you could possibly have: a set of key-value pairs, created
* by the `{} object literal notation` or constructed with `new Object()`
* reference the jquery.isPlainObject
*/
'use strict';
const isPlainObject = (obj) => {
const hasOwnProperty = Object.prototype.hasOwnProperty;
const toString = Object.prototype.toString;
// Detect obvious negatives use toString to catch host objects
if (obj === null || toString.call(obj) !== '[object Object]') {
return false;
}
const proto = Object.getPrototypeOf(obj);
// Objects with no prototype (e.g., `Object.create( null )`) are plain
if (!proto) {
return true;
}
// Objects with prototype are plain iff constructed by `Object` function
const ctor = hasOwnProperty.call(proto, 'constructor') && proto.constructor;
return (
typeof ctor === 'function' &&
hasOwnProperty.toString.call(ctor) === hasOwnProperty.toString.call(Object)
);
};
module.exports = isPlainObject;