analytics/assets/js/dashboard/number-formatter.js

42 lines
1.1 KiB
JavaScript
Raw Normal View History

const THOUSAND = 1000
const HUNDRED_THOUSAND = 100000
const MILLION = 1000000
const HUNDRED_MILLION = 100000000
export default function numberFormatter(num) {
if (num >= THOUSAND && num < MILLION) {
const thousands = num / THOUSAND
if (thousands === Math.floor(thousands) || num >= HUNDRED_THOUSAND) {
return Math.floor(thousands) + 'k'
} else {
return (Math.floor(thousands * 10) / 10) + 'k'
}
} else if (num >= MILLION && num < HUNDRED_MILLION) {
const millions = num / MILLION
if (millions === Math.floor(millions)) {
2020-11-23 12:08:16 +03:00
return Math.floor(millions) + 'M'
} else {
2020-11-23 12:08:16 +03:00
return (Math.floor(millions * 10) / 10) + 'M'
}
} else {
return num
}
}
function pad(num, size) {
return ('000' + num).slice(size * -1);
}
export function durationFormatter(duration) {
const hours = Math.floor(duration / 60 / 60)
const minutes = Math.floor(duration / 60) % 60
const seconds = Math.floor(duration - (minutes * 60) - (hours * 60 * 60))
if (hours > 0) {
return `${hours}h ${minutes}m ${seconds}s`
} else if (minutes > 0) {
return `${minutes}m ${pad(seconds, 2)}s`
} else {
return `${seconds}s`
}
}