Files
writer-helpers-mcp/helpers/shuffle.ts

12 lines
478 B
TypeScript

const fishersYatesShuffle = <Type>(arr: Type[]): Type[] => {
const shuffled = [...arr]
for (let index = shuffled.length - 1; index > 0; index--) {
const secondIndex = Math.floor(Math.random() * (index + 1))
// biome-ignore lint/style/noNonNullAssertion: indices are guaranteed to be valid in Fisher-Yates shuffle
;[shuffled[index]!, shuffled[secondIndex]!] = [shuffled[secondIndex]!, shuffled[index]!]
}
return shuffled
}
export default fishersYatesShuffle