Skip to content

Instantly share code, notes, and snippets.

View hctilg's full-sized avatar
🌱
Studying for the entrance exam (Konkour)

Mahi hctilg

🌱
Studying for the entrance exam (Konkour)
View GitHub Profile
@hctilg
hctilg / format-bytes.js
Last active June 15, 2024 21:10
convert bytes to a human-readable string
function formatBytes(bytes, decimals = 2) {
if (Number.isNaN(bytes)) return "Incalculable";
if (!+bytes) return "0 B";
let i = 0;
for (i; bytes >= 1024; i++) bytes /= 1024;
const dm = bytes % 1 === 0 ? 0 : decimals;
const units = ["B", "KB", "MB", "GB", "TB", "PB"];