misc_pterodactyl-panel/resources/scripts/helpers.ts

23 lines
846 B
TypeScript
Raw Normal View History

export const bytesToMegabytes = (bytes: number) => Math.floor(bytes / 1024 / 1024);
export const megabytesToBytes = (mb: number) => Math.floor(mb * 1024 * 1024);
2019-07-28 03:23:51 +00:00
export function bytesToHuman (bytes: number): string {
if (bytes === 0) {
return '0 kB';
}
const i = Math.floor(Math.log(bytes) / Math.log(1024));
return `${Number((bytes / Math.pow(1024, i)).toFixed(2))} ${[ 'Bytes', 'kB', 'MB', 'GB', 'TB' ][i]}`;
2019-07-28 03:23:51 +00:00
}
export function megabytesToHuman (mb: number): string {
return bytesToHuman(megabytesToBytes(mb));
}
export const randomInt = (low: number, high: number) => Math.floor(Math.random() * (high - low) + low);
export const cleanDirectoryPath = (path: string) => path.replace(/(^#\/*)|(\/(\/*))|(^$)/g, '/');
export const capitalize = (s: string) => s.charAt(0).toUpperCase() + s.slice(1).toLowerCase();