UNPKG

surrogate

Version:

Object method hooks made easy

50 lines (45 loc) 1.99 kB
import {isUndefined as $8c13fb1fbdc14b86$export$fce6876652108ab} from "./surrogate.68fc8564.js"; class $5373dd1107ba7dc2$export$1f0bc0d8e047b444 { constructor(instance){ this.instance = instance; } doesNotIncludeEvent(event, methods) { return !this.doesIncludeEvent(event) || !this.matchEvent(event, methods); } doesIncludeEvent(event) { return this.instanceMethodNames().includes(event); } matchEvent(event, methods) { return methods.filter((v)=>v).map((method)=>new RegExp(method)).some((regex)=>regex.test(event)); } getApplicableMethods(event, methods) { const methodTest = new RegExp(event); return methods.includes(event) ? [ event ] : methods.filter((method)=>methodTest.test(method)); } instanceMethodNames() { const prototype = Reflect.getPrototypeOf(this.instance) || {}; const properties = this.getPropertyNames(); return properties.filter((prop)=>prop !== 'constructor').filter((name)=>this.isNotProperty(name)).filter((name)=>this.isNotAccessor(name, prototype)); } isNotProperty(name) { const descriptor = Reflect.getOwnPropertyDescriptor(this.instance, name); return (0, $8c13fb1fbdc14b86$export$fce6876652108ab)(descriptor); } isNotAccessor(name, prototype) { const descriptor = Reflect.getOwnPropertyDescriptor(prototype, name); return (0, $8c13fb1fbdc14b86$export$fce6876652108ab)(descriptor?.get) && (0, $8c13fb1fbdc14b86$export$fce6876652108ab)(descriptor?.set); } getPropertyNames() { const props = []; let current = this.instance; do props.push(Object.getOwnPropertyNames(current)); while (current = Reflect.getPrototypeOf(current)); return [ ...new Set(props.flat()).values() ]; } } export {$5373dd1107ba7dc2$export$1f0bc0d8e047b444 as MethodIdentifier}; //# sourceMappingURL=surrogate.d934c73c.js.map