Skip to content

Commit 15803fc

Browse files
committed
feat: add numberArray and use it in times and asyncTimes
1 parent 08836f0 commit 15803fc

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

src/functions/times.ts

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,30 @@
11
import {asyncMap} from './async-map'
22

3+
/**
4+
* Build an array of the numbers from `start` to `end`
5+
*
6+
* @param start number to start from
7+
* @param end number to end at
8+
* @returns an array of numbers
9+
*/
10+
export const numberArray = (start: number, end: number) => {
11+
const a: number[] = []
12+
13+
for (let i = start; i <= end; i++) {
14+
a.push(i)
15+
}
16+
17+
return a
18+
}
19+
320
/**
421
* Repeat the given function `number` times
522
*
623
* @param number The number of times to itterate
724
* @param cb The function to run
825
*/
926
export const times = <T>(number: number, cb: (i: number) => T): T[] => {
10-
const result: T[] = []
11-
for (let i = 1; i <= number; i++) {
12-
result.push(cb(i))
13-
}
14-
15-
return result
27+
return numberArray(1, number).map(cb)
1628
}
1729

1830
/**
@@ -25,13 +37,7 @@ export const asyncTimes = async <T>(
2537
number: number,
2638
cb: (i: number) => Promise<T>
2739
): Promise<T[]> => {
28-
const numbers: number[] = []
29-
30-
for (let i = 1; i <= number; i++) {
31-
numbers.push(i)
32-
}
33-
34-
return asyncMap(numbers, cb)
40+
return asyncMap(numberArray(1, number), cb)
3541
}
3642

37-
// {times, asyncTimes}
43+
// {numberArray, times, asyncTimes}

0 commit comments

Comments
 (0)