UNPKG

clone-class

Version:

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

38 lines 1.51 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.interfaceOfClass = void 0; const interfaceOfClass = (Klass) => { /** * Get properties of a class * @see https://stackoverflow.com/a/40637896/1123955 */ /** * Huan(202110): we decide not to check the instance properties * because the instance properties are not always the same as the class properties */ // const instance = new Klass() // const instanceProperties = Object.getOwnPropertyNames(instance) const propertySet = new Set(); let currentPrototype = Klass.prototype; while (currentPrototype && currentPrototype !== Object.prototype) { Object .getOwnPropertyNames(currentPrototype) .filter(property => property !== 'constructor') // ignore `constructor` .filter(property => !property.startsWith('_')) // ignore property names starting with `_` .forEach(prop => propertySet.add(prop)); currentPrototype = Object.getPrototypeOf(currentPrototype); } // console.info('propertySet:', propertySet) if (propertySet.size === 0) { throw new Error(`${Klass.name} has no prototype properties`); } return () => (target) => { if (target instanceof Object) { return [...propertySet] .every(prop => prop in target); } return false; }; }; exports.interfaceOfClass = interfaceOfClass; //# sourceMappingURL=interface-of-class.js.map