Skip to content

Commit

Permalink
Add 'human', humanize bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
myhro committed Oct 10, 2024
1 parent ec05246 commit 9044305
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions human/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Humanize Bytes</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 50px;
text-align: center;
}
#output {
margin-top: 20px;
font-size: 1.5em;
color: #333;
}
#paste-button {
padding: 10px;
border: 2px solid #aaa;
width: 300px;
margin: 20px auto;
cursor: pointer;
background-color: #f0f0f0;
font-size: 1em;
}
</style>
</head>
<body>
<h1>Human-Readable Bytes Conversion</h1>
<button id="paste-button">Read number from clipboard</button>
<div id="output"></div>

<script>
function bytesToHumanReadable(bytes) {
const sizes = ["Bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
if (bytes === 0) return "0 Bytes";
const i = Math.floor(Math.log(bytes) / Math.log(1024));
return parseFloat((bytes / Math.pow(1024, i)).toFixed(2)) + " " + sizes[i];
}

function updateOutput(parsedValue) {
const outputElement = document.getElementById("output");
if (!isNaN(parsedValue)) {
outputElement.textContent = bytesToHumanReadable(parsedValue);
} else {
outputElement.textContent = "Please paste a valid integer.";
}
}

const pasteButton = document.getElementById("paste-button");
pasteButton.addEventListener("click", async () => {
try {
const pastedData = await navigator.clipboard.readText();
const parsedValue = parseInt(pastedData, 10);
updateOutput(parsedValue);
} catch (err) {
document.getElementById("output").textContent = "Failed to read clipboard content.";
}
});

document.addEventListener("paste", (event) => {
event.preventDefault();
const pastedData = event.clipboardData.getData("text");
const parsedValue = parseInt(pastedData, 10);
updateOutput(parsedValue);
});
</script>
</body>
</html>

0 comments on commit 9044305

Please sign in to comment.