56 lines
1.5 KiB
JavaScript
56 lines
1.5 KiB
JavaScript
const isMobile = /mobile/i.test(window.navigator.userAgent);
|
||
|
||
const utils = {
|
||
/**
|
||
* Parse second to time string
|
||
*
|
||
* @param {Number} second
|
||
* @return {String} 00:00 or 00:00:00
|
||
*/
|
||
secondToTime: (second) => {
|
||
const add0 = (num) => (num < 10 ? '0' + num : '' + num);
|
||
const hour = Math.floor(second / 3600);
|
||
const min = Math.floor((second - hour * 3600) / 60);
|
||
const sec = Math.floor(second - hour * 3600 - min * 60);
|
||
return (hour > 0 ? [hour, min, sec] : [min, sec]).map(add0).join(':');
|
||
},
|
||
|
||
isMobile: isMobile,
|
||
|
||
storage: {
|
||
set: (key, value) => {
|
||
localStorage.setItem(key, value);
|
||
},
|
||
|
||
get: (key) => localStorage.getItem(key),
|
||
},
|
||
|
||
nameMap: {
|
||
dragStart: isMobile ? 'touchstart' : 'mousedown',
|
||
dragMove: isMobile ? 'touchmove' : 'mousemove',
|
||
dragEnd: isMobile ? 'touchend' : 'mouseup',
|
||
},
|
||
|
||
/**
|
||
* get random order, using Fisher–Yates shuffle
|
||
*/
|
||
randomOrder: (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;
|
||
})
|
||
);
|
||
},
|
||
};
|
||
|
||
export default utils;
|