tokenize-it / src /lib /format.ts
Orion-zhen's picture
:boom: use web stack
21ab33c unverified
Raw History Blame Contribute Delete
680 Bytes
export function formatBytes(bytes: number): string {
if (bytes < 1024) return `${bytes} B`;
const units = ["KB", "MB", "GB"];
let value = bytes / 1024;
let unit = units[0];
for (let index = 1; index < units.length && value >= 1024; index += 1) {
value /= 1024;
unit = units[index];
}
return `${value >= 10 ? value.toFixed(1) : value.toFixed(2)} ${unit}`;
}
export function formatDuration(milliseconds: number): string {
if (milliseconds < 1) return "< 1 ms";
if (milliseconds < 1000) return `${milliseconds.toFixed(1)} ms`;
return `${(milliseconds / 1000).toFixed(2)} s`;
}
export function shortSha(sha: string): string {
return sha.slice(0, 8);
}