Function
The function module provides a set of utilities for manipulating and composing functions. These utilities help create more flexible and reusable functions, making common operations like memoization, functional composition, and partial argument application easier.
Main features:
- Function composition (
compose,pipe) - Memoization of results (
memo) - Partial argument application (
partialLeft,partialRight) - Utility functions (
identity,noop,not,once) - Conditional execution (
when) - Repeated execution (
times)
Overview
compose
compose<T>(...callbacks: ((value: T) => T)[]): (value: T) => TCreates a composed function that executes a sequence of functions from right to left.
identity
identity<T>(value: T): TReturns the received value without modifications.
memo
memo<T extends (...args: any[]) => any>(callback: T, options?: { cacheTimeout?: number, serializer?: (args: Parameters<T>) => string }): (...args: Parameters<T>) => ReturnType<T>Creates a memoized function that caches the results of previous calls.
noop
noop(): voidA function that does nothing (no operation).
not
not<T extends (...args: any[]) => boolean>(callback: T): (...args: Parameters<T>) => booleanCreates a function that returns the boolean-inverted value of the provided function's result.
once
once<T extends (...args: any[]) => any>(callback: T): (...args: Parameters<T>) => ReturnType<T>Creates a function that can only be executed once; subsequent calls return the result of the first execution.
partialLeft
partialLeft<Args extends any[], R, P extends Partial<Args>>(
callback: (...args: Args) => R,
...partial: P
): (...args: DropFirst<Args> extends P ? [] : DropFirst<Args>) => RCreates a new function with pre-filled arguments on the left.
partialRight
partialRight<Args extends any[], R, P extends Partial<Args>>(
callback: (...args: Args) => R | Promise<R>,
...partial: P
): (...args: DropLast<Args, P>) => R | Promise<R>Creates a new function with pre-filled arguments on the right.
pipe
pipe<T>(...callbacks: ((value: T) => T)[]): (value: T) => TCreates a composed function that executes a sequence of functions from left to right.
times
times<T>(callback: (index: number) => T, count: number): T[]Executes a callback function a specific number of times, returning an array with the results.
when
when<T extends (...args: any) => any>(
predicate: (...args: any) => boolean,
action: T
): (...args: Parameters<T>) => ReturnType<T> | undefinedCreates a function that executes an action only if the predicate is true.
