// SPDX-FileCopyrightText: Teffen Ellis and Mailwoman contributors // SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial /** * @file Splits a US register's street line into a PO box, or a house number, street and unit. */ import { matchPOBox } from "@mailwoman/codex/us/po-box" import { matchLeadingDesignator } from "@mailwoman/codex/us/unit/designator" import { splitStreetLine } from "#adapters/street/line" import type { StreetPlace, UnitSplitter } from "#adapters/street/unit" /** * A register's second address line as a unit, or `null` when it holds something else. * * Most filled second lines open with a USPS secondary unit designator * (`SUITE 200`, `STE. 4`, `APT 2B`, `DEPARTMENT OF SURGERY`) or a bare number sign (`#200`). * The rest name a recipient, a building or a mail stop (`ATTN: BILLING`, `C/O …`, `MEDICAL OFFICE BUILDING`). * * A caller leaves those out of the row rather than joining them to the street span, * because they belong to neither the street nor the unit. */ export function unitLine(line: string): string | null { const trimmed = line.trim() if (!trimmed) return null if (/^#\s*\w/u.test(trimmed)) return trimmed const [first = ""] = trimmed.split(/\s+/u) return matchLeadingDesignator(first.replace(/[.,]$/u, "")) ? trimmed : null } /** * The address components written on one US street line: a PO box, or a house number, street and unit. */ export type USStreetLineComponents = { po_box: string } | { house_number?: string; street: string; unit?: string } /** * A whole line that's a box designator and an identifier holding a digit * (`PO BOX 846`, `P.O. Box 10240`, `POB 1088`) as the `po_box` component, or `null` for any other line. * * The digit requirement keeps a street such as `BOX ELDER RD` a street. */ export function usPOBoxLine(line: string): { po_box: string } | null { const box = matchPOBox(line) return box && /\d/u.test(box.id) ? { po_box: line.trim() } : null } /** * Splits one US street line, as a register writes it, into its address components. * * A whole-line PO box ({@linkcode usPOBoxLine}) becomes `po_box`. * * Any other line takes the house number off its start with `splitStreetLine` * and a secondary unit off its end with the shared unit splitter (`#adapters/street/unit`), * which reads USPS Publication 28's designators from the US codex table. * * @returns `null` for a blank line. */ export function splitUSStreetLine( line: string, units: UnitSplitter, place: Omit = {} ): USStreetLineComponents | null { const box = usPOBoxLine(line) if (box) return box let split = splitStreetLine(line) if (!split) return null // A hyphenated single-letter suffix (`101 - B Street`) belongs to the street // when the remainder is only a bare street-type word (`Street`, `Ave`, `Road`) // rather than a complete street name (#3506). if (split.house_number) { const letterSuffix = /^(.+?)\s*-\s*([A-Za-z])$/u.exec(split.house_number) const streetWord = split.street.replace(/[.,]$/u, "").toLowerCase() if (letterSuffix && !/\s/u.test(split.street) && units.vocabulary("US")?.streetTypes.has(streetWord)) { split = { house_number: letterSuffix[1]!, street: `${letterSuffix[2]!} ${split.street}` } } } const trailing = units.split(split.street, { ...place, country: "US" }) return { ...(split.house_number ? { house_number: split.house_number } : {}), street: trailing?.street ?? split.street, ...(trailing ? { unit: trailing.unit } : {}), } }