/** * Canonical phone normalisation: cotton-rules' `phone` * (crates/cotton-rules/src/phone.rs, the `phonenumber` crate), asked through * the rules module. The rows the Mac imported carry the ids its readings * made, and now the object reads a number with the very same crate and * metadata, so the two cannot differ. * * A number written with its country code (a leading `+` or `00`) is parsed and * kept as its E.164 digits, so the trunk 0 a person writes after the code goes. * A number without one stays its digits as typed, unless a region is given (the * owner's own number's country) and it is a valid number there. Either way the * result is 8–15 ASCII digits, no `+`, or null. */ import { rule } from "../rules/index.ts"; /** An ISO 3166 region code, as the rule names it ("IN", "GB"). */ export type Region = string; /** Presentation (whitespace, `-`, `(`, `)`, `.`) out. */ export function stripPresentation(raw: string): string { return rule("phone.strip", raw); } /** 8–15 ASCII digits, or null. */ export function normalizePhone(raw: string): string | null { return normalizePhoneIn(raw, null); } /** The region a canonical number is valid in, or null. */ export function regionOf(digits: string): Region | null { return rule("phone.region", digits); } /** `normalizePhone`, read in `region` when the number carries no country code. */ export function normalizePhoneIn(raw: string, region: Region | null): string | null { return rule("phone.normalize", region === null ? { raw } : { raw, region }); }