(value, unit)
| 1 | export const BYTE_UNITS = ['B', 'KB', 'MB', 'GB', 'TB']; |
| 2 | |
| 3 | export function toBytes(value, unit) { |
| 4 | const num = Number(value); |
| 5 | if (!Number.isFinite(num)) { |
| 6 | throw new Error('请输入合法的数字'); |
| 7 | } |
| 8 | const index = BYTE_UNITS.indexOf(unit); |
| 9 | if (index === -1) { |
| 10 | throw new Error('不支持的单位'); |
| 11 | } |
| 12 | return num * Math.pow(1024, index); |
| 13 | } |
| 14 | |
| 15 | export function convertBytes(value, fromUnit, toUnit) { |
| 16 | const bytes = toBytes(value, fromUnit); |