Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | 2x 2x 2x 8x | import { Mapper } from "@benneq/function";
/**
* Generates an {@link Array} with a given length, and calls a {@link Function}
* for each index to determine it's value.
*
* @example
* ```ts
* const result = generate(3, (index) => -index);
*
* console.log(result); // [-1, -2, -3]
* ```
*
* @typeParam T - the {@link Array} value type
* @param length - the {@link Array} length
* @param mapper - the {@link Function} to call for each index
* @returns the generated {@link Array}
*/
export const generate = <T>(length: number, mapper: Mapper<number, T>): T[] => {
if (process.env.NODE_ENV !== "production") {
console.assert(length >= 0);
}
return [...Array<T>(length)].map((_value, index) => mapper(index));
};
|