18 lines
470 B
TypeScript
18 lines
470 B
TypeScript
const KILO = 1024;
|
|
const MEGA = KILO * KILO;
|
|
const GIGA = MEGA * KILO;
|
|
|
|
export default function shortenFileSize(size: number): string {
|
|
const giga = Math.floor(size / GIGA);
|
|
let remainder = size % GIGA;
|
|
const mega = Math.floor(remainder / MEGA);
|
|
remainder %= MEGA;
|
|
const kilo = Math.floor(remainder / KILO);
|
|
remainder %= KILO;
|
|
|
|
if (giga > 0) return `${giga} GB`;
|
|
if (mega > 0) return `${mega} MB`;
|
|
if (kilo > 0) return `${kilo} kB`;
|
|
return `${remainder} B`;
|
|
}
|