UNPKG

is-explicit

Version:

Combines instance of operator and typeof operator in a way that works seamlessly with objects and literals.

50 lines (35 loc) 2.04 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _is = _interopRequireWildcard(require("./is")); var _isInstanceable = _interopRequireDefault(require("./is-instanceable")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } } /******************************************************************************/ // Prototype Test /******************************************************************************/ function isConstructorOrInstanceOf(prototype, type) { return prototype.constructor === type || prototype instanceof type; } /******************************************************************************/ // Main /******************************************************************************/ function isSubclassOf(value, type) { if (this != null && this !== _is.default) type = this; const arrayOfTypes = Array.isArray(type); if (!(0, _is.typeIsValid)(type, arrayOfTypes)) throw new Error('is.subclassOf requires at least one type.'); if (!(0, _isInstanceable.default)(value)) return false; const { prototype } = value; if (!arrayOfTypes) return isConstructorOrInstanceOf(prototype, type); for (const t of type) if (isConstructorOrInstanceOf(prototype, t)) return true; return false; } /******************************************************************************/ // Exports /******************************************************************************/ var _default = isSubclassOf; exports.default = _default;