opt-cc/api/tools/util.js

51 lines
1.4 KiB
JavaScript
Raw Permalink Normal View History

2017-06-08 13:14:53 +02:00
"use strict";
2017-05-10 11:04:06 +02:00
const sortCollectionBy = (collection, key) => {
collection.sort((a, b) => {
a = a[key].toLowerCase();
b = b[key].toLowerCase();
if (a < b) return -1;
if (a > b) return 1;
return 0;
});
return collection;
};
const playerArrayContains = (arr, item) => {
2017-10-20 23:42:41 +02:00
let i = 0, count = arr.length, matchFound = false;
2017-10-24 20:21:06 +02:00
for (; i < count; i++) {
if (arr[i].name === item.name && arr[i].fraction === item.fraction) {
2017-10-20 23:42:41 +02:00
matchFound = true;
break;
}
}
return matchFound;
};
2017-10-24 20:21:06 +02:00
const timeStringToDecimal = (timeString) => {
const timeArray = timeString.split(':');
const hour = parseInt(timeArray[0]);
const sek = parseInt(timeArray[1]) * 60 + parseInt(timeArray[2]);
2017-10-26 19:12:54 +02:00
return hour + (sek / 3600);
2017-10-24 20:21:06 +02:00
};
const decimalToTimeString = (decimal) => {
const hours = parseInt(decimal.toString().split(".")[0]);
const minutesFloat = ((decimal % 1) * 3600) / 60;
const minutes = parseInt(minutesFloat.toString().split(".")[0]);
const seconds = Math.round((minutesFloat - parseInt(minutes)) * 60);
return (hours < 10 ? '0' + hours : hours) + ':' +
(minutes < 10 ? '0' + minutes : minutes) + ':' +
(seconds < 10 ? '0' + seconds : seconds);
};
2017-05-10 11:04:06 +02:00
exports.sortCollection = sortCollectionBy;
exports.playerArrayContains = playerArrayContains;
2017-10-24 20:21:06 +02:00
exports.timeStringToDecimal = timeStringToDecimal;
exports.decimalToTimeString = decimalToTimeString;