Added format bytes helper
This commit is contained in:
parent
adcd6eaf79
commit
a0f0699a85
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@ztimson/utils",
|
||||
"version": "0.15.5",
|
||||
"version": "0.15.6",
|
||||
"description": "Utility library",
|
||||
"author": "Zak Timson",
|
||||
"license": "MIT",
|
||||
|
@ -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
|
||||
*
|
||||
|
Loading…
Reference in New Issue
Block a user