pandora-hub
Version:
pandora.js messenge hub
53 lines • 1.67 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
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';
}
}
IntrospectionUtils.namesOfObject = getAllNames({});
exports.IntrospectionUtils = IntrospectionUtils;
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