@pandorajs/hub
Version:
pandora.js messenge hub
57 lines • 1.78 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.IntrospectionUtils = void 0;
const SKIP_NAMES = ['subscribe', 'unsubscribe'];
class IntrospectionUtils {
/**
* Introspect an Object, get the Definition of that Object
* @param obj
* @return {Introspection}
*/
static introspect(obj) {
const allNames = getAllNames(obj);
const properties = [];
const methods = [];
for (const name of allNames) {
if (IntrospectionUtils.namesOfObject.indexOf(name) > -1 ||
SKIP_NAMES.indexOf(name) > -1) {
continue;
}
if (typeof obj[name] === 'function') {
methods.push({
name: name,
length: obj[name].length,
type: IntrospectionUtils.isGenerator(obj[name])
? 'generator'
: 'function',
});
}
else {
properties.push({
name: name,
type: typeof obj[name],
});
}
}
return { properties, methods };
}
/**
* decide a function is a generator function
* @param fn
* @return {boolean}
*/
static isGenerator(fn) {
return fn.constructor.name === 'GeneratorFunction';
}
}
exports.IntrospectionUtils = IntrospectionUtils;
IntrospectionUtils.namesOfObject = getAllNames({});
function getAllNames(obj) {
let props = [];
do {
props = props.concat(Object.getOwnPropertyNames(obj));
} while ((obj = Object.getPrototypeOf(obj)));
props = [...new Set(props)];
return props;
}
//# sourceMappingURL=IntrospectionUtils.js.map