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-08-23 14:48:16 +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) {
2019-08-23 14:48:16 +00:00
return Math.round(elapsed / 1000) + 's'
} else if (elapsed < msPerHour) {
2019-08-23 14:48:16 +00:00
return Math.round(elapsed / msPerMinute) + 'm'
} else if (elapsed < msPerDay) {
2019-08-23 14:48:16 +00:00
return Math.round(elapsed / msPerHour) + 'h'
} else if (elapsed < msPerWeek) {
2019-08-23 14:48:16 +00:00
return Math.round(elapsed / msPerDay) + 'j'
} else {
2019-08-23 14:48:16 +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-08-23 14:48:16 +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) {
2019-08-23 14:48:16 +00:00
return 'https://cdn.valentinkaelin.ch/riot/tier-icons/provisional.png'
}
2019-08-23 14:48:16 +00:00
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) {
2019-08-23 14:48:16 +00:00
return string.charAt(0).toUpperCase() + string.slice(1)
}