Skip to content

Commit

Permalink
feat: add numberArray and use it in times and asyncTimes
Browse files Browse the repository at this point in the history
  • Loading branch information
Arcath committed Apr 5, 2023
1 parent 08836f0 commit 15803fc
Showing 1 changed file with 20 additions and 14 deletions.
34 changes: 20 additions & 14 deletions src/functions/times.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,30 @@
import {asyncMap} from './async-map'

/**
* Build an array of the numbers from `start` to `end`
*
* @param start number to start from
* @param end number to end at
* @returns an array of numbers
*/
export const numberArray = (start: number, end: number) => {
const a: number[] = []

for (let i = start; i <= end; i++) {
a.push(i)
}

return a
}

/**
* Repeat the given function `number` times
*
* @param number The number of times to itterate
* @param cb The function to run
*/
export const times = <T>(number: number, cb: (i: number) => T): T[] => {
const result: T[] = []
for (let i = 1; i <= number; i++) {
result.push(cb(i))
}

return result
return numberArray(1, number).map(cb)
}

/**
Expand All @@ -25,13 +37,7 @@ export const asyncTimes = async <T>(
number: number,
cb: (i: number) => Promise<T>
): Promise<T[]> => {
const numbers: number[] = []

for (let i = 1; i <= number; i++) {
numbers.push(i)
}

return asyncMap(numbers, cb)
return asyncMap(numberArray(1, number), cb)
}

// {times, asyncTimes}
// {numberArray, times, asyncTimes}

0 comments on commit 15803fc

Please sign in to comment.