Files
rfid-database/src/lib/rfid/uid.ts
T
Cursor Agent 3331944773 Implement RFID tag dump PWA with REST API, auth, and CI
Add Next.js app with SQLite/Drizzle storage for sites and LF/HF tag
dumps, multi-user Auth.js (credentials + optional OIDC), personal API
tokens in Settings, versioned /api/v1 for UI and future CLI use,
MCT/Proxmark/JSON import-export, PWA offline shell, Vitest tests, and
GitHub Actions CI.
2026-08-23 22:17:51 +00:00

28 lines
792 B
TypeScript

/**
* 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;
}
}