(bytes: number)
| 3 | export const TRASH_PATH = `/.Trash/`; |
| 4 | |
| 5 | export function humanFileSize(bytes: number) { |
| 6 | if (Math.abs(bytes) < 1024) { |
| 7 | return `${bytes} B`; |
| 8 | } |
| 9 | |
| 10 | const units = ['KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; |
| 11 | |
| 12 | let unitIndex = -1; |
| 13 | const roundedPower = 10 ** 2; |
| 14 | |
| 15 | do { |
| 16 | bytes /= 1024; |
| 17 | ++unitIndex; |
| 18 | } while (Math.round(Math.abs(bytes) * roundedPower) / roundedPower >= 1024 && unitIndex < units.length - 1); |
| 19 | |
| 20 | if (bytes.toFixed(2).split('.')[1] === '00') { |
| 21 | return `${bytes.toFixed(0)} ${units[unitIndex]}`; |
| 22 | } |
| 23 | |
| 24 | return `${bytes.toFixed(2)} ${units[unitIndex]}`; |
| 25 | } |
| 26 | |
| 27 | export function bytesFromHumanFileSize(humanFileSize: string) { |
| 28 | const [numberString, unit] = humanFileSize.split(' '); |
no outgoing calls
no test coverage detected