refactor: dedupe cli lowercase helpers

This commit is contained in:
Peter Steinberger
2026-04-07 17:35:58 +01:00
parent cebfa70277
commit 1a3f141215
28 changed files with 96 additions and 63 deletions

View File

@@ -1,3 +1,5 @@
import { normalizeLowercaseStringOrEmpty } from "../shared/string-coerce.js";
export type BytesParseOptions = {
defaultUnit?: "b" | "kb" | "mb" | "gb" | "tb";
};
@@ -15,9 +17,7 @@ const UNIT_MULTIPLIERS: Record<string, number> = {
};
export function parseByteSize(raw: string, opts?: BytesParseOptions): number {
const trimmed = String(raw ?? "")
.trim()
.toLowerCase();
const trimmed = normalizeLowercaseStringOrEmpty(String(raw ?? "").trim());
if (!trimmed) {
throw new Error("invalid byte size (empty)");
}
@@ -32,7 +32,7 @@ export function parseByteSize(raw: string, opts?: BytesParseOptions): number {
throw new Error(`invalid byte size: ${raw}`);
}
const unit = (m[2] ?? opts?.defaultUnit ?? "b").toLowerCase();
const unit = normalizeLowercaseStringOrEmpty(m[2] ?? opts?.defaultUnit ?? "b");
const multiplier = UNIT_MULTIPLIERS[unit];
if (!multiplier) {
throw new Error(`invalid byte size unit: ${raw}`);