/** * One way of reaching somebody, normalised: cotton-rules' `handles` * (crates/cotton-rules/src/handles.rs: the person-handle kinds, the social * rule and a message contact's handle), asked through the rules module. */import { rule } from"../rules/index.ts";importtype { Region } from"./phone.ts";exportconst HANDLE_KINDS = ["phone", "email", "instagram", "facebook", "linkedin"] asconst;exporttypeHandleKind = (typeof HANDLE_KINDS)[number];exportinterfaceHandle { kind: HandleKind; value: string;}exportfunction isHandleKind(value: unknown): value is HandleKind {returntypeof value === "string" && (HANDLE_KINDS asreadonly string[]).includes(value);}/** A site handle as its name alone: no scheme, host, `@`, query or path. */exportfunction socialHandle(kind: HandleKind, raw: string): string | null {return rule<string | null>("handles.social", { kind, raw });}/** A raw handle in the form the contact handle column holds, or null; a code-less phone is read in `region`. */exportfunction normalizeHandle(kind: HandleKind, raw: string, region: Region | null = null): Handle | null {return rule<Handle | null>("handles.normalize", region === null ? { kind, raw } : { kind, raw, region });}/** `kind:value`, so a number and an address never collide. */exportconst handleKey = (h: Handle): string => `${h.kind}:${h.value}`;exportconst sameHandle = (a: Handle, b: Handle): boolean => a.kind === b.kind && a.value === b.value;/** Every handle a `{ phone?, email? }` names, phone first. */exportfunction handlesFrom(source: { phone?: string | null; email?: string | null }, region: Region | null): Handle[] {return rule<Handle[]>("handles.from", { phone: source.phone ?? null, email: source.email ?? null, region });}