@mailwoman/codex 7.2.0 → 7.2.1

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 (57) 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 +14 -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/de/bundesland.ts +102 -0
  19. package/de/index.ts +12 -0
  20. package/de/postleitzahl.ts +91 -0
  21. package/de/street-type.ts +83 -0
  22. package/fr/cedex.ts +56 -0
  23. package/fr/code-postal.ts +105 -0
  24. package/fr/departement.ts +142 -0
  25. package/fr/index.ts +14 -0
  26. package/fr/region.ts +93 -0
  27. package/fr/voie.ts +98 -0
  28. package/gb/country.ts +74 -0
  29. package/gb/index.ts +14 -0
  30. package/gb/postcode-area.ts +107 -0
  31. package/gb/postcode.ts +109 -0
  32. package/gb/street-type.ts +90 -0
  33. package/index.ts +38 -0
  34. package/jp/address-unit.ts +87 -0
  35. package/jp/index.ts +13 -0
  36. package/jp/postal-code.ts +93 -0
  37. package/jp/prefecture.ts +173 -0
  38. package/level-semantics.ts +623 -0
  39. package/nz/delivery-service.ts +211 -0
  40. package/nz/index.ts +12 -0
  41. package/nz/postcode.ts +42 -0
  42. package/package.json +81 -37
  43. package/postcode-systems.ts +68 -0
  44. package/tools/build-country-surface-lexicon.ts +166 -0
  45. package/tools/export-country-surfaces.ts +46 -0
  46. package/tools/generate-country-reference.ts +153 -0
  47. package/tools/generate-official-languages.ts +188 -0
  48. package/tools/index.ts +12 -0
  49. package/us/floor-designator.ts +119 -0
  50. package/us/index.ts +19 -0
  51. package/us/military-address.ts +199 -0
  52. package/us/po-box.ts +82 -0
  53. package/us/state.ts +156 -0
  54. package/us/street-directional.ts +220 -0
  55. package/us/street-suffix.ts +345 -0
  56. package/us/unit-designator.ts +223 -0
  57. package/us/zipcode.ts +212 -0
package/us/zipcode.ts ADDED
@@ -0,0 +1,212 @@
1
+ /**
2
+ * @copyright Sister Software
3
+ * @license AGPL-3.0
4
+ * @author Teffen Ellis, et al.
5
+ *
6
+ * US ZIP (Zone Improvement Plan) codes: branded string types, the shape patterns, the first-digit →
7
+ * state geographic prior, and a state-plus-ZIP plucker.
8
+ */
9
+
10
+ import type { Tagged } from "type-fest"
11
+
12
+ import { isUSStateAbbreviation, type USStateAbbreviation } from "./state.ts"
13
+
14
+ /**
15
+ * USPS-recognized ZIP code digits.
16
+ *
17
+ * @internal
18
+ */
19
+ export type ZipCodeDigit = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
20
+
21
+ /**
22
+ * A ZIP (Zone Improvement Plan) Code is a five-digit code assigned by the USPS to a section of a street, a collection
23
+ * of streets, an establishment, structure, or group of post office boxes, for the delivery of mail.
24
+ *
25
+ * - The first 3 digits of the ZIP code represent a specific central mail processing facility, which can be used to
26
+ * identify the locality and region of the address, i.e. the city and state.
27
+ * - The last 2 digits of the ZIP code represent a specific post office or delivery area.
28
+ *
29
+ * ```txt
30
+ * 90210
31
+ * / |⎿__(Post Office)
32
+ * / \
33
+ * (State) \
34
+ * \
35
+ * (City)
36
+ * ```
37
+ *
38
+ * Note that ZIP codes are not areas, but rather a group of deliverable addresses, which can and do change over time.
39
+ *
40
+ * @category Delivery
41
+ * @category Postal
42
+ * @title ZIP Code
43
+ * @pattern ^\d{5}$
44
+ * @see {@linkcode ZipCodePlusFour} for the extended ZIP code format.
45
+ */
46
+ export type ZipCode = Tagged<string, "ZipCode">
47
+
48
+ /**
49
+ * The extended ZIP code format includes the five-digit ZIP code followed by a hyphen and four additional digits. This
50
+ * extended format is used to provide more precise location information.
51
+ *
52
+ * - The first 3 digits of the ZIP code represent a specific central mail processing facility,
53
+ * - The last 2 digits of the ZIP code represent a specific post office or delivery area.
54
+ * - The four additional digits represent a specific delivery route within the ZIP code area.
55
+ *
56
+ * ```txt
57
+ * 90210-1234
58
+ * \_/\ /\__/
59
+ * / | \
60
+ * (State, City)_ / | \_ (Delivery Route)
61
+ * |
62
+ * (Post Office)
63
+ * ```
64
+ *
65
+ * Note that ZIP codes are not areas, but rather a group of deliverable addresses, which can and do change over time.
66
+ *
67
+ * @category Delivery
68
+ * @category Postal
69
+ * @type string
70
+ * @title ZIP Code+4
71
+ * @pattern ^\d{5}-\d{4}$
72
+ * @see {@linkcode ZipCode} for the standard ZIP code format.
73
+ */
74
+ export type ZipCodePlusFour = Tagged<string, "ZipCodePlusFour">
75
+
76
+ /**
77
+ * Type utility to extract the state abbreviation from a ZIP code.
78
+ *
79
+ * @internal
80
+ */
81
+ export type ExtractStateFromZipCode<Zip extends ZipCode | ZipCodePlusFour> =
82
+ Zip extends `${infer StateCode}${infer _Rest}` ? StateCode : never
83
+
84
+ /**
85
+ * Record of US state abbreviations to their corresponding ZIP code prefix (the leading digit). A cheap geographic
86
+ * prior: a 5-digit code's first digit narrows it to a band of states, which the parser can weigh against the
87
+ * surrounding city/state tokens.
88
+ *
89
+ * @internal
90
+ * @see {@linkcode ZipCodePrefixAbbreviationMap} for the reverse mapping.
91
+ */
92
+ export const StateAbbreviationZipCodePrefixRecord = {
93
+ AL: 3,
94
+ AK: 9,
95
+ AZ: 8,
96
+ AR: 7,
97
+ CA: 9,
98
+ CO: 8,
99
+ CT: 0,
100
+ DE: 1,
101
+ DC: 2,
102
+ FL: 3,
103
+ GA: 3,
104
+ HI: 9,
105
+ ID: 8,
106
+ IL: 6,
107
+ IN: 4,
108
+ IA: 5,
109
+ KS: 6,
110
+ KY: 4,
111
+ LA: 7,
112
+ ME: 0,
113
+ MD: 2,
114
+ MA: 0,
115
+ MI: 4,
116
+ MN: 5,
117
+ MS: 3,
118
+ MO: 6,
119
+ MT: 5,
120
+ NE: 6,
121
+ NV: 8,
122
+ NH: 0,
123
+ NJ: 0,
124
+ NM: 8,
125
+ NY: 1,
126
+ NC: 2,
127
+ ND: 5,
128
+ OH: 4,
129
+ OK: 7,
130
+ OR: 9,
131
+ PA: 1,
132
+ RI: 0,
133
+ SC: 2,
134
+ SD: 5,
135
+ TN: 3,
136
+ TX: 7,
137
+ UT: 8,
138
+ VT: 0,
139
+ VA: 2,
140
+ WA: 9,
141
+ WV: 2,
142
+ WI: 5,
143
+ WY: 8,
144
+ PR: 0,
145
+ GU: 9,
146
+ VI: 0,
147
+ MP: 9,
148
+ AS: 9,
149
+ } as const satisfies Record<USStateAbbreviation, ZipCodeDigit>
150
+
151
+ /**
152
+ * Map of ZIP code prefixes to their corresponding US state abbreviations.
153
+ */
154
+ const ZipCodePrefixAbbreviationMap = new Map<ZipCodeDigit, USStateAbbreviation[]>()
155
+
156
+ for (const [state, prefix] of Object.entries(StateAbbreviationZipCodePrefixRecord) as [
157
+ USStateAbbreviation,
158
+ ZipCodeDigit,
159
+ ][]) {
160
+ const states = ZipCodePrefixAbbreviationMap.get(prefix) ?? []
161
+ ZipCodePrefixAbbreviationMap.set(prefix, [...states, state])
162
+ }
163
+
164
+ export { ZipCodePrefixAbbreviationMap }
165
+
166
+ /**
167
+ * Regex patterns for ZIP codes.
168
+ */
169
+ export const ZipCodePatterns = {
170
+ /**
171
+ * 5-digit, or 9-digit ZIP code or ZIP+4 code.
172
+ */
173
+ Standard: /^\d{5}(?:[-\s]\d{4})?$/,
174
+
175
+ /**
176
+ * Two-letter state abbreviation followed by a 5-digit ZIP code or ZIP+4 code.
177
+ */
178
+ StateAbbreviationWithZipCode: /^(?:([A-Za-z]{2})[ ,]*)?([0-9]{5}(?:[-\s][0-9]{4})?)$/,
179
+ } as const
180
+
181
+ /**
182
+ * Type-predicate to determine if a value is a valid ZIP code.
183
+ */
184
+ export function isZipCode(input: unknown): input is ZipCode | ZipCodePlusFour {
185
+ return typeof input === "string" && ZipCodePatterns.Standard.test(input)
186
+ }
187
+
188
+ export interface PluckedStateZIPCodeResult {
189
+ stateAbbreviation: USStateAbbreviation | null
190
+ zipCode: ZipCode | ZipCodePlusFour
191
+ }
192
+
193
+ /**
194
+ * Given an address string like `"NY"`, `"CA 94016"`, attempts to match the state abbreviation and postal code, if
195
+ * applicable.
196
+ *
197
+ * @see {@linkcode isUSStateAbbreviation} to validate the state abbreviation.
198
+ */
199
+ export function pluckStateZIPCode(input: unknown): PluckedStateZIPCodeResult | null {
200
+ if (!input || typeof input !== "string") return null
201
+
202
+ const [, stateAbbreviation, zipCode = null] = input.match(ZipCodePatterns.StateAbbreviationWithZipCode) || []
203
+
204
+ if (!zipCode) return null
205
+
206
+ const normalizedState = stateAbbreviation?.toUpperCase()
207
+
208
+ return {
209
+ stateAbbreviation: isUSStateAbbreviation(normalizedState) ? normalizedState : null,
210
+ zipCode: zipCode as ZipCode | ZipCodePlusFour,
211
+ }
212
+ }