Skip to content

enhanceFunction

enhanceFunction<F>(original: F): EnhancedFunc<F>

Defined in: ext/methodware/enhance-function.ts:79

Extends a method on a single target using funcware, allowing you to intercept and augment the method’s behavior. The funcware receives the original method and its parameters, and can return a new value or modify the call.

Usage:

const disposer = enhanceMethod(obj, 'greet', (next) => (name) => {
console.log('Calling greet with', name);
return next(name);
});

This function does not return the enhanced method itself. Instead, it replaces the method on the target object and returns a disposer function that restores whatever was there before.

Type parameters T and K are usually inferred automatically and rarely need to be specified.

Type Parameter
F extends Func
ParameterType
originalF

EnhancedFunc<F>

A disposer function that restores the previous method when called

This function is useful for dynamically extending or wrapping 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.

// Example: Enhance a method to log calls
const obj = { greet(name) { return `Hello, ${name}`; } };
const disposer = enhanceMethod(obj, 'greet', (next) => (name) => {
console.log('Calling greet with', name);
return next(name);
});
obj.greet('World'); // Logs and returns 'Hello, World'
disposer(); // Restores original method