|
1 | 1 | /** Round a value+unit string like "2.711MiB" → "2.7 MiB" */ |
2 | 2 | export const formatSizeValue = (raw: string): string => { |
3 | 3 | const match = raw.match(/^([\d.]+)\s*([A-Za-z]+)$/); |
4 | | - if (!match) return raw; |
| 4 | + if (!match?.[1] || !match[2]) return raw; |
5 | 5 | const num = Number.parseFloat(match[1]); |
6 | 6 | const unit = match[2]; |
7 | 7 | if (Number.isNaN(num)) return raw; |
8 | | - // Show 1 decimal for values >= 1, 2 decimals for tiny values |
9 | 8 | const rounded = num >= 1 ? num.toFixed(1) : num.toFixed(2); |
10 | 9 | return `${rounded} ${unit}`; |
11 | 10 | }; |
12 | 11 |
|
13 | 12 | /** Format "2.711MiB / 7.609GiB" → "2.7 MiB / 7.6 GiB" */ |
14 | 13 | export const formatMemUsage = (raw: string): string => { |
15 | | - const parts = raw.split("/").map((s) => s.trim()); |
16 | | - if (parts.length !== 2) return raw; |
17 | | - return `${formatSizeValue(parts[0])} / ${formatSizeValue(parts[1])}`; |
| 14 | + const [left, right] = raw.split("/").map((s) => s.trim()); |
| 15 | + if (!left || !right) return raw; |
| 16 | + return `${formatSizeValue(left)} / ${formatSizeValue(right)}`; |
18 | 17 | }; |
19 | 18 |
|
20 | 19 | /** Format "978B / 252B" → "978 B / 252 B" */ |
21 | 20 | export const formatIOValue = (raw: string): string => { |
22 | | - const parts = raw.split("/").map((s) => s.trim()); |
23 | | - if (parts.length !== 2) return raw; |
24 | | - return `${formatSizeValue(parts[0])} / ${formatSizeValue(parts[1])}`; |
| 21 | + const [left, right] = raw.split("/").map((s) => s.trim()); |
| 22 | + if (!left || !right) return raw; |
| 23 | + return `${formatSizeValue(left)} / ${formatSizeValue(right)}`; |
25 | 24 | }; |
26 | 25 |
|
27 | 26 | /** Format "0.00%" → "0.0%", "12.345%" → "12.3%" */ |
|
0 commit comments