Const
Rest
...funcs: ((arg) => any)[]The functions to compose
A function that is the composition of all provided functions
const addOne = (x: number) => x + 1;
const double = (x: number) => x * 2;
const addOneAndDouble = FunctionUtils.compose(double, addOne);
addOneAndDouble(5); // Returns 12 (5 + 1 = 6, then 6 * 2 = 12)
Creates a debounced function that delays invoking the provided function until after the specified wait time has elapsed since the last time it was invoked.
A debounced version of the provided function
const debouncedSearch = FunctionUtils.debounce((query: string) => {
// Perform search
}, 300);
// Will only execute after 300ms of no calls
debouncedSearch("test");
Creates a function that delays invoking the provided function until the current call stack has cleared.
A deferred version of the provided function
Rest
...args: Tconst deferredLog = FunctionUtils.defer((message: string) => {
console.log(message);
});
// Will execute after current execution context
deferredLog("Hello");
Creates a function that can only be called once. Subsequent calls will return the result of the first call.
A function that can only be called once
const initialize = FunctionUtils.once(() => {
// Expensive initialization
return "initialized";
});
initialize(); // Returns "initialized"
initialize(); // Returns "initialized" without executing the function again
Creates a throttled function that only invokes the provided function at most once per every wait milliseconds.
A throttled version of the provided function
const throttledScroll = FunctionUtils.throttle(() => {
// Handle scroll event
}, 100);
// Will execute at most once every 100ms
window.addEventListener('scroll', throttledScroll);
Composes multiple functions into a single function, where the output of each function is passed as input to the next function.