hydra/src/shared/index.ts

21 lines
491 B
TypeScript
Raw Normal View History

export enum Downloader {
RealDebrid,
Torrent,
}
2024-05-12 15:29:12 +03:00
const FORMAT = ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
export const formatBytes = (bytes: number): string => {
if (!Number.isFinite(bytes) || isNaN(bytes) || bytes <= 0) {
return `0 ${FORMAT[0]}`;
}
const byteKBase = 1024;
const base = Math.floor(Math.log(bytes) / Math.log(byteKBase));
const formatedByte = bytes / byteKBase ** base;
return `${Math.trunc(formatedByte * 10) / 10} ${FORMAT[base]}`;
};