LeagueStats/client/src/helpers/functions.js

60 lines
1.8 KiB
JavaScript
Raw Normal View History

/**
* Return the relative time betweeen a chosen moment and the current time
* @param previous : time we want to get difference
*/
export function timeDifference(previous) {
2019-04-05 20:41:32 +00:00
const current = new Date();
const msPerMinute = 60 * 1000;
const msPerHour = msPerMinute * 60;
const msPerDay = msPerHour * 24;
const msPerWeek = msPerDay * 7;
const elapsed = current - previous;
if (elapsed < msPerMinute) {
return Math.round(elapsed / 1000) + 's';
} else if (elapsed < msPerHour) {
return Math.round(elapsed / msPerMinute) + 'm';
} else if (elapsed < msPerDay) {
return Math.round(elapsed / msPerHour) + 'h';
} else if (elapsed < msPerWeek) {
return Math.round(elapsed / msPerDay) + 'j';
} else {
2019-04-05 20:41:32 +00:00
const oldDate = new Date(previous);
const day = oldDate.getDate() < 10 ? '0' + oldDate.getDate() : oldDate.getDate();
const month = oldDate.getMonth() < 9 ? '0' + (oldDate.getMonth() + 1) : (oldDate.getMonth() + 1);
return day + '.' + month + '.' + oldDate.getFullYear().toString().substr(-2);
}
}
/**
* Return time in a formatted way
* @param sec : time in seconds to convert
*/
export function secToTime(sec) {
2019-04-05 20:41:32 +00:00
const min = Math.floor(sec / 60);
const newSec = sec - min * 60;
return min + ':' + (newSec < 10 ? '0' + newSec : newSec); //
}
/**
* Return the link of the rank image
* @param soloQStats : stats in soloQ of the player
*/
export function getRankImg(soloQStats) {
if (!soloQStats) {
return 'https://cdn.valentinkaelin.ch/riot/tier-icons/provisional.png';
}
return 'https://cdn.valentinkaelin.ch/riot/tier-icons/Emblem_' + capitalize(soloQStats.tier.toLowerCase()) + '.png';
}
/**
* Capitalize first letter of params string
* @param string : string to capitalize
*/
function capitalize(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}