(value: string)
| 25 | * @throws Error if the value is invalid |
| 26 | */ |
| 27 | export function parseSize(value: string): number { |
| 28 | // If the value is a plain number, treat it as bytes |
| 29 | if (/^\d+$/.test(value)) { |
| 30 | return parseInteger(value) |
| 31 | } |
| 32 | |
| 33 | // Match a number followed by a unit |
| 34 | const match = value.match(/^([\d.]+)\s*([A-Za-z]+)$/) |
| 35 | if (!match) { |
| 36 | throw new Error(`Invalid size format: ${value}`) |
| 37 | } |
| 38 | |
| 39 | const [_, numStr, unit] = match |
| 40 | const num = parseFloat(numStr) |
| 41 | |
| 42 | if (Number.isNaN(num)) { |
| 43 | throw new Error(`Invalid size value: ${numStr}`) |
| 44 | } |
| 45 | |
| 46 | const multiplier = sizeUnits[unit] |
| 47 | if (multiplier === undefined) { |
| 48 | throw new Error(`Unknown size unit: ${unit}`) |
| 49 | } |
| 50 | |
| 51 | return Math.floor(num * multiplier) |
| 52 | } |
no test coverage detected