File size: 494 Bytes
1f122c3
d0c63f4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

export function pick<T>(items: T[], defaultValue: T, {
  skipList = [],
  maxRetries = 10
}: {
  skipList?: T[]
  maxRetries?: number
} = {
  skipList: [],
  maxRetries: 10
}): T {
  let candidate: T | undefined = undefined
  for (let i = 0; i < maxRetries; i++) {
    candidate = items[Math.floor(Math.random() * items.length)] as T
    if (skipList.includes(candidate)) { continue }
  }
  if (typeof candidate === "undefined") {
    return defaultValue
  } else {
    return candidate
  }
}