/** * Normalize RFID UIDs to uppercase hex without separators. * Accepts common forms: "04:A1:B2:C3", "04 A1 B2 C3", "04a1b2c3". */ export function normalizeUid(input: string): string { const cleaned = input.replace(/[^0-9a-fA-F]/g, "").toUpperCase(); if (cleaned.length < 4 || cleaned.length % 2 !== 0) { throw new Error( `Invalid UID "${input}": expected even-length hex of at least 2 bytes` ); } return cleaned; } /** Display form with colon separators: 04:A1:B2:C3 */ export function formatUid(uid: string): string { const normalized = normalizeUid(uid); return normalized.match(/.{1,2}/g)?.join(":") ?? normalized; } export function tryNormalizeUid(input: string): string | null { try { return normalizeUid(input); } catch { return null; } }