46 lines
1.6 KiB
TypeScript
46 lines
1.6 KiB
TypeScript
/**
|
|
* Helpers for admin user list free-text search (ID, email hash, name, referral).
|
|
*/
|
|
|
|
const UUID_RE =
|
|
/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
const UUID_HEX32_RE = /^[0-9a-f]{32}$/i;
|
|
const UUID_PREFIX_RE = /^[0-9a-f-]{8,36}$/i;
|
|
|
|
/** Normalize braces/case/hyphenless forms into canonical lowercase UUID, or null. */
|
|
export function tryParseUuid(term: string): string | null {
|
|
const cleaned = term.trim().replace(/^\{|\}$/g, '').toLowerCase();
|
|
if (UUID_RE.test(cleaned)) return cleaned;
|
|
if (UUID_HEX32_RE.test(cleaned)) {
|
|
return `${cleaned.slice(0, 8)}-${cleaned.slice(8, 12)}-${cleaned.slice(12, 16)}-${cleaned.slice(16, 20)}-${cleaned.slice(20)}`;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
export function looksLikeEmail(term: string): boolean {
|
|
return term.includes('@') && !/\s/.test(term);
|
|
}
|
|
|
|
/** Escape `%` / `_` / `\` for ILIKE / LIKE patterns. */
|
|
export function escapeLikePattern(term: string): string {
|
|
return term.replace(/\\/g, '\\\\').replace(/%/g, '\\%').replace(/_/g, '\\_');
|
|
}
|
|
|
|
export type AdminSearchKind = 'uuid' | 'email' | 'general';
|
|
|
|
export function classifyAdminUserSearch(term: string): AdminSearchKind {
|
|
const t = term.trim();
|
|
if (!t) return 'general';
|
|
if (tryParseUuid(t)) return 'uuid';
|
|
if (looksLikeEmail(t)) return 'email';
|
|
return 'general';
|
|
}
|
|
|
|
/** True when the term is a plausible UUID prefix (min 8 hex digits ignoring hyphens). */
|
|
export function isUuidPrefix(term: string): boolean {
|
|
const t = term.trim().toLowerCase();
|
|
if (!UUID_PREFIX_RE.test(t)) return false;
|
|
const hex = t.replace(/-/g, '');
|
|
return hex.length >= 8 && hex.length < 32 && /^[0-9a-f]+$/.test(hex);
|
|
}
|