/** * 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"; import type { Region } from "./phone.ts"; export const HANDLE_KINDS = ["phone", "email", "instagram", "facebook", "linkedin"] as const; export type HandleKind = (typeof HANDLE_KINDS)[number]; export interface Handle { kind: HandleKind; value: string; } export function isHandleKind(value: unknown): value is HandleKind { return typeof value === "string" && (HANDLE_KINDS as readonly string[]).includes(value); } /** A site handle as its name alone: no scheme, host, `@`, query or path. */ export function socialHandle(kind: HandleKind, raw: string): string | null { return rule("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`. */ export function normalizeHandle(kind: HandleKind, raw: string, region: Region | null = null): Handle | null { return rule("handles.normalize", region === null ? { kind, raw } : { kind, raw, region }); } /** `kind:value`, so a number and an address never collide. */ export const handleKey = (h: Handle): string => `${h.kind}:${h.value}`; export const sameHandle = (a: Handle, b: Handle): boolean => a.kind === b.kind && a.value === b.value; /** Every handle a `{ phone?, email? }` names, phone first. */ export function handlesFrom(source: { phone?: string | null; email?: string | null }, region: Region | null): Handle[] { return rule("handles.from", { phone: source.phone ?? null, email: source.email ?? null, region }); }