gitscope
← marzneshin / v0.1.0 / app/dashboard/src/utils/formatByte.tsx
https://dev.kronoz.win/r/marzneshin.git
app/dashboard/src/utils/formatByte.tsx · TypeScript
Blame Raw
 1export function formatBytes(bytes: number, decimals = 2, asArray = false) {
 2  if (!+bytes) return '0 B';
 3
 4  const k = 1024;
 5  const dm = decimals < 0 ? 0 : decimals;
 6  const sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
 7
 8  const i = Math.floor(Math.log(bytes) / Math.log(k));
 9  if (!asArray)
10    return `${parseFloat((bytes / Math.pow(k, i)).toFixed(dm))} ${sizes[i]}`;
11  else return [parseFloat((bytes / Math.pow(k, i)).toFixed(dm)), sizes[i]];
12}
13
14export const numberWithCommas = (x: number) => {
15  if (x !== null) return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');
16};