createMethodEnhancer
createMethodEnhancer<
K
>(methodName
:K
): <T
>(fn
:Funcware
<MethodOf
<T
,K
>>) =>MethodEnhancer
<K
,T
>
Defined in: ext/methodware/method-enhancer.ts:59
Creates a MethodEnhancer for a method on a target object.
Usage:
- Call with the method name to match (string key).
- Call with a Funcware to wrap/enhance the method.
- Call the resulting enhancer with your target object.
Returns an unenhance function to restore the original method.
See createMethodEnhancer for the enhancer function type returned.
See Funcware for the enhancer function type. Funcware
Type Parameters
Section titled “Type Parameters”Type Parameter |
---|
K extends string |
Parameters
Section titled “Parameters”Parameter | Type |
---|---|
methodName | K |
Returns
Section titled “Returns”<
T
>(fn
:Funcware
<MethodOf
<T
,K
>>):MethodEnhancer
<K
,T
>
Type Parameters
Section titled “Type Parameters”Type Parameter |
---|
T extends HasMethod <K > |
Parameters
Section titled “Parameters”Parameter | Type |
---|---|
fn | Funcware <MethodOf <T , K >> |
Returns
Section titled “Returns”MethodEnhancer
<K
, T
>
Example
Section titled “Example”const enhancer = createMethodEnhancer('foo')(fn => (...args) => { console.log('before', args); const r = fn(...args); console.log('after', r); return r;});const unenhance = enhancer(target);target.foo('will', 'be', 'enhanced');unenhance();target.foo('will', 'NOT be', 'enhanced');
Source
Section titled “Source”This function is useful for dynamically enhancing methods on objects, such as adding logging, instrumentation, or custom behavior. It is commonly used in middleware, plugin, or extension systems where you want to intercept method calls and restore the original implementation when no longer needed.
export const createMethodEnhancer = <K extends string>(methodName: K) => <T extends HasMethod<K>>( fn: Funcware<MethodOf<T, K>> ): MethodEnhancer<K, T> => (target: T) => { return enhanceMethod(target, methodName, fn); };