|
Last change
on this file was a762898, checked in by istevanoska <ilinastevanoska@…>, 5 months ago |
|
Added visualizations
|
-
Property mode
set to
100644
|
|
File size:
1.1 KB
|
| Rev | Line | |
|---|
| [a762898] | 1 | /**
|
|---|
| 2 | * Creates a function that limits the number of times the given function (`func`) can be called.
|
|---|
| 3 | *
|
|---|
| 4 | * @template F - The type of the function to be invoked.
|
|---|
| 5 | * @param {number} n - The number of times the returned function is allowed to call `func` before stopping.
|
|---|
| 6 | * - If `n` is 0, `func` will never be called.
|
|---|
| 7 | * - If `n` is a positive integer, `func` will be called up to `n-1` times.
|
|---|
| 8 | * @param {F} func - The function to be called with the limit applied.
|
|---|
| 9 | * @returns {(...args: Parameters<F>) => ReturnType<F> | undefined} - A new function that:
|
|---|
| 10 | * - Tracks the number of calls.
|
|---|
| 11 | * - Invokes `func` until the `n-1`-th call.
|
|---|
| 12 | * - Returns `undefined` if the number of calls reaches or exceeds `n`, stopping further calls.
|
|---|
| 13 | * @throws {Error} - Throw an error if `n` is negative.
|
|---|
| 14 | * @example
|
|---|
| 15 | *
|
|---|
| 16 | * const beforeFn = before(3, () => {
|
|---|
| 17 | * console.log("called");
|
|---|
| 18 | * })
|
|---|
| 19 | *
|
|---|
| 20 | * // Will log 'called'.
|
|---|
| 21 | * beforeFn();
|
|---|
| 22 | *
|
|---|
| 23 | * // Will log 'called'.
|
|---|
| 24 | * beforeFn();
|
|---|
| 25 | *
|
|---|
| 26 | * // Will not log anything.
|
|---|
| 27 | * beforeFn();
|
|---|
| 28 | */
|
|---|
| 29 | declare function before<F extends (...args: any[]) => any>(n: number, func: F): (...args: Parameters<F>) => ReturnType<F> | undefined;
|
|---|
| 30 |
|
|---|
| 31 | export { before };
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.