-
-
Notifications
You must be signed in to change notification settings - Fork 275
Expand file tree
/
Copy patharray.ts
More file actions
25 lines (20 loc) · 585 Bytes
/
array.ts
File metadata and controls
25 lines (20 loc) · 585 Bytes
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
export async function eachParallel<T>(times: number, fn: (index: number) => Promise<T>) {
const promises = []
for (let i = 0; i < times; i++) {
promises.push(fn(i))
}
return Promise.all(promises)
}
export function pickRandomFromArray<T>(arr: T[]): T {
return arr[Math.floor(Math.random() * arr.length)]
}
export function pickRandomRangeFromArray<T>(arr: T[], range: number): T[] {
if (arr.length <= range) {
return arr
}
const result = new Set<T>()
while (result.size < range) {
result.add(pickRandomFromArray(arr))
}
return Array.from(result)
}