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