95 lines
2.2 KiB
JavaScript
95 lines
2.2 KiB
JavaScript
/**
|
|
* Function to randomise an array order
|
|
* @param {array} array
|
|
*/
|
|
export function shuffleArray(array) {
|
|
var currentIndex = array.length,
|
|
temporaryValue,
|
|
randomIndex;
|
|
// While there remain elements to shuffle...
|
|
while (0 !== currentIndex) {
|
|
// Pick a remaining element...
|
|
randomIndex = Math.floor(Math.random() * currentIndex);
|
|
currentIndex -= 1;
|
|
// And swap it with the current element.
|
|
temporaryValue = array[currentIndex];
|
|
array[currentIndex] = array[randomIndex];
|
|
array[randomIndex] = temporaryValue;
|
|
}
|
|
return array;
|
|
}
|
|
|
|
export function urltoFile(url, filename, mimeType) {
|
|
return fetch(url)
|
|
.then(function (res) {
|
|
return res.arrayBuffer();
|
|
})
|
|
.then(function (buf) {
|
|
return new File([buf], filename, { type: mimeType });
|
|
});
|
|
}
|
|
|
|
export function formatBytes(bytes, decimals = 2) {
|
|
if (bytes === 0) return "0 Bytes";
|
|
|
|
const k = 1024;
|
|
const dm = decimals < 0 ? 0 : decimals;
|
|
const sizes = ["Bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
|
|
|
|
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
|
|
|
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + " " + sizes[i];
|
|
}
|
|
|
|
export function CountDownTimer(duration, granularity) {
|
|
this.duration = duration;
|
|
this.granularity = granularity || 1000;
|
|
this.tickFtns = [];
|
|
this.running = false;
|
|
}
|
|
|
|
CountDownTimer.prototype.start = function () {
|
|
if (this.running) {
|
|
return;
|
|
}
|
|
this.running = true;
|
|
var start = Date.now(),
|
|
that = this,
|
|
diff,
|
|
obj;
|
|
|
|
(function timer() {
|
|
diff = that.duration - (((Date.now() - start) / 1000) | 0);
|
|
|
|
if (diff > 0) {
|
|
setTimeout(timer, that.granularity);
|
|
} else {
|
|
diff = 0;
|
|
that.running = false;
|
|
}
|
|
|
|
obj = CountDownTimer.parse(diff);
|
|
that.tickFtns.forEach(function (ftn) {
|
|
ftn.call(this, obj.minutes, obj.seconds);
|
|
}, that);
|
|
})();
|
|
};
|
|
|
|
CountDownTimer.prototype.onTick = function (ftn) {
|
|
if (typeof ftn === 'function') {
|
|
this.tickFtns.push(ftn);
|
|
}
|
|
return this;
|
|
};
|
|
|
|
CountDownTimer.prototype.expired = function () {
|
|
return !this.running;
|
|
};
|
|
|
|
CountDownTimer.parse = function (seconds) {
|
|
return {
|
|
'minutes': (seconds / 60) | 0,
|
|
seconds: seconds % 60 | 0,
|
|
};
|
|
};
|