functions
Variables
after
Creates a function that only executes starting from the n-th call.
The provided function will be invoked starting from the n-th call.
This is particularly useful for scenarios involving events or asynchronous operations where an action should occur only after a certain number of invocations.
Template
The type of the function to be invoked.
Param
The number of calls required for func to execute.
Param
The function to be invoked.
Returns
- A new function that:
- Tracks the number of calls.
- Invokes
funcstarting from then-th call. - Returns
undefinedif fewer thanncalls have been made.
Throws
- Throws an error if
nis negative.
Example
const afterFn = after(3, () => {
console.log("called")
});
// Will not log anything.
afterFn()
// Will not log anything.
afterFn()
// Will log 'called'.
afterFn()
See
Powered by es-toolkit (MIT License): https://github.com/toss/es-toolkit
ary
Creates a function that invokes func, with up to n arguments, ignoring any additional arguments.
Template
The type of the function.
Param
The function to cap arguments for.
Param
The arity cap.
Returns
Returns the new capped function.
Example
function fn(a: number, b: number, c: number) {
return Array.from(arguments);
}
ary(fn, 0)(1, 2, 3) // []
ary(fn, 1)(1, 2, 3) // [1]
ary(fn, 2)(1, 2, 3) // [1, 2]
ary(fn, 3)(1, 2, 3) // [1, 2, 3]
See
Powered by es-toolkit (MIT License): https://github.com/toss/es-toolkit
before
Creates a function that limits the number of times the given function (func) can be called.
Template
The type of the function to be invoked.
Param
The number of times the returned function is allowed to call func before stopping.
- If n is 0, func will never be called.
- If n is a positive integer, func will be called up to n-1 times.
Param
The function to be called with the limit applied.
Returns
- A new function that:
- Tracks the number of calls.
- Invokes
funcuntil then-1-th call. - Returns
undefinedif the number of calls reaches or exceedsn, stopping further calls.
Throws
- Throw an error if
nis negative.
Example
const beforeFn = before(3, () => {
console.log("called");
})
// Will log 'called'.
beforeFn();
// Will log 'called'.
beforeFn();
// Will not log anything.
beforeFn();
See
Powered by es-toolkit (MIT License): https://github.com/toss/es-toolkit
curry
Curries a function, allowing it to be called with a single argument at a time and returning a new function that takes the next argument. This process continues until all arguments have been provided, at which point the original function is called with all accumulated arguments.
Param
The function to curry.
Returns
A curried function.
Example
function noArgFunc() {
return 42;
}
const curriedNoArgFunc = curry(noArgFunc);
console.log(curriedNoArgFunc()); // 42
See
Powered by es-toolkit (MIT License): https://github.com/toss/es-toolkit
curryRight
Curries a function, allowing it to be called with a single argument at a time and returning a new function that takes the next argument. This process continues until all arguments have been provided, at which point the original function is called with all accumulated arguments.
Unlike curry, this function curries the function from right to left.
Param
The function to curry.
Returns
A curried function.
Example
function noArgFunc() {
return 42;
}
const curriedNoArgFunc = curryRight(noArgFunc);
console.log(curriedNoArgFunc()); // 42
See
Powered by es-toolkit (MIT License): https://github.com/toss/es-toolkit
flow
Creates a new function that executes the given functions in sequence. The return value of the previous function is passed as an argument to the next function.
The this context of the returned function is also passed to the functions provided as parameters.
Param
The function to invoke.
Returns
Returns the new composite function.
Example
function noArgFunc() {
return 42;
}
const combined = flow(noArgFunc);
console.log(combined()); // 42
See
Powered by es-toolkit (MIT License): https://github.com/toss/es-toolkit
flowRight
Creates a new function that executes the given functions in sequence from right to left. The return value of the previous function is passed as an argument to the next function.
The this context of the returned function is also passed to the functions provided as parameters.
This method is like flow except that it creates a function that invokes the given functions from right to left.
Param
The function to invoke.
Returns
Returns the new composite function.
Example
function noArgFunc() {
return 42;
}
const combined = flowRight(noArgFunc);
console.log(combined()); // 42
See
Powered by es-toolkit (MIT License): https://github.com/toss/es-toolkit
identity
Returns the input value unchanged.
Template
The type of the input value.
Param
The value to be returned.
Returns
The input value.
Examples
See
Powered by es-toolkit (MIT License): https://github.com/toss/es-toolkit
memoize
Creates a memoized version of the provided function. The memoized function caches results based on the argument it receives, so if the same argument is passed again, it returns the cached result instead of recomputing it.
This function works with functions that take zero or just one argument. If your function originally takes multiple arguments, you should refactor it to take a single object or array that combines those arguments.
If the argument is not primitive (e.g., arrays or objects), provide a
getCacheKey function to generate a unique cache key for proper caching.
Template
The type of the function to be memoized.
Param
The function to be memoized. It should accept a single argument and return a value.
Param
Optional configuration for the memoization.
Param
The cache object used to store results. Defaults to a new Map.
Param
An optional function to generate a unique cache key for each argument.
Returns
The memoized function with an additional cache property that exposes the internal cache.
Examples
// Example using the default cache
const add = (x: number) => x + 10;
const memoizedAdd = memoize(add);
console.log(memoizedAdd(5)); // 15
console.log(memoizedAdd(5)); // 15 (cached result)
console.log(memoizedAdd.cache.size); // 1
// Example using a custom resolver
const sum = (arr: number[]) => arr.reduce((x, y) => x + y, 0);
const memoizedSum = memoize(sum, { getCacheKey: (arr: number[]) => arr.join(',') });
console.log(memoizedSum([1, 2])); // 3
console.log(memoizedSum([1, 2])); // 3 (cached result)
console.log(memoizedSum.cache.size); // 1
// Example using a custom cache implementation
class CustomCache<K, T> implements MemoizeCache<K, T> {
private cache = new Map<K, T>();
set(key: K, value: T): void {
this.cache.set(key, value);
}
get(key: K): T | undefined {
return this.cache.get(key);
}
has(key: K): boolean {
return this.cache.has(key);
}
delete(key: K): boolean {
return this.cache.delete(key);
}
clear(): void {
this.cache.clear();
}
get size(): number {
return this.cache.size;
}
}
const customCache = new CustomCache<string, number>();
const memoizedSumWithCustomCache = memoize(sum, { cache: customCache });
console.log(memoizedSumWithCustomCache([1, 2])); // 3
console.log(memoizedSumWithCustomCache([1, 2])); // 3 (cached result)
console.log(memoizedAddWithCustomCache.cache.size); // 1
See
Powered by es-toolkit (MIT License): https://github.com/toss/es-toolkit
negate
Creates a function that negates the result of the predicate function.
Template
The type of the function to negate.
Param
The function to negate.
Returns
The new negated function, which negates the boolean result of func.
Example
const array = [1, 2, 3, 4, 5, 6];
const isEven = (n: number) => n % 2 === 0;
const result = array.filter(negate(isEven));
// result will be [1, 3, 5]
See
Powered by es-toolkit (MIT License): https://github.com/toss/es-toolkit
noop
A no-operation function that does nothing. This can be used as a placeholder or default function.
Example
Returns
This function does not return anything.
See
Powered by es-toolkit (MIT License): https://github.com/toss/es-toolkit
once
Creates a function that is restricted to invoking func once. Repeat calls to the function return the value of the first invocation.
Template
The type of the function.
Param
The function to restrict.
Returns
Returns the new restricted function.
Example
const initialize = once(createApplication);
initialize();
initialize();
// => `createApplication` is invoked once
See
Powered by es-toolkit (MIT License): https://github.com/toss/es-toolkit
partial
Creates a function that invokes func with partialArgs prepended to the arguments it receives. This method is like bind except it does not alter the this binding.
The partial.placeholder value, which defaults to a symbol, may be used as a placeholder for partially applied arguments.
Note: This method doesn't set the length property of partially applied functions.
Template
The type of the first argument.
Template
The return type of the function.
Param
The function to partially apply.
Param
The first argument to apply.
Returns
A new function that takes no arguments and returns the result of the original function.
Example
const addOne = (x: number) => x + 1;
const addOneToFive = partial(addOne, 5);
console.log(addOneToFive()); // => 6
See
Powered by es-toolkit (MIT License): https://github.com/toss/es-toolkit
partialRight
This method is like partial except that partially applied arguments are appended to the arguments it receives.
The partialRight.placeholder value, which defaults to a symbol, may be used as a placeholder for partially applied arguments.
Note: This method doesn't set the length property of partially applied functions.
Template
The return type of the function.
Param
The function to invoke.
Returns
Returns the new function.
Example
const getValue = () => 42;
const getValueFunc = partialRight(getValue);
console.log(getValueFunc()); // => 42
See
Powered by es-toolkit (MIT License): https://github.com/toss/es-toolkit
rest
Creates a function that transforms the arguments of the provided function func.
The transformed arguments are passed to func such that the arguments starting from a specified index
are grouped into an array, while the previous arguments are passed as individual elements.
Template
The type of the function being transformed.
Param
The function whose arguments are to be transformed.
Param
The index from which to start grouping the remaining arguments into an array.
Defaults to func.length - 1, grouping all arguments after the last parameter.
Returns
A new function that, when called, returns the result of calling func with the transformed arguments.
The transformed arguments are:
- The first start arguments as individual elements.
- The remaining arguments from index start onward grouped into an array.
Example
function fn(a, b, c) {
return [a, b, c];
}
// Using default start index (func.length - 1, which is 2 in this case)
const transformedFn = rest(fn);
console.log(transformedFn(1, 2, 3, 4)); // [1, 2, [3, 4]]
// Using start index 1
const transformedFnWithStart = rest(fn, 1);
console.log(transformedFnWithStart(1, 2, 3, 4)); // [1, [2, 3, 4]]
// With fewer arguments than the start index
console.log(transformedFn(1)); // [1, undefined, []]
See
Powered by es-toolkit (MIT License): https://github.com/toss/es-toolkit
spread
Creates a new function that spreads elements of an array argument into individual arguments for the original function.
Template
A function type with any number of parameters and any return type.
Param
The function to be transformed. It can be any function with any number of arguments.
Returns
- A new function that takes an array of arguments and returns the result of calling the original function with those arguments.
Example
function add(a, b) {
return a + b;
}
const spreadAdd = spread(add);
console.log(spreadAdd([1, 2])); // Output: 3
See
Powered by es-toolkit (MIT License): https://github.com/toss/es-toolkit
unary
Creates a function that accepts up to one argument, ignoring any additional arguments.
Template
The type of the function.
Param
The function to cap arguments for.
Returns
Returns the new capped function.
Example
See
Powered by es-toolkit (MIT License): https://github.com/toss/es-toolkit