move getRandomOrder to utils

This commit is contained in:
DIYgod
2018-03-02 16:12:55 +08:00
parent 532dddc3b0
commit c590dd4080
2 changed files with 20 additions and 30 deletions
+18
View File
@@ -65,6 +65,24 @@ const utils = {
dragStart: isMobile ? 'touchstart' : 'mousedown',
dragMove: isMobile ? 'touchmove' : 'mousemove',
dragEnd: isMobile ? 'touchend' : 'mouseup'
},
/**
* get random order, using Fisher–Yates shuffle
*/
randomOrder: (length = this.options.music.length) => {
function shuffle (arr) {
for (let i = arr.length - 1; i >= 0; i--) {
const randomIndex = Math.floor(Math.random() * (i + 1));
const itemAtIndex = arr[randomIndex];
arr[randomIndex] = arr[i];
arr[i] = itemAtIndex;
}
return arr;
}
return shuffle([...Array(length)].map(function (item, i) {
return i;
}));
}
};