summaryrefslogtreecommitdiff
path: root/node_modules/@11ty/eleventy-utils/src/IsPlainObject.js
blob: 1d4c752788ed646ba6ac3c9225fda331eefe6c05 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/* Prior art: this utility was created for https://github.com/11ty/eleventy/issues/2214

 * Inspired by implementations from `is-what`, `typechecker`, `jQuery`, and `lodash`

 * `is-what`
 * More reading at https://www.npmjs.com/package/is-what#user-content-isplainobject-vs-isanyobject
 * if (Object.prototype.toString.call(value).slice(8, -1) !== 'Object') return false;
 * return value.constructor === Object && Object.getPrototypeOf(value) === Object.prototype;

 * `typechecker`
 * return value !== null && typeof value === 'object' && value.__proto__ === Object.prototype;

 * Notably jQuery and lodash have very similar implementations.

 * For later, remember the `value === Object(value)` trick
 */

module.exports = function (value) {
  if (value === null || typeof value !== "object") {
    return false;
  }
  let proto = Object.getPrototypeOf(value);
  return !proto || proto === Object.prototype;
};