UNPKG

clone-class

Version:

Clone an ES6 Class as Another Class Name for Isolating Class Static Properties.

62 lines 1.53 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.looseInstanceOfClass = void 0; /** * Huan(202011) * Create a `looseInstanceOfClass` to check `FileBox` and `Puppet` instances #2090 * https://github.com/wechaty/wechaty/issues/2090 * * `instanceof`: checking by constructor name. */ const looseInstanceOfClass = (Klass) => (target) => { /** * Easy way */ if (target instanceof Klass) { /** * Singleton Module */ return true; } /** * * Hard (loose) way * */ if (!target || typeof target !== 'object') { return false; } if (typeof target.constructor !== 'function') { /** * Not a class? */ return false; } if (target.constructor.name === Klass.name) { /** * Different Module Class with the same name * with a direct instance class */ return true; } const parent = Reflect.getPrototypeOf(target.constructor.prototype); if (!parent) { /** * No parent class */ return false; } if (parent.constructor.name === Klass.name) { /** * Different Module class with the same name * but the instance is a child class */ return true; } /** * Not match any of the above */ return false; }; exports.looseInstanceOfClass = looseInstanceOfClass; //# sourceMappingURL=loose-instance-of-class.js.map