times
util
Invokes the iteratee n times, returning an array of the results of each invocation.
Installation
Import
import { times } from '@tulx/utils';Source Code
Implementation
/**
* Invokes the iteratee n times, returning an array of the results of each invocation.
*
* @param n - The number of times to invoke iteratee.
* @param iteratee - The function invoked per iteration.
* @returns Returns the array of results.
*
* @example
* ```ts
* times(3, String); // ['0', '1', '2']
* times(4, () => 0); // [0, 0, 0, 0]
* ```
*/
export function times<T>(n: number, iteratee: (index: number) => T): T[] {
const result: T[] = [];
for (let i = 0; i < n; i++) {
result.push(iteratee(i));
}
return result;
}
Example
import { times } from '@tulx/utils';
times(3, String); // ['0', '1', '2']
times(4, () => 0); // [0, 0, 0, 0]Related Functions
attempt
Attempts to invoke func, returning either the result or the caught error object.
bindAll
Binds methods of an object to the object itself, overwriting the existing method.
cond
Creates a function that iterates over pairs and invokes the corresponding function of the first predicate to return truthy.
conforms
Creates a function that invokes the predicate properties of source with the corresponding property values of a given object.
constant
Creates a function that returns value.
defaultTo
Checks value to determine whether a default value should be returned in its place.