// #region Functions /** * Get an array with a specified length, filled with indices * * @param length Length of the array * @returns Array of indices * * @example * ```typescript * range(5); // => [0, 1, 2, 3, 4] * ``` */ export function range(length: number): number[]; /** * Get an array of numbers in a specified range * * @param start Starting number _(inclusive)_ * @param end Ending number _(exclusive)_ * @returns Array of numbers in range * * @example * ```typescript * range(2, 5); // => [2, 3, 4] * ``` */ export function range(start: number, end: number): number[]; /** * Get an array of numbers in a specified range with a specified step * * @param start Starting number _(inclusive)_ * @param end Ending number _(exclusive)_ * @param step Step between numbers * @returns Array of numbers in range * * @example * ```typescript * range(0, 10, 2); // => [0, 2, 4, 6, 8] * ``` */ export function range(start: number, end: number, step: number): number[]; export function range(first: number, second?: number, third?: number): number[] { const start = typeof second === 'number' ? first : 0; const end = typeof second === 'number' ? second : first; const step = typeof third === 'number' ? third : 1; const values: number[] = []; if (step === 0) { return values; } if (step > 0) { for (let i = start; i < end; i += step) { values.push(i); } } else { for (let i = start; i > end; i += step) { values.push(i); } } return values; } /** * Get an array with a specified length, filled with indices * * @param length Length of the array * @returns Array of indices * * @example * ```typescript * times(5); // => [0, 1, 2, 3, 4] * ``` */ export function times(length: number): number[]; /** * Get an array with a specified length, filled by values from a callback * * @param length Length of the array * @param callback Callback function to generate values * @returns Array of values generated by the callback * * @example * ```typescript * times(5, index => index * 2); // => [0, 2, 4, 6, 8] * ``` */ export function times unknown>( length: number, callback: Callback, ): ReturnType[]; /** * Get an array with a specified length, filled with a specified value * * @param length Length of the array * @param value Value to fill the array with * @returns Array filled with the specified value * * @example * ```typescript * times(5, 'a'); // => ['a', 'a', 'a', 'a', 'a'] * ``` */ export function times(length: number, value: Value): Value[]; export function times(length: number, value?: unknown): unknown[] { if (typeof length !== 'number' || length <= 0) { return []; } const isFunction = typeof value === 'function'; const values: unknown[] = []; for (let index = 0; index < length; index += 1) { values.push(isFunction ? (value as (index: number) => unknown)(index) : (value ?? index)); } return values; } // #endregion