mirror of
https://github.com/amir20/dozzle.git
synced 2026-06-23 04:10:12 +00:00
a79ffdaf50
Co-authored-by: Dhaval Patel <dhavu262@gmail.com> Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
32 lines
859 B
TypeScript
32 lines
859 B
TypeScript
export function formatBytes(
|
|
bytes: number,
|
|
{ decimals = 2, short = false }: { decimals?: number; short?: boolean } = { decimals: 2, short: false },
|
|
) {
|
|
if (bytes === 0) return short ? "0B" : "0 Bytes";
|
|
const k = 1024;
|
|
const dm = decimals < 0 ? 0 : decimals;
|
|
const sizes = ["Bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
|
|
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
|
|
|
const value = parseFloat((bytes / Math.pow(k, i)).toFixed(dm));
|
|
if (short) {
|
|
return value + sizes[i].charAt(0);
|
|
} else {
|
|
return value + " " + sizes[i];
|
|
}
|
|
}
|
|
|
|
export function stripVersion(label: string) {
|
|
const [name, _] = label.split(":");
|
|
return name;
|
|
}
|
|
|
|
export function hashCode(str: string) {
|
|
let hash = 0;
|
|
for (let i = 0; i < str.length; i++) {
|
|
hash = (hash << 5) - hash + str.charCodeAt(i);
|
|
hash |= 0;
|
|
}
|
|
return hash;
|
|
}
|