(bytes: number, decimals = 2)
| 40 | } |
| 41 | |
| 42 | export function formatBytes(bytes: number, decimals = 2) { |
| 43 | const positiveBytes = Math.max(bytes, 0); |
| 44 | |
| 45 | // early exit |
| 46 | if (positiveBytes === 0) { |
| 47 | return '0 B'; |
| 48 | } |
| 49 | |
| 50 | const k = 1024; |
| 51 | const dm = Math.max(decimals, 0); |
| 52 | const sizes = ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; |
| 53 | |
| 54 | const i = Math.floor(Math.log(positiveBytes) / Math.log(k)); |
| 55 | |
| 56 | return `${roundDecimals(positiveBytes / Math.pow(k, i), dm)} ${sizes[i]}`; |
| 57 | } |
| 58 | |
| 59 | export function pluralizeToken(token: string, times: number): string { |
| 60 | return `${times} ${Math.abs(times) === 1 ? token : pluralize(token)}`; |
no test coverage detected