@mailwoman/codex 7.2.0 → 7.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (66) hide show
  1. package/address-system-conventions.ts +68 -0
  2. package/au/delivery-service.ts +179 -0
  3. package/au/index.ts +15 -0
  4. package/au/level-designator.ts +209 -0
  5. package/au/postcode.ts +51 -0
  6. package/au/state.ts +35 -0
  7. package/ca/index.ts +12 -0
  8. package/ca/postal-code.ts +121 -0
  9. package/ca/province.ts +99 -0
  10. package/ca/street-type.ts +167 -0
  11. package/country/codes.ts +534 -0
  12. package/country/country.ts +125 -0
  13. package/country/index.ts +15 -0
  14. package/country/names.ts +274 -0
  15. package/country/official-languages.ts +397 -0
  16. package/country/reference-data.ts +267 -0
  17. package/country/reference.ts +47 -0
  18. package/country/subdivision.ts +93 -0
  19. package/de/bundesland.ts +102 -0
  20. package/de/index.ts +12 -0
  21. package/de/postleitzahl.ts +91 -0
  22. package/de/street-type.ts +83 -0
  23. package/fr/cedex.ts +56 -0
  24. package/fr/code-postal.ts +105 -0
  25. package/fr/departement.ts +142 -0
  26. package/fr/index.ts +14 -0
  27. package/fr/region.ts +93 -0
  28. package/fr/voie.ts +98 -0
  29. package/gb/country.ts +74 -0
  30. package/gb/index.ts +14 -0
  31. package/gb/postcode-area.ts +107 -0
  32. package/gb/postcode.ts +109 -0
  33. package/gb/street-type.ts +90 -0
  34. package/index.ts +38 -0
  35. package/jp/address-unit.ts +87 -0
  36. package/jp/index.ts +13 -0
  37. package/jp/postal-code.ts +93 -0
  38. package/jp/prefecture.ts +173 -0
  39. package/level-semantics.ts +623 -0
  40. package/nz/delivery-service.ts +211 -0
  41. package/nz/index.ts +12 -0
  42. package/nz/postcode.ts +42 -0
  43. package/out/country/index.d.ts +1 -0
  44. package/out/country/index.d.ts.map +1 -1
  45. package/out/country/index.js +1 -0
  46. package/out/country/index.js.map +1 -1
  47. package/out/country/subdivision.d.ts +44 -0
  48. package/out/country/subdivision.d.ts.map +1 -0
  49. package/out/country/subdivision.js +76 -0
  50. package/out/country/subdivision.js.map +1 -0
  51. package/package.json +81 -37
  52. package/postcode-systems.ts +68 -0
  53. package/tools/build-country-surface-lexicon.ts +166 -0
  54. package/tools/export-country-surfaces.ts +46 -0
  55. package/tools/generate-country-reference.ts +153 -0
  56. package/tools/generate-official-languages.ts +188 -0
  57. package/tools/index.ts +12 -0
  58. package/us/floor-designator.ts +119 -0
  59. package/us/index.ts +19 -0
  60. package/us/military-address.ts +199 -0
  61. package/us/po-box.ts +82 -0
  62. package/us/state.ts +156 -0
  63. package/us/street-directional.ts +220 -0
  64. package/us/street-suffix.ts +345 -0
  65. package/us/unit-designator.ts +223 -0
  66. package/us/zipcode.ts +212 -0
@@ -0,0 +1,119 @@
1
+ /**
2
+ * @copyright Sister Software
3
+ * @license AGPL-3.0
4
+ * @author Teffen Ellis, et al.
5
+ *
6
+ * USPS Publication 28, Appendix C2 — Floor-class Secondary Unit Designators.
7
+ *
8
+ * The sibling of {@link ./unit-designator.ts}: where the unit table covers the full secondary-unit
9
+ * vocabulary (APT, STE, RM, …), this module extracts the floor-class subset — designators that
10
+ * name a FLOOR or LEVEL of the building rather than a specific addressable unit on that floor.
11
+ * USPS Pub 28 Appendix C2 identifies these designators as requiring a secondary number: "FL" (the
12
+ * approved abbreviation for FLOOR). The publication gives `FLOOR` as the canonical designator
13
+ * with approved abbreviation `FL` and variant `FLR`; `BASEMENT` (`BSMT`), `PENTHOUSE` (`PH`), and
14
+ * `LOBBY` (`LBBY`) are the standalone-or-numbered floor-adjacent types also listed in Appendix
15
+ * C2.
16
+ *
17
+ * Appendix C2 explicitly marks FLOOR, BASEMENT as requiring a secondary number (alongside APT,
18
+ * BLDG, etc.) while PENTHOUSE and LOBBY may stand alone. PH and LBBY are kept here (not just in
19
+ * {@link ./unit-designator.ts}) because the span proposer treats them as level-class hints —
20
+ * "LOBBY" and "PH" name a specific floor-analog, not a numbered unit, and the prior map routes
21
+ * `LEVEL_PHRASE` → `unit` (the schema carries no separate `level` tag).
22
+ *
23
+ * This table drives the `levelDesignators` set in the span-proposer lexicon. The full
24
+ * secondary-unit designators (APT, STE, RM, …) remain in {@link ./unit-designator.ts}.
25
+ *
26
+ * Data is verbatim USPS Pub 28 Appendix C2.
27
+ * @see {@link https://pe.usps.com/text/pub28/28apc_003.htm USPS Publication 28 — Appendix C2: Secondary Unit Designators}
28
+ */
29
+
30
+ /**
31
+ * One USPS Pub 28 C2 floor-class designator row.
32
+ *
33
+ * `requiresNumber` mirrors the Appendix C2 classification: FLOOR and BASEMENT must be followed by a secondary number;
34
+ * PENTHOUSE and LOBBY may stand alone.
35
+ */
36
+ export interface USFloorDesignator {
37
+ /** Full canonical designator (uppercase per the publication). */
38
+ name: string
39
+ /** Approved USPS abbreviation (what the post office prints on standardized mail). */
40
+ abbreviation: string
41
+ /** Additional recognized surface variants from Appendix C2. */
42
+ variants: readonly string[]
43
+ /**
44
+ * True when Appendix C2 marks this designator as "Requires a Secondary Number" (FLOOR, BASEMENT). False for
45
+ * standalone types (PENTHOUSE, LOBBY) that name a specific floor-analog without an identifier.
46
+ */
47
+ requiresNumber: boolean
48
+ }
49
+
50
+ /**
51
+ * USPS Pub 28 C2 floor-class secondary unit designators. Verbatim from the publication; see the module header for the
52
+ * per-row provenance. Ordered with the most-common numbered form first.
53
+ */
54
+ export const US_FLOOR_DESIGNATORS = [
55
+ { name: "FLOOR", abbreviation: "FL", variants: ["FLR"], requiresNumber: true },
56
+ { name: "BASEMENT", abbreviation: "BSMT", variants: [], requiresNumber: true },
57
+ { name: "PENTHOUSE", abbreviation: "PH", variants: [], requiresNumber: false },
58
+ { name: "LOBBY", abbreviation: "LBBY", variants: [], requiresNumber: false },
59
+ ] as const satisfies readonly USFloorDesignator[]
60
+
61
+ /** A canonical USPS floor-class designator name. */
62
+ export type USFloorDesignatorName = (typeof US_FLOOR_DESIGNATORS)[number]["name"]
63
+
64
+ /**
65
+ * Inverse lookup: every surface form (canonical name, approved abbreviation, or Appendix C2 variant) → its canonical
66
+ * designator name. Lowercase-keyed for case-insensitive matching: `"fl"` → `"FLOOR"`, `"bsmt"` → `"BASEMENT"`, `"ph"` →
67
+ * `"PENTHOUSE"`.
68
+ */
69
+ export const US_FLOOR_DESIGNATOR_LOOKUP: ReadonlyMap<string, USFloorDesignatorName> = (() => {
70
+ const out = new Map<string, USFloorDesignatorName>()
71
+
72
+ for (const row of US_FLOOR_DESIGNATORS) {
73
+ out.set(row.name.toLowerCase(), row.name)
74
+ out.set(row.abbreviation.toLowerCase(), row.name)
75
+
76
+ for (const v of row.variants) {
77
+ if (!out.has(v.toLowerCase())) {
78
+ out.set(v.toLowerCase(), row.name)
79
+ }
80
+ }
81
+ }
82
+
83
+ return out
84
+ })()
85
+
86
+ /**
87
+ * All lowercase surface tokens for the floor-class designators — the set the span proposer populates `levelDesignators`
88
+ * with when wiring the US codex slice. Includes canonical names, approved abbreviations, and Appendix C2 variants.
89
+ */
90
+ export const US_FLOOR_DESIGNATOR_TOKENS: ReadonlySet<string> = new Set(US_FLOOR_DESIGNATOR_LOOKUP.keys())
91
+
92
+ /** Approved USPS abbreviation per canonical floor designator name. */
93
+ export const US_FLOOR_DESIGNATOR_PREFERRED_ABBR: Readonly<Record<USFloorDesignatorName, string>> = Object.fromEntries(
94
+ US_FLOOR_DESIGNATORS.map((r) => [r.name, r.abbreviation])
95
+ ) as Readonly<Record<USFloorDesignatorName, string>>
96
+
97
+ /**
98
+ * Look up a USPS floor-class designator (by canonical name, abbreviation, or any Appendix C2 variant) and return the
99
+ * canonical name + approved abbreviation. Returns null if the token isn't a recognized floor-class designator.
100
+ */
101
+ export function lookupFloorDesignator(input: string | null | undefined): {
102
+ designator: USFloorDesignatorName
103
+ abbreviation: string
104
+ } | null {
105
+ if (!input || typeof input !== "string") return null
106
+ const designator = US_FLOOR_DESIGNATOR_LOOKUP.get(input.trim().toLowerCase())
107
+
108
+ if (!designator) return null
109
+
110
+ return { designator, abbreviation: US_FLOOR_DESIGNATOR_PREFERRED_ABBR[designator] }
111
+ }
112
+
113
+ /**
114
+ * True when a token is a recognized USPS floor-class secondary unit designator (case-insensitive) — `"Floor"`, `"FL"`,
115
+ * `"flr"`, `"bsmt"`, `"ph"`, `"lbby"`.
116
+ */
117
+ export function isFloorDesignatorToken(input: unknown): boolean {
118
+ return typeof input === "string" && US_FLOOR_DESIGNATOR_LOOKUP.has(input.trim().toLowerCase())
119
+ }
package/us/index.ts ADDED
@@ -0,0 +1,19 @@
1
+ /**
2
+ * @copyright Sister Software
3
+ * @license AGPL-3.0
4
+ * @author Teffen Ellis, et al.
5
+ *
6
+ * The United States address system (USPS): street suffixes, secondary unit designators, floor-class
7
+ * designators (USPS Pub 28 C2 floor/level subset), military/diplomatic post office designators
8
+ * (USPS Pub 28 Chapter 7: APO/FPO/DPO + PSC/CMR/UNIT), ZIP codes, and the state abbreviations
9
+ * they hang off of.
10
+ */
11
+
12
+ export * from "./floor-designator.ts"
13
+ export * from "./military-address.ts"
14
+ export * from "./po-box.ts"
15
+ export * from "./state.ts"
16
+ export * from "./street-directional.ts"
17
+ export * from "./street-suffix.ts"
18
+ export * from "./unit-designator.ts"
19
+ export * from "./zipcode.ts"
@@ -0,0 +1,199 @@
1
+ /**
2
+ * @copyright Sister Software
3
+ * @license AGPL-3.0
4
+ * @author Teffen Ellis, et al.
5
+ *
6
+ * USPS Publication 28 — Military / Diplomatic Post Office designators.
7
+ *
8
+ * Military and diplomatic overseas addresses use a distinct addressing scheme: instead of a city
9
+ * name, a standardized location-class code (APO, FPO, DPO) appears on the city line, followed by
10
+ * the armed-forces "state" code (AA, AE, AP) and a 09xxx ZIP code. The unit/PSC/CMR line
11
+ * appearing above the city line identifies the specific installation unit, postal service center,
12
+ * or community mail room.
13
+ *
14
+ * Sourcing (accessed 2026-06-12):
15
+ *
16
+ * - **USPS Publication 28, Chapter 7** ("Armed Forces and Diplomatic Post Offices") defines the three
17
+ * armed-forces location codes and three state-code regions, and states: "Use APO with AA
18
+ * (Americas), AE (Europe/Middle East/Africa/Canada), or AP (Pacific)"; "FPO (Fleet Post
19
+ * Office) for Navy ships and shore installations"; "DPO (Diplomatic Post Office) for US
20
+ * embassies and missions." Full URL: https://pe.usps.com/text/pub28/28c7_001.htm
21
+ * - **USPS Publication 28, Appendix B** gives the complete list of accepted unit-line formats: `UNIT
22
+ * <id>`, `PSC <id> BOX <box>`, `CMR <id> BOX <box>`, and `UNIT <id> BOX <box>`. The same
23
+ * appendix notes the two-digit unit ranges for PSC/CMR/UNIT assignment by theater. Full URL:
24
+ * https://pe.usps.com/text/pub28/28apb_001.htm
25
+ * - The **Armed Forces "state" codes** (AA, AE, AP) are defined in the same USPS appendix and are
26
+ * also the official USPS abbreviations for the three Armed Forces addressing regions. See:
27
+ * https://pe.usps.com/text/pub28/28apb_002.htm
28
+ * - "DPO" for Diplomatic Post Offices was added as a distinct code in 2011 (USPS Customer/ Industry
29
+ * Notice 61). It does NOT replace APO in diplomatic mail — both exist, with DPO used
30
+ * specifically for State Department overseas posts and APO/FPO retained for DoD.
31
+ *
32
+ * @see {@link https://pe.usps.com/text/pub28/28c7_001.htm USPS Pub 28 Chapter 7 — Military Addresses}
33
+ * @see {@link https://pe.usps.com/text/pub28/28apb_001.htm USPS Pub 28 Appendix B — Armed Forces Addresses}
34
+ */
35
+
36
+ /** USPS military / diplomatic post-office location codes (the "city" substitute on the city line). */
37
+ export const US_MILITARY_POST_OFFICE_CODES = [
38
+ /**
39
+ * Army Post Office — domestic USPS gateway for Army and Air Force overseas mail; also used for some diplomatic
40
+ * addresses (DPO is preferred for State Dept posts since 2011).
41
+ */
42
+ { code: "APO", name: "Army Post Office", armedForces: true },
43
+ /** Fleet Post Office — Navy ships and shore installations. */
44
+ { code: "FPO", name: "Fleet Post Office", armedForces: true },
45
+ /** Diplomatic Post Office — US embassies and missions (added 2011). */
46
+ { code: "DPO", name: "Diplomatic Post Office", armedForces: false },
47
+ ] as const
48
+
49
+ export type USMilitaryPostOfficeCode = (typeof US_MILITARY_POST_OFFICE_CODES)[number]["code"]
50
+
51
+ /**
52
+ * USPS Armed Forces "state" codes used in place of state names on military/diplomatic addresses. These appear where a
53
+ * US state abbreviation (NY, CA, …) would appear in a civilian address.
54
+ */
55
+ export const US_ARMED_FORCES_REGIONS = [
56
+ { code: "AA", name: "Armed Forces Americas", description: "Americas (excluding Canada)" },
57
+ { code: "AE", name: "Armed Forces Europe", description: "Europe, Middle East, Africa, and Canada" },
58
+ { code: "AP", name: "Armed Forces Pacific", description: "Pacific" },
59
+ ] as const
60
+
61
+ export type USArmedForcesRegionCode = (typeof US_ARMED_FORCES_REGIONS)[number]["code"]
62
+
63
+ /**
64
+ * USPS Pub 28 Appendix B unit-line designators for military/diplomatic overseas addresses. Each designator introduces
65
+ * an installation identifier and optionally a box number.
66
+ *
67
+ * Format rules per Appendix B:
68
+ *
69
+ * - `PSC <id> BOX <box>` — Postal Service Center
70
+ * - `CMR <id> BOX <box>` — Community Mail Room
71
+ * - `UNIT <id> BOX <box>` — numbered unit (battalion/company); UNIT may stand alone with just an id and no BOX when the
72
+ * unit has direct mail delivery
73
+ *
74
+ * BOX is required for PSC and CMR; UNIT may omit BOX.
75
+ */
76
+ export const US_MILITARY_UNIT_DESIGNATORS = [
77
+ {
78
+ code: "PSC",
79
+ name: "Postal Service Center",
80
+ requiresBox: true,
81
+ description: "Installation-level postal service center; format: PSC <id> BOX <box>",
82
+ },
83
+ {
84
+ code: "CMR",
85
+ name: "Community Mail Room",
86
+ requiresBox: true,
87
+ description: "Sub-installation mail room; format: CMR <id> BOX <box>",
88
+ },
89
+ {
90
+ code: "UNIT",
91
+ name: "Unit",
92
+ requiresBox: false,
93
+ description: "Numbered military unit (battalion/company); format: UNIT <id> [BOX <box>]",
94
+ },
95
+ ] as const
96
+
97
+ export type USMilitaryUnitDesignatorCode = (typeof US_MILITARY_UNIT_DESIGNATORS)[number]["code"]
98
+
99
+ /** Result of a military address line parse (the unit line: PSC/CMR/UNIT). */
100
+ export interface USMilitaryUnitMatch {
101
+ /** The designator as it appeared ("PSC", "CMR", "Unit"). */
102
+ matched: string
103
+ /** The canonical designator code ("PSC", "CMR", "UNIT"). */
104
+ code: USMilitaryUnitDesignatorCode
105
+ /** The installation identifier ("1520", "453"). */
106
+ id: string
107
+ /** The box number when present ("4620", "1234A"). */
108
+ box?: string
109
+ }
110
+
111
+ // Unit-line regex: PSC/CMR/UNIT <id> [BOX <box>]
112
+ // Identifiers are numeric; box numbers are alphanumeric. UNIT may stand without BOX.
113
+ const UNIT_LINE_RE = /^\s*(psc|cmr|unit)\s+(\d+)(?:\s+box\s+([\dA-Za-z]+))?\s*$/i
114
+
115
+ /**
116
+ * If `input` is a USPS military unit-line ("PSC 1520 BOX 4620", "CMR 453 BOX 100", "UNIT 7 BOX 234A", "UNIT 7"), return
117
+ * the canonical designator, installation id, and optional box. Null otherwise. Throws on a PSC or CMR line without a
118
+ * BOX component (per Appendix B, BOX is required for PSC/CMR; a bare "PSC 1520" is malformed).
119
+ */
120
+ export function matchMilitaryUnitLine(input: unknown): USMilitaryUnitMatch | null {
121
+ if (typeof input !== "string") return null
122
+ const m = UNIT_LINE_RE.exec(input)
123
+
124
+ if (!m) return null
125
+ const code = m[1]!.toUpperCase() as USMilitaryUnitDesignatorCode
126
+ const id = m[2]!
127
+ const box = m[3]
128
+
129
+ const row = US_MILITARY_UNIT_DESIGNATORS.find((r) => r.code === code)!
130
+
131
+ if (row.requiresBox && !box) {
132
+ throw new Error(
133
+ `[codex/us/military-address] ${code} line requires a BOX component per USPS Pub 28 Appendix B; got bare "${input.trim()}"`
134
+ )
135
+ }
136
+
137
+ return { matched: m[1]!, code, id, ...(box ? { box } : {}) }
138
+ }
139
+
140
+ /** Type-predicate: does the input look like a USPS military unit line (PSC/CMR/UNIT)? */
141
+ export function isMilitaryUnitLine(input: unknown): boolean {
142
+ try {
143
+ return matchMilitaryUnitLine(input) !== null
144
+ } catch {
145
+ return false // PSC/CMR without BOX is structurally malformed (not a false negative)
146
+ }
147
+ }
148
+
149
+ /** Result of a military city-line parse (APO/FPO/DPO + region code + ZIP). */
150
+ export interface USMilitaryCityMatch {
151
+ /** The post-office code as it appeared ("APO", "FPO", "DPO"). */
152
+ matched: string
153
+ /** The canonical post-office code. */
154
+ code: USMilitaryPostOfficeCode
155
+ /** The Armed Forces region code ("AA", "AE", "AP"). */
156
+ region: USArmedForcesRegionCode
157
+ /**
158
+ * The 5-digit or 9-digit ZIP code. Typical ranges per Pub 28: 09xxx (AE), 34xxx (AA), 96xxx (AP) — range validation
159
+ * per region is caller responsibility.
160
+ */
161
+ zip: string
162
+ }
163
+
164
+ // City-line regex: APO/FPO/DPO <region> <zip>
165
+ // USPS military ZIP assignment per Pub 28 and the Armed Forces zip code list:
166
+ // - AA (Americas): 340xx range
167
+ // - AE (Europe/ME/Africa/Canada): 09xxx range
168
+ // - AP (Pacific): 962xx-966xx range
169
+ // The regex accepts any 5-digit or 9-digit ZIP code in combination with a valid region code —
170
+ // validating the specific numeric range for each region is left to the caller (region+ZIP
171
+ // co-validation is operational policy, not structural syntax).
172
+ const CITY_LINE_RE = /^\s*(apo|fpo|dpo)\s+(aa|ae|ap)\s+(\d{5}(?:-\d{4})?)\s*$/i
173
+
174
+ /**
175
+ * If `input` is a USPS military city line ("APO AE 09165", "FPO AP 96602-1254", "DPO AE 09498", "APO AA 34022", "APO AP
176
+ * 96525"), return the canonical code, region, and ZIP. Null otherwise.
177
+ *
178
+ * ZIP ranges per Pub 28: AE (Europe/ME/Africa/Canada) → 09xxx; AP (Pacific) → 96xxx; AA (Americas) → 34xxx. Range
179
+ * validation per region is left to the caller; the matcher accepts any 5. or 9-digit ZIP paired with a valid region
180
+ * code.
181
+ */
182
+ export function matchMilitaryCityLine(input: unknown): USMilitaryCityMatch | null {
183
+ if (typeof input !== "string") return null
184
+ const m = CITY_LINE_RE.exec(input)
185
+
186
+ if (!m) return null
187
+
188
+ return {
189
+ matched: m[1]!.toUpperCase(),
190
+ code: m[1]!.toUpperCase() as USMilitaryPostOfficeCode,
191
+ region: m[2]!.toUpperCase() as USArmedForcesRegionCode,
192
+ zip: m[3]!,
193
+ }
194
+ }
195
+
196
+ /** Type-predicate: does the input look like a USPS military city line (APO/FPO/DPO + region + ZIP)? */
197
+ export function isMilitaryCityLine(input: unknown): boolean {
198
+ return matchMilitaryCityLine(input) !== null
199
+ }
package/us/po-box.ts ADDED
@@ -0,0 +1,82 @@
1
+ /**
2
+ * @copyright Sister Software
3
+ * @license AGPL-3.0
4
+ * @author Teffen Ellis, et al.
5
+ *
6
+ * USPS PO Box recognition + normalization to the surface forms that actually occur
7
+ * (case-insensitive, punctuated "P.O. Box", spelled-out "Post Office Box", bare "Box"). A PO box
8
+ * isn't a closed vocabulary like street suffixes — it's a designator phrase + a box id — so the
9
+ * API is a detector ({@link isPOBox}), a normalizer ({@link normalizePOBox}), and an extractor
10
+ * ({@link matchPOBox}) the corpus po_box synth/parsing can reuse instead of re-deriving the
11
+ * regex.
12
+ * @see {@link https://pe.usps.com/text/pub28/28c2_012.htm USPS Pub 28 §29 (PO Box / Caller service)}
13
+ */
14
+
15
+ /**
16
+ * USPS designator phrases that introduce a post-office-box identifier, longest-first so the matcher prefers the most
17
+ * specific phrase ("Post Office Box" before "Box"). Each is matched case-insensitively with flexible internal
18
+ * punctuation/spacing.
19
+ */
20
+ export const US_PO_BOX_DESIGNATORS = [
21
+ "POST OFFICE BOX",
22
+ "PO BOX",
23
+ "P O BOX",
24
+ "FIRM CALLER",
25
+ "CALLER",
26
+ "DRAWER",
27
+ "LOCKBOX",
28
+ "BOX",
29
+ ] as const satisfies readonly string[]
30
+
31
+ export type USPoBoxDesignator = (typeof US_PO_BOX_DESIGNATORS)[number]
32
+
33
+ // Matches a leading PO-box designator + its identifier. Allows "P.O. Box", "PO BOX", "Post Office
34
+ // Box", "Box 12-A", etc. The id is alphanumeric with optional dashes (USPS caller/firm ids exist).
35
+ const PO_BOX_RE =
36
+ /^\s*(?:(p\.?\s*o\.?\s*box)|(post\s+office\s+box)|(firm\s+caller)|(caller)|(drawer)|(lockbox)|(box))\s*#?\s*([\dA-Za-z][\dA-Za-z-]*)\s*$/i
37
+
38
+ /**
39
+ * Type-predicate: does the input look like a standalone PO Box address? Case-insensitive and tolerant of "P.O. Box",
40
+ * "Post Office Box", "Box 12", "PMB"-style ids. (Widens the original isp-nexus `/^PO BOX [\d-]+$/`, which only matched
41
+ * all-caps "PO BOX 123".)
42
+ */
43
+ export function isPOBox(input: unknown): boolean {
44
+ return typeof input === "string" && PO_BOX_RE.test(input)
45
+ }
46
+
47
+ /** Result of a PO-box parse: the matched designator phrase and the box identifier. */
48
+ export interface PoBoxMatch {
49
+ /** The designator phrase as it appeared, e.g. "P.O. Box", "Post Office Box". */
50
+ matched: string
51
+ /** The box identifier, e.g. "123", "12-A". */
52
+ id: string
53
+ }
54
+
55
+ /**
56
+ * If `input` is a PO-box phrase ("PO Box 123", "P.O. Box 12-A", "Post Office Box 7"), return the designator phrase and
57
+ * the id. Null otherwise. Useful for the corpus po_box shard (split the designator from the number) and for
58
+ * resolver/parsing reuse.
59
+ */
60
+ export function matchPOBox(input: unknown): PoBoxMatch | null {
61
+ if (typeof input !== "string") return null
62
+ const m = PO_BOX_RE.exec(input)
63
+
64
+ if (!m) return null
65
+ const matched = (m[1] ?? m[2] ?? m[3] ?? m[4] ?? m[5] ?? m[6] ?? m[7] ?? "").trim()
66
+ const id = m[8]!
67
+
68
+ return { matched, id }
69
+ }
70
+
71
+ /**
72
+ * Normalize any recognized PO-box phrase to the canonical USPS "PO BOX <id>" form. Returns the input unchanged if it
73
+ * isn't a PO box. (Widens the original isp-nexus normalizer, which only collapsed the "P.O. BOX" spelling and left the
74
+ * id/casing alone.)
75
+ */
76
+ export function normalizePOBox(input: string): string {
77
+ const m = matchPOBox(input)
78
+
79
+ if (!m) return input
80
+
81
+ return `PO BOX ${m.id.toUpperCase()}`
82
+ }
package/us/state.ts ADDED
@@ -0,0 +1,156 @@
1
+ /**
2
+ * @copyright Sister Software
3
+ * @license AGPL-3.0
4
+ * @author Teffen Ellis, et al.
5
+ *
6
+ * US two-letter postal abbreviations for the 50 states, DC, and the five primary territories.
7
+ *
8
+ * This is the minimal state primitive `zipcode.ts` needs (a closed abbreviation set + predicate)
9
+ * without pulling in `@mailwoman/tiger`'s FIPS enums and their `shapefile-parser` dependency —
10
+ * the point of the codex being a zero-runtime-dep package the browser-pure parser can consume.
11
+ * The full FIPS-keyed table still lives in `@mailwoman/tiger` (`tiger/state.ts`) and in the
12
+ * corpus TIGER adapter; folding those onto this set is a deliberate follow-up, not a v1 concern.
13
+ */
14
+
15
+ /**
16
+ * USPS two-letter abbreviations: the 50 states, the District of Columbia, and the five primary territories (Puerto
17
+ * Rico, Guam, US Virgin Islands, Northern Mariana Islands, American Samoa).
18
+ */
19
+ export const US_STATE_ABBREVIATIONS = [
20
+ "AL",
21
+ "AK",
22
+ "AZ",
23
+ "AR",
24
+ "CA",
25
+ "CO",
26
+ "CT",
27
+ "DE",
28
+ "DC",
29
+ "FL",
30
+ "GA",
31
+ "HI",
32
+ "ID",
33
+ "IL",
34
+ "IN",
35
+ "IA",
36
+ "KS",
37
+ "KY",
38
+ "LA",
39
+ "ME",
40
+ "MD",
41
+ "MA",
42
+ "MI",
43
+ "MN",
44
+ "MS",
45
+ "MO",
46
+ "MT",
47
+ "NE",
48
+ "NV",
49
+ "NH",
50
+ "NJ",
51
+ "NM",
52
+ "NY",
53
+ "NC",
54
+ "ND",
55
+ "OH",
56
+ "OK",
57
+ "OR",
58
+ "PA",
59
+ "RI",
60
+ "SC",
61
+ "SD",
62
+ "TN",
63
+ "TX",
64
+ "UT",
65
+ "VT",
66
+ "VA",
67
+ "WA",
68
+ "WV",
69
+ "WI",
70
+ "WY",
71
+ "PR",
72
+ "GU",
73
+ "VI",
74
+ "MP",
75
+ "AS",
76
+ ] as const satisfies readonly string[]
77
+
78
+ /** A USPS two-letter state-or-territory abbreviation. */
79
+ export type USStateAbbreviation = (typeof US_STATE_ABBREVIATIONS)[number]
80
+
81
+ const STATE_ABBREVIATION_SET: ReadonlySet<string> = new Set(US_STATE_ABBREVIATIONS)
82
+
83
+ /**
84
+ * Type-predicate for a USPS state-or-territory abbreviation. Case-insensitive (`"ca"` and `"CA"` both pass), since the
85
+ * abbreviation arrives from raw address text.
86
+ */
87
+ export function isUSStateAbbreviation(input: unknown): input is USStateAbbreviation {
88
+ return typeof input === "string" && STATE_ABBREVIATION_SET.has(input.toUpperCase())
89
+ }
90
+
91
+ /**
92
+ * Full names for the 50 states, DC, and the five primary territories, keyed by USPS abbreviation. The names half of the
93
+ * closed region vocabulary — where the country/state homographs live ("Georgia", "Washington"), which the
94
+ * gazetteer-anchor lexicon needs to flag candidate-tag collisions. Source: USPS Publication 28, Appendix B.
95
+ */
96
+ export const US_STATE_BY_ABBREVIATION = {
97
+ AL: "Alabama",
98
+ AK: "Alaska",
99
+ AZ: "Arizona",
100
+ AR: "Arkansas",
101
+ CA: "California",
102
+ CO: "Colorado",
103
+ CT: "Connecticut",
104
+ DE: "Delaware",
105
+ DC: "District of Columbia",
106
+ FL: "Florida",
107
+ GA: "Georgia",
108
+ HI: "Hawaii",
109
+ ID: "Idaho",
110
+ IL: "Illinois",
111
+ IN: "Indiana",
112
+ IA: "Iowa",
113
+ KS: "Kansas",
114
+ KY: "Kentucky",
115
+ LA: "Louisiana",
116
+ ME: "Maine",
117
+ MD: "Maryland",
118
+ MA: "Massachusetts",
119
+ MI: "Michigan",
120
+ MN: "Minnesota",
121
+ MS: "Mississippi",
122
+ MO: "Missouri",
123
+ MT: "Montana",
124
+ NE: "Nebraska",
125
+ NV: "Nevada",
126
+ NH: "New Hampshire",
127
+ NJ: "New Jersey",
128
+ NM: "New Mexico",
129
+ NY: "New York",
130
+ NC: "North Carolina",
131
+ ND: "North Dakota",
132
+ OH: "Ohio",
133
+ OK: "Oklahoma",
134
+ OR: "Oregon",
135
+ PA: "Pennsylvania",
136
+ RI: "Rhode Island",
137
+ SC: "South Carolina",
138
+ SD: "South Dakota",
139
+ TN: "Tennessee",
140
+ TX: "Texas",
141
+ UT: "Utah",
142
+ VT: "Vermont",
143
+ VA: "Virginia",
144
+ WA: "Washington",
145
+ WV: "West Virginia",
146
+ WI: "Wisconsin",
147
+ WY: "Wyoming",
148
+ PR: "Puerto Rico",
149
+ GU: "Guam",
150
+ VI: "US Virgin Islands",
151
+ MP: "Northern Mariana Islands",
152
+ AS: "American Samoa",
153
+ } as const satisfies Record<USStateAbbreviation, string>
154
+
155
+ /** The full state/territory names, derived from {@link US_STATE_BY_ABBREVIATION}. */
156
+ export const US_STATE_NAMES: readonly string[] = Object.values(US_STATE_BY_ABBREVIATION)