mirror of
https://github.com/appwrite/console.git
synced 2026-06-06 19:27:48 +00:00
38 lines
944 B
TypeScript
38 lines
944 B
TypeScript
import prettyBytes from 'pretty-bytes';
|
|
|
|
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
|
|
|
|
export function calculateSize(bytes: number, decimals = 1) {
|
|
if (bytes === 0) return '0 Bytes';
|
|
|
|
const k = 1024;
|
|
const dm = decimals < 0 ? 0 : decimals;
|
|
|
|
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
|
|
|
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i];
|
|
}
|
|
|
|
export function sizeToBytes(value: number, unit: string) {
|
|
const index = sizes.indexOf(unit);
|
|
return value * Math.pow(1024, index);
|
|
}
|
|
export function bytesToSize(value: number, unit: string) {
|
|
const index = sizes.indexOf(unit);
|
|
return value / Math.pow(1024, index);
|
|
}
|
|
|
|
export function humanFileSize(bytes: number): {
|
|
value: string;
|
|
unit: string;
|
|
} {
|
|
const value = prettyBytes(bytes, {
|
|
locale: 'en'
|
|
}).split(' ');
|
|
|
|
return {
|
|
value: value[0],
|
|
unit: value[1]
|
|
};
|
|
}
|