wrap
function
Creates a function that provides value to wrapper as its first argument.
Installation
Import
import { wrap } from '@tulx/utils';Source Code
Implementation
/**
* Creates a function that provides value to wrapper as its first argument.
*
* @param value - The value to wrap.
* @param wrapper - The wrapper function.
* @returns Returns the new function.
*
* @example
* ```ts
* const p = wrap(escape, (func: typeof escape, text: string) => '<p>' + func(text) + '</p>');
* p('fred, barney, & pebbles'); // '<p>fred, barney, & pebbles</p>'
* ```
*/
export function wrap<T, TArgs extends unknown[], TResult>(
value: T,
wrapper: (value: T, ...args: TArgs) => TResult
): (...args: TArgs) => TResult {
return function (this: unknown, ...args: TArgs): TResult {
return wrapper(value, ...args);
};
}
Example
import { wrap } from '@tulx/utils';
const p = wrap(escape, (func: typeof escape, text: string) => '<p>' + func(text) + '</p>');
p('fred, barney, & pebbles'); // '<p>fred, barney, & pebbles</p>'Related Functions
after
The opposite of before; this method creates a function that invokes func once it's called n or more times.
ary
Creates a function that invokes func, with up to n arguments, ignoring any additional arguments.
before
Creates a function that invokes func, with the this binding and arguments of the created function, while it's called less than n times.
bind
Creates a function that invokes func with the this binding of thisArg and partials prepended to the arguments it receives.
bindKey
Creates a function that invokes the method at object[key] with partials prepended to the arguments it receives.
curry
Creates a function that accepts arguments of func and either invokes func returning its result, or returns a function that accepts the remaining arguments.