Added format bytes helper
All checks were successful
Build / Build NPM Project (push) Successful in 24s
Build / Tag Version (push) Successful in 7s

This commit is contained in:
Zakary Timson 2024-09-11 18:33:10 -04:00
parent adcd6eaf79
commit a0f0699a85
2 changed files with 9 additions and 1 deletions

View File

@ -1,6 +1,6 @@
{
"name": "@ztimson/utils",
"version": "0.15.5",
"version": "0.15.6",
"description": "Utility library",
"author": "Zak Timson",
"license": "MIT",

View File

@ -6,6 +6,14 @@ export function createHex(length: number) {
return Array(length).fill(null).map(() => Math.round(Math.random() * 0xF).toString(16)).join('');
}
export function formatBytes(bytes: number, decimals = 2) {
if (bytes === 0) return '0 Bytes';
const k = 1024;
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(decimals)) + ' ' + sizes[i];
}
/**
* String of all letters
*