LeagueStats/client/src/helpers/functions.js

46 lines
1.4 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) {
return Math.round(elapsed / 1000) + ' seconds ago'
} else if (elapsed < msPerHour) {
return Math.round(elapsed / msPerMinute) + ' minutes ago'
} else if (elapsed < msPerDay) {
return Math.round(elapsed / msPerHour) + ' hours ago'
} else if (elapsed < msPerWeek) {
return Math.round(elapsed / msPerDay) + ' days ago'
} else {
const dateOptions = { day: '2-digit', month: '2-digit', year: 'numeric' }
return new Date(previous).toLocaleString(undefined, dateOptions).replace(/\//g, '.')
}
}
2020-08-28 10:09:28 +00:00
/**
* Convert seconds to a readable string
* @param {Number} seconds
*/
export function secToTime(seconds) {
const min = Math.floor(seconds / 60)
let newSec = Math.floor(seconds - min * 60)
newSec = newSec < 10 ? '0' + newSec : newSec
return `${min}:${newSec}`
}
/**
* Sort an array of players by role
*/
2020-08-28 10:09:28 +00:00
export function sortTeamByRole(a, b) {
const sortingArr = ['TOP', 'JUNGLE', 'MIDDLE', 'BOTTOM', 'SUPPORT']
return sortingArr.indexOf(a.role) - sortingArr.indexOf(b.role)
}