UNPKG

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.

22 lines (21 loc) 653 B
/** * Creates a function that invokes the original function only after it's called n times. * @template T * @param {number} times - The number of calls before the original function is invoked. * @param {AnyFunction} fn - The function to restrict. * @returns {T} The new restricted function. * @example * let count = 0; * const incrementAfterThree = after(() => ++count, 3); * incrementAfterThree(); // 0 * incrementAfterThree(); // 0 * incrementAfterThree(); // 1 * incrementAfterThree(); // 2 */ export function after(fn, times) { let count = 0; return ((...args) => { if (++count >= times) fn(args); }); }