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

Function parseByteSize

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

Source from the content-addressed store, hash-verified

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

Callers 2

parseByteSizeRangeFunction · 0.85
bytes.test.tsFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected