es-next-tools
Version:
A comprehensive utility library for JavaScript and TypeScript that provides a wide range of functions for common programming tasks, including mathematical operations, date manipulations, array and object handling, string utilities, and more.
15 lines (14 loc) • 630 B
TypeScript
import type { AnyFunction } from "..";
/**
* Creates a function that invokes the original function only before it's called n times.
* @param {number} times - The number of calls before the original function stops being invoked.
* @param {Function} fn - The function to restrict.
* @returns {Function} The new restricted function.
* @example
* let count = 0;
* const incrementBeforeThree = before(() => ++count, 3);
* incrementBeforeThree(); // 1
* incrementBeforeThree(); // 2
* incrementBeforeThree(); // 2 (doesn't increment anymore)
*/
export declare function before<T extends AnyFunction>(fn: T, times: number): T;