MCPcopy Create free account
hub / github.com/ChromeDevTools/chrome-devtools-mcp / parseByteSize

Function parseByteSize

src/utils/bytes.ts:31–71  ·  view source on GitHub ↗
(value: string)

Source from the content-addressed store, hash-verified

29 * Parses a byte size string (e.g. "1M", "1MB", "500KB", "1.5GB", "1024") into bytes.
30 */
31export function parseByteSize(value: string): number {
32 const trimmed = value.trim();
33 if (trimmed === '') {
34 throw new Error(`Invalid byte size: "${value}"`);
35 }
36
37 const match = trimmed.match(/^(\d+(?:\.\d+)?)\s*([a-zA-Z]+)?$/);
38 if (!match) {
39 throw new Error(
40 `Invalid byte size format: "${value}". Expected a number or format like "1024", "1M", "1MB", "1G", "1GB".`,
41 );
42 }
43
44 const numMatch = match[1];
45 if (numMatch === undefined) {
46 throw new Error(`Invalid byte size: "${value}"`);
47 }
48 const num = Number(numMatch);
49 if (!Number.isFinite(num) || num < 0) {
50 throw new Error(`Invalid byte size: "${value}"`);
51 }
52
53 const unitMatch = match[2];
54 if (unitMatch === undefined) {
55 return Math.round(num);
56 }
57
58 const unit = unitMatch.toLowerCase();
59 const multiplier = BYTE_UNITS[unit];
60 if (multiplier === undefined) {
61 throw new Error(
62 `Unknown unit "${unitMatch}" in "${value}". Supported units: B, KB, KiB, MB, MiB, GB, GiB, TB, TiB.`,
63 );
64 }
65
66 const bytes = Math.round(num * multiplier);
67 if (!Number.isFinite(bytes)) {
68 throw new Error(`Invalid byte size: "${value}"`);
69 }
70 return bytes;
71}
72
73export interface ByteSizeRange {
74 min: number;

Callers 2

parseByteSizeRangeFunction · 0.85
bytes.test.tsFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected