chore: move utils to package
This commit is contained in:
19
packages/utils/array.ts
Normal file
19
packages/utils/array.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
/**
|
||||
* Return random elements from an array
|
||||
*/
|
||||
export const getRandomItems = <T> (array: T[], amount: number): T[] => {
|
||||
const shuffled = array.slice()
|
||||
for (let i = shuffled.length - 1; i > 0; i--) {
|
||||
const j = Math.floor(Math.random() * (i + 1));
|
||||
[shuffled[i], shuffled[j]] = [shuffled[j], shuffled[i]]
|
||||
}
|
||||
return shuffled.slice(0, amount)
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Return a random element from an array
|
||||
*/
|
||||
export const getRandomItem = <T extends Array<unknown>> (array: T): T[0] => {
|
||||
return getRandomItems(array, 1)[0]
|
||||
}
|
||||
Reference in New Issue
Block a user