transbeam/static/js/util.js

36 lines
860 B
JavaScript

const SIZE_UNITS = [
{ name: 'GB', size: Math.pow(10, 9) },
{ name: 'MB', size: Math.pow(10, 6) },
{ name: 'KB', size: Math.pow(10, 3) },
];
function displaySize(bytes) {
for (const unit of SIZE_UNITS) {
if (bytes >= unit.size) {
return `${(bytes / unit.size).toFixed(1)}${unit.name}`;
}
}
return `${bytes}B`;
}
const TIME_UNITS = [
{ name: 'd', size: 86400 },
{ name: 'h', size: 3600 },
{ name: 'm', size: 60 },
{ name: 's', size: 1 },
];
function displayTime(seconds) {
let result = "";
for (const unit of TIME_UNITS) {
if (seconds >= unit.size || result !== "") {
result += `${Math.floor(seconds / unit.size)}${unit.name}`;
seconds = seconds % unit.size;
}
}
if (result === "") {
result = "0s";
}
return result;
}