(bytes: number, decimals = 2)
| 2 | import DOMPurify from 'dompurify'; |
| 3 | |
| 4 | export const formatBytes = (bytes: number, decimals = 2): string => { |
| 5 | if (bytes === 0) return '0 B'; |
| 6 | |
| 7 | const k = 1024; |
| 8 | const sizes = ['B', 'KB', 'MB', 'GB', 'TB']; |
| 9 | const i = Math.floor(Math.log(bytes) / Math.log(k)); |
| 10 | |
| 11 | return ( |
| 12 | parseFloat((bytes / Math.pow(k, i)).toFixed(decimals)) + ' ' + sizes[i] |
| 13 | ); |
| 14 | }; |
| 15 | |
| 16 | export const generateFilename = ( |
| 17 | originalFilename: string, |