Files
mtdb_movie/common/resources/client/utils/array/shuffle-array.ts
maher 703f50a09d
Some checks failed
Build / run (push) Has been cancelled
first commit
2025-10-29 11:42:25 +01:00

23 lines
504 B
TypeScript
Executable File

export function shuffleArray(items: any[], keepFirst = false) {
let first = keepFirst ? items.shift() : null;
let currentIndex = items.length,
temporaryValue,
randomIndex;
while (0 !== currentIndex) {
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
temporaryValue = items[currentIndex];
items[currentIndex] = items[randomIndex];
items[randomIndex] = temporaryValue;
}
if (first) {
items.unshift(first);
}
return [...items];
}