@mailwoman/resolver 9.4.0 → 10.1.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.
- package/README.md +9 -7
- package/lib/admin/coherence-passes.ts +21 -161
- package/lib/admin/containment.ts +12 -30
- package/lib/admin/jp-municipality.ts +20 -13
- package/lib/admin/winner.ts +30 -62
- package/lib/backend-capabilities.ts +25 -24
- package/lib/bare-toponym-race.ts +73 -39
- package/lib/decorate-node.ts +17 -41
- package/lib/fold-name.ts +5 -18
- package/lib/index.ts +5 -9
- package/lib/plausibility.ts +37 -63
- package/lib/postcode/country-coherence.ts +64 -284
- package/lib/postcode/prefix.ts +20 -84
- package/lib/postcode/shape-coherence.ts +23 -113
- package/lib/remote-resolver.ts +32 -29
- package/lib/rerank.ts +34 -50
- package/lib/resolve/passes.ts +145 -148
- package/lib/resolve.ts +726 -0
- package/lib/span-rescore.ts +215 -172
- package/lib/street/evidence.ts +65 -73
- package/lib/street/tier.ts +33 -175
- package/lib/toponym-prior.ts +53 -179
- package/out/admin/coherence-passes.d.ts +18 -69
- package/out/admin/coherence-passes.d.ts.map +1 -1
- package/out/admin/coherence-passes.js +19 -161
- package/out/admin/coherence-passes.js.map +1 -1
- package/out/admin/containment.d.ts +11 -29
- package/out/admin/containment.d.ts.map +1 -1
- package/out/admin/containment.js +12 -30
- package/out/admin/containment.js.map +1 -1
- package/out/admin/jp-municipality.d.ts +20 -13
- package/out/admin/jp-municipality.d.ts.map +1 -1
- package/out/admin/jp-municipality.js +18 -12
- package/out/admin/jp-municipality.js.map +1 -1
- package/out/admin/winner.d.ts +30 -62
- package/out/admin/winner.d.ts.map +1 -1
- package/out/admin/winner.js +18 -51
- package/out/admin/winner.js.map +1 -1
- package/out/{admin/index.d.ts → admin.d.ts} +1 -1
- package/out/admin.d.ts.map +1 -0
- package/out/{admin/index.js → admin.js} +1 -1
- package/out/admin.js.map +1 -0
- package/out/backend-capabilities.d.ts +18 -19
- package/out/backend-capabilities.d.ts.map +1 -1
- package/out/backend-capabilities.js +25 -24
- package/out/backend-capabilities.js.map +1 -1
- package/out/bare-toponym-race.d.ts +56 -28
- package/out/bare-toponym-race.d.ts.map +1 -1
- package/out/bare-toponym-race.js +69 -37
- package/out/bare-toponym-race.js.map +1 -1
- package/out/decorate-node.d.ts +6 -8
- package/out/decorate-node.d.ts.map +1 -1
- package/out/decorate-node.js +17 -41
- package/out/decorate-node.js.map +1 -1
- package/out/fold-name.d.ts +5 -18
- package/out/fold-name.d.ts.map +1 -1
- package/out/fold-name.js +5 -18
- package/out/fold-name.js.map +1 -1
- package/out/index.d.ts +5 -6
- package/out/index.d.ts.map +1 -1
- package/out/index.js +5 -8
- package/out/index.js.map +1 -1
- package/out/plausibility.d.ts +29 -58
- package/out/plausibility.d.ts.map +1 -1
- package/out/plausibility.js +16 -49
- package/out/plausibility.js.map +1 -1
- package/out/postcode/country-coherence.d.ts +48 -162
- package/out/postcode/country-coherence.d.ts.map +1 -1
- package/out/postcode/country-coherence.js +19 -252
- package/out/postcode/country-coherence.js.map +1 -1
- package/out/postcode/prefix.d.ts +17 -68
- package/out/postcode/prefix.d.ts.map +1 -1
- package/out/postcode/prefix.js +8 -77
- package/out/postcode/prefix.js.map +1 -1
- package/out/postcode/shape-coherence.d.ts +17 -84
- package/out/postcode/shape-coherence.d.ts.map +1 -1
- package/out/postcode/shape-coherence.js +9 -105
- package/out/postcode/shape-coherence.js.map +1 -1
- package/out/{postcode/index.d.ts → postcode.d.ts} +1 -1
- package/out/postcode.d.ts.map +1 -0
- package/out/{postcode/index.js → postcode.js} +1 -1
- package/out/postcode.js.map +1 -0
- package/out/remote-resolver.d.ts +27 -28
- package/out/remote-resolver.d.ts.map +1 -1
- package/out/remote-resolver.js +9 -19
- package/out/remote-resolver.js.map +1 -1
- package/out/rerank.d.ts +28 -45
- package/out/rerank.d.ts.map +1 -1
- package/out/rerank.js +13 -38
- package/out/rerank.js.map +1 -1
- package/out/resolve/passes.d.ts +97 -86
- package/out/resolve/passes.d.ts.map +1 -1
- package/out/resolve/passes.js +40 -105
- package/out/resolve/passes.js.map +1 -1
- package/out/resolve.d.ts +15 -0
- package/out/resolve.d.ts.map +1 -0
- package/out/resolve.js +543 -0
- package/out/resolve.js.map +1 -0
- package/out/span-rescore.d.ts +51 -64
- package/out/span-rescore.d.ts.map +1 -1
- package/out/span-rescore.js +132 -130
- package/out/span-rescore.js.map +1 -1
- package/out/street/evidence.d.ts +56 -61
- package/out/street/evidence.d.ts.map +1 -1
- package/out/street/evidence.js +22 -46
- package/out/street/evidence.js.map +1 -1
- package/out/street/tier.d.ts +17 -29
- package/out/street/tier.d.ts.map +1 -1
- package/out/street/tier.js +27 -175
- package/out/street/tier.js.map +1 -1
- package/out/{street/index.d.ts → street.d.ts} +1 -1
- package/out/street.d.ts.map +1 -0
- package/out/{street/index.js → street.js} +1 -1
- package/out/street.js.map +1 -0
- package/out/toponym-prior.d.ts +21 -109
- package/out/toponym-prior.d.ts.map +1 -1
- package/out/toponym-prior.js +41 -170
- package/out/toponym-prior.js.map +1 -1
- package/package.json +44 -147
- package/lib/resolve/index.ts +0 -995
- package/out/admin/index.d.ts.map +0 -1
- package/out/admin/index.js.map +0 -1
- package/out/postcode/index.d.ts.map +0 -1
- package/out/postcode/index.js.map +0 -1
- package/out/resolve/index.d.ts +0 -20
- package/out/resolve/index.d.ts.map +0 -1
- package/out/resolve/index.js +0 -807
- package/out/resolve/index.js.map +0 -1
- package/out/street/index.d.ts.map +0 -1
- package/out/street/index.js.map +0 -1
- package/vitest.config.ts +0 -38
- /package/lib/{admin/index.ts → admin.ts} +0 -0
- /package/lib/{postcode/index.ts → postcode.ts} +0 -0
- /package/lib/{street/index.ts → street.ts} +0 -0
|
@@ -2,143 +2,26 @@
|
|
|
2
2
|
* @copyright Sister Software
|
|
3
3
|
* @license AGPL-3.0
|
|
4
4
|
* @author Teffen Ellis, et al.
|
|
5
|
-
*
|
|
6
|
-
* Postcode-country coherence (#42) — the joint-consistency resolve keyed on the (postcode, locality)
|
|
7
|
-
* PAIR, and the first mechanism in the tree allowed to override `defaultCountry`.
|
|
8
|
-
*
|
|
9
|
-
* The failing case: `12 Rue de Rivoli, 75001 Paris` under the en-US locale. `--locale` defaults to
|
|
10
|
-
* `en-US`, `localeToCountry` turns that into `ResolveOpts.defaultCountry = "US"`, and the backend
|
|
11
|
-
* turns THAT into a hard `spr.country = 'US'` WHERE clause. The candidate pool is all-US before
|
|
12
|
-
* ranking begins, so a coarse placer that already called this address FR at confidence
|
|
13
|
-
* 0.9999908844 has nothing to promote — a soft re-rank downstream of a hard filter is inert by
|
|
14
|
-
* construction. Population then picks Paris, Texas (pop 24,969). With the postal extracts attached
|
|
15
|
-
* the answer gets WORSE, not merely wrong: the postcode resolves to the US row (ZIP 75001,
|
|
16
|
-
* Addison TX), `applyPostcodeConsistency` finds no Paris within its 50 km check (the nearest is
|
|
17
|
-
* 143.8 km), and falls the locality coordinate back to the ZIP point. See
|
|
18
|
-
* `docs/records/evals/2026-08-03-postcode-locality-scoping.md` for the instrumented diagnosis.
|
|
19
|
-
*
|
|
20
|
-
* No pattern-only change can fix this. `75001` is NOT unambiguously French:
|
|
21
|
-
* `candidateSystemsForPostcode("75001")` returns `["us", "de", "fr"]`, and the 2026-08-04
|
|
22
|
-
* candidate gazetteer holds the literal string in FOUR countries — FR 48.863,2.336 (Paris 1er),
|
|
23
|
-
* US 32.960,-96.838 (Addison TX), DE 48.844,9.367, PL 54.190,16.188 (×2 rows). The SHAPE is
|
|
24
|
-
* ambiguous four ways. The GEOMETRY is not: Addison and Paris are ~8,000 km apart, and only one
|
|
25
|
-
* of the four has a same-named locality next to its 75001.
|
|
26
|
-
*
|
|
27
|
-
* So this pass asks the question the shape cannot answer — _in which country are this postcode and
|
|
28
|
-
* this locality geographically consistent?_ — over exactly the primitive the resolver already uses
|
|
29
|
-
* three times (`applyAdminCoherence` #263, `applyExplicitCountryCoherence` #822,
|
|
30
|
-
* `applyRegionCountryCoherence`), just keyed on the postcode instead of a region or country token.
|
|
31
|
-
*
|
|
32
|
-
* ## Why it runs BEFORE the walk, not after
|
|
33
|
-
*
|
|
34
|
-
* Its three siblings are post-walk re-picks: the walk resolves greedily, they swap the wrong node
|
|
35
|
-
* pair for the right one. That shape cannot work here, because the thing to correct is the walk's
|
|
36
|
-
* COUNTRY SCOPE, and the scope poisons three separate things a post-walk pass cannot reach — the
|
|
37
|
-
* postcode node's own resolution (Addison, not Paris 1er), the `applyPostcodeConsistency` fallback
|
|
38
|
-
* that then drags the locality onto it, and the `country` filter on every other admin lookup. So
|
|
39
|
-
* this pass is a pre-walk SCOPE decision: it runs once, before the first lookup, and replaces
|
|
40
|
-
* `state.defaultCountry` for the whole walk. Everything downstream — including the three post-walk
|
|
41
|
-
* coherence passes and the street/rooftop tiers — then sees the corrected country.
|
|
42
|
-
*
|
|
43
|
-
* ## Why it is not a hard veto (the doctrine check)
|
|
44
|
-
*
|
|
45
|
-
* Registries are SOFT priors here, positive evidence only. Two properties keep this one honest:
|
|
46
|
-
*
|
|
47
|
-
* 1. **The default country is tested FIRST, and a coherent default always wins.** If (postcode,
|
|
48
|
-
* locality) is consistent under `defaultCountry`, the pass returns `null` immediately, having
|
|
49
|
-
* spent at most two lookups, and the walk is byte-identical. Every correctly-scoped domestic
|
|
50
|
-
* parse exits here. It is not "US loses to FR"; it is "US had no answer".
|
|
51
|
-
* 2. **Abstention is the default outcome.** Zero coherent countries (a wrong-for-the-city
|
|
52
|
-
* postcode, or a recall gap in the gazetteer) and MORE than one coherent country (a genuine
|
|
53
|
-
* geographic tie) both return `null`. The mechanism only ever speaks when exactly one country
|
|
54
|
-
* makes the pair consistent, and only ever when the default country could not.
|
|
55
|
-
*
|
|
56
|
-
* ## What #24 changed (2026-08-10)
|
|
57
|
-
*
|
|
58
|
-
* Two structural gaps, both found by replaying the 420-row panel-v2 benchmark. The pass is default-ON,
|
|
59
|
-
* the eu-mixed block gave it 29 chances, and it fired **zero** times — while the right answer sat in
|
|
60
|
-
* the shipped gazetteer within 4 km of truth on 21 of them.
|
|
61
|
-
*
|
|
62
|
-
* 1. **The candidate countries came from a proxy.** `candidateSystemsForPostcode` is a shape test over
|
|
63
|
-
* the eight slices codex specifies (`us de fr ca gb jp au nz`), so CZ, CH, BE, DK, AT, NL and IT
|
|
64
|
-
* could never be PROPOSED, however good the evidence. `13000` shapes as `[us, de, fr]`; the
|
|
65
|
-
* gazetteer holds it in exactly one country, and that country is CZ. The candidate set now unions
|
|
66
|
-
* the shape list with the countries the gazetteer actually holds the postcode in — one unscoped
|
|
67
|
-
* lookup, exhaustive at the measured cardinality (the most-shared code in the artifact carries 12
|
|
68
|
-
* rows across 8 countries). Every candidate still has to pass the same geometric pair test, so the
|
|
69
|
-
* widening can only convert an abstention into a verdict or a verdict into an abstention, never a
|
|
70
|
-
* verdict into a different one.
|
|
71
|
-
* 2. **The pair was all-or-nothing.** Ten panel rows had unimpeachable one-sided evidence and no pair:
|
|
72
|
-
* the gazetteer carries no CH or BE postcodes at all (so `Sarnen` and `Kelmis` could never agree
|
|
73
|
-
* with anything), and `Praha 3` / `Praha 9` are municipal districts no admin gazetteer names (so
|
|
74
|
-
* `13000` had nothing to agree WITH). Each half may now speak alone — but only when the DEFAULT
|
|
75
|
-
* country corroborates NEITHER half, and only when the speaking half names exactly one country in
|
|
76
|
-
* the whole gazetteer. See {@link PostcodeCountryScopeEvidence}.
|
|
77
|
-
*
|
|
78
|
-
* The abstention doctrine is unchanged and is what bounds the new rungs: a two-country tie is a HARD
|
|
79
|
-
* abstention that never falls through to the single-sided rungs. Corroboration at home bounds them
|
|
80
|
-
* asymmetrically: the address's own POSTCODE existing in the default country stops the pass dead,
|
|
81
|
-
* while a LOCALITY value at home neutralizes that value as a foreign witness without silencing its
|
|
82
|
-
* siblings (the per-value rule in the single-sided section below).
|
|
83
|
-
*
|
|
84
|
-
* ## Evidence
|
|
85
|
-
*
|
|
86
|
-
* Prototyped out-of-tree against the live gazetteer over the public `findPlace` surface (2026-08-03):
|
|
87
|
-
* 400 real US (ZIP, city) pairs from `postalcode-us.db` + 400 real FR (CP, commune) pairs from
|
|
88
|
-
* `postcode-locality-fr.db` — 800 pairs, **zero** border crossings, at both the 15 km and 25 km
|
|
89
|
-
* checks. The 22 US abstentions were pairs whose ZIP parent name is not an exact-matching locality
|
|
90
|
-
* in the admin gazetteer (a recall gap; abstention is the safe outcome). The confound board's
|
|
91
|
-
* verdicts were identical at 15, 25 AND 50 km, so the mechanism is not check-tuned; the default
|
|
92
|
-
* below is the 25 km the scale run measured.
|
|
93
|
-
*
|
|
94
|
-
* Cost: 2 lookups on the byte-stable path (postcode + locality under the default country), and at
|
|
95
|
-
* most `2 × |candidateSystems| × |localityValues|` — candidates bounded at 12, locality values at 3,
|
|
96
|
-
* and the common tree carries exactly one — when the default is incoherent. Nothing runs at all
|
|
97
|
-
* unless the tree carries both a postcode and a locality.
|
|
98
|
-
*
|
|
99
|
-
* **Default-ON** since the operator promotion of 2026-08-05 (#1477) — the resolver gauntlet, pinned
|
|
100
|
-
* both ways, returned zero newly-failing conditional cases, and 56,000 pair evaluations across both
|
|
101
|
-
* backends returned zero false positives. `ResolveOpts.postcodeCountryCoherence: false` opts out,
|
|
102
|
-
* and the walk is byte-stable then. Receipts:
|
|
103
|
-
* `docs/records/evals/2026-08-05-postcode-coherence-default-on-evidence.md`.
|
|
104
5
|
*/
|
|
105
|
-
// `postcode-systems` has no dedicated export subpath; the barrel is where every other consumer
|
|
106
|
-
// (`neural/postcode-anchor.ts`) reaches it from.
|
|
107
6
|
import { candidateSystemsForPostcode } from "@mailwoman/codex";
|
|
108
|
-
import { walkNodes } from "@mailwoman/core/decoder";
|
|
7
|
+
import { firstNodeWhere, walkNodes } from "@mailwoman/core/decoder";
|
|
109
8
|
import { haversineKm } from "@mailwoman/spatial";
|
|
110
9
|
/**
|
|
111
|
-
*
|
|
112
|
-
*
|
|
10
|
+
* The default maximum distance, in kilometers, between a postcode and a locality
|
|
11
|
+
* for them to count as consistent.
|
|
12
|
+
*
|
|
13
|
+
* Verdicts did not change anywhere between 15 and 50 km, so the exact value matters little.
|
|
113
14
|
*/
|
|
114
15
|
export const POSTCODE_COUNTRY_COHERENCE_THRESHOLD_KM = 25;
|
|
115
|
-
/**
|
|
116
|
-
* A usable coordinate — the gazetteer stores an unlocated row as 0,0, which haversine would measure against.
|
|
117
|
-
*/
|
|
118
16
|
function hasCoord(p) {
|
|
119
17
|
return p.lat !== 0 || p.lon !== 0;
|
|
120
18
|
}
|
|
121
|
-
/**
|
|
122
|
-
* Upper bound on locality values the pass will consider. A parse rarely tags more than two; the cap keeps a
|
|
123
|
-
* pathological tree from multiplying the pair sweep.
|
|
124
|
-
*/
|
|
125
19
|
const MAX_LOCALITY_VALUES = 3;
|
|
126
20
|
/**
|
|
127
|
-
*
|
|
128
|
-
*
|
|
129
|
-
* order.
|
|
21
|
+
* Returns up to three distinct locality values in document order, listing every
|
|
22
|
+
* `locality` before any `dependent_locality`.
|
|
130
23
|
*
|
|
131
|
-
*
|
|
132
|
-
* Laurell Road, Gander, NL A1V 0A9` — where the model tags both `Gander` and the province abbreviation `NL` as
|
|
133
|
-
* localities — the pass keyed its whole country verdict on "NL", whose only exact locality-band bearer is an alias of
|
|
134
|
-
* Nal, Afghanistan, and the walk resolved a Newfoundland street 10,000 km away. The FIRST-written locality is the one
|
|
135
|
-
* the address is about.
|
|
136
|
-
*
|
|
137
|
-
* ALL values matter, not just the first. On `Calle Mayor 12, Aravaca, 28023 Madrid` the model tags both `Aravaca` and
|
|
138
|
-
* `Madrid` as localities; `Aravaca` is a neighbourhood the locality band cannot see, while `Madrid` carries the ES pair
|
|
139
|
-
* verdict (the ES 28023 row sits 9.6 km from the Madrid locality row). A pass keyed on a single value bets the whole
|
|
140
|
-
* country verdict on whichever one a traversal order happens to pick — first-only re-scoped that address to the US ZIP
|
|
141
|
-
* 28023 centroid in North Carolina. So the verdict rungs try each value in order until one produces evidence.
|
|
24
|
+
* Order matters because the first locality written is the one the address is about.
|
|
142
25
|
*/
|
|
143
26
|
export function localityValuesInDocumentOrder(roots) {
|
|
144
27
|
const out = [];
|
|
@@ -149,7 +32,7 @@ export function localityValuesInDocumentOrder(roots) {
|
|
|
149
32
|
return out.slice(0, MAX_LOCALITY_VALUES);
|
|
150
33
|
}
|
|
151
34
|
/**
|
|
152
|
-
*
|
|
35
|
+
* Returns the first value of {@link localityValuesInDocumentOrder}.
|
|
153
36
|
*/
|
|
154
37
|
export function firstLocalityValue(roots) {
|
|
155
38
|
return localityValuesInDocumentOrder(roots)[0];
|
|
@@ -167,37 +50,9 @@ function collectInDocumentOrder(nodes, tag, out, seen) {
|
|
|
167
50
|
collectInDocumentOrder(n.children, tag, out, seen);
|
|
168
51
|
}
|
|
169
52
|
}
|
|
170
|
-
/**
|
|
171
|
-
* Over-fetch for the UNSCOPED holder probes (#24). Postcodes are bounded and small — the 2026-08-10 candidate
|
|
172
|
-
* gazetteer's most-shared code carries 12 rows across 8 countries — so 20 sees every bearer and the postcode holder set
|
|
173
|
-
* is COMPLETE, not a sample. Locality names are not bounded that way (`rampur` has 1,096 rows, `bara` spans 42
|
|
174
|
-
* countries), so 30 is a population-first WINDOW: it can only ever hide a country, which turns a "more than one
|
|
175
|
-
* country" abstention into a false "exactly one". The explicit default-country probe below is what closes that gap for
|
|
176
|
-
* the case that matters (the address is domestic after all).
|
|
177
|
-
*/
|
|
178
53
|
const POSTCODE_HOLDER_FETCH = 20;
|
|
179
54
|
const LOCALITY_HOLDER_FETCH = 30;
|
|
180
|
-
/**
|
|
181
|
-
* Upper bound on countries the pair rung will test, so a pathological name can't turn one incoherent parse into a
|
|
182
|
-
* hundred lookups. The postcode holder set is ≤8 countries in the measured artifact and the codex shape list is ≤8, so
|
|
183
|
-
* this only ever binds on a gazetteer very unlike today's.
|
|
184
|
-
*/
|
|
185
55
|
const MAX_CANDIDATE_COUNTRIES = 12;
|
|
186
|
-
/**
|
|
187
|
-
* Countries that hold an exact, coordinate-bearing row for `text` at `placetype`, mapped to their best (first) such
|
|
188
|
-
* row. One UNSCOPED lookup.
|
|
189
|
-
*
|
|
190
|
-
* This is the #24 candidate source, and it replaces a proxy with the thing itself. The pass used to ask codex "which
|
|
191
|
-
* address SYSTEMS could this shape be?" — a model-free shape test over the eight slices codex specifies. That answer is
|
|
192
|
-
* correct and useless for two thirds of Europe: `13000` shapes as `[us, de, fr]`, the gazetteer holds it in exactly one
|
|
193
|
-
* country (CZ), and CZ has no codex slice, so the pass could not propose the only country that could possibly be right.
|
|
194
|
-
* Membership in the gazetteer is positive evidence of the same kind the pair test already runs on, available for one
|
|
195
|
-
* lookup, and it is EXHAUSTIVE for postcodes at the measured cardinality.
|
|
196
|
-
*
|
|
197
|
-
* `exactMatch !== false` is the admission bar: a fuzzy postcode hit is a DIFFERENT postcode (the 2026-08-05 Code-Point
|
|
198
|
-
* lesson — `BT3 9QQ` trigram-matching Sheffield's `S3 9QQ`), and a fuzzy locality hit is evidence about the index, not
|
|
199
|
-
* about the country. Backends that do not stamp the flag leave it undefined and still contribute.
|
|
200
|
-
*/
|
|
201
56
|
async function countriesHolding(backend, text, placetype, limit, opts) {
|
|
202
57
|
const out = new Map();
|
|
203
58
|
let hits;
|
|
@@ -205,7 +60,7 @@ async function countriesHolding(backend, text, placetype, limit, opts) {
|
|
|
205
60
|
hits = await backend.findPlace({ text, placetype, limit, ...(opts?.primaryOnly ? { primaryOnly: true } : {}) });
|
|
206
61
|
}
|
|
207
62
|
catch {
|
|
208
|
-
return out;
|
|
63
|
+
return out;
|
|
209
64
|
}
|
|
210
65
|
for (const hit of hits) {
|
|
211
66
|
const country = hit.country?.trim().toUpperCase();
|
|
@@ -217,13 +72,6 @@ async function countriesHolding(backend, text, placetype, limit, opts) {
|
|
|
217
72
|
}
|
|
218
73
|
return out;
|
|
219
74
|
}
|
|
220
|
-
/**
|
|
221
|
-
* Is the (postcode, locality) pair geographically consistent in `country`? Returns the winning pair and its distance,
|
|
222
|
-
* or `null` when the postcode does not resolve there, no same-named locality exists there, or the nearest one is
|
|
223
|
-
* outside the check. Costs one postcode lookup plus (only if that hit) one locality lookup — or just the locality
|
|
224
|
-
* lookup when the caller already knows the country's postcode row (`knownPostcodePlace`, from the exhaustive unscoped
|
|
225
|
-
* probe).
|
|
226
|
-
*/
|
|
227
75
|
async function coherenceIn(country, postcode, locality, backend, thresholdKm, knownPostcodePlace) {
|
|
228
76
|
let postcodePlace = knownPostcodePlace;
|
|
229
77
|
if (!postcodePlace) {
|
|
@@ -232,7 +80,7 @@ async function coherenceIn(country, postcode, locality, backend, thresholdKm, kn
|
|
|
232
80
|
postcodeHits = await backend.findPlace({ text: postcode, placetype: "postalcode", country, limit: 3 });
|
|
233
81
|
}
|
|
234
82
|
catch {
|
|
235
|
-
return null;
|
|
83
|
+
return null;
|
|
236
84
|
}
|
|
237
85
|
postcodePlace = postcodeHits.find(hasCoord);
|
|
238
86
|
}
|
|
@@ -245,9 +93,6 @@ async function coherenceIn(country, postcode, locality, backend, thresholdKm, kn
|
|
|
245
93
|
catch {
|
|
246
94
|
return null;
|
|
247
95
|
}
|
|
248
|
-
// EXACT matches only. A fuzzy same-country hit ("Paris" → "Parish") is not evidence that the postcode
|
|
249
|
-
// belongs to this country; it is evidence that the FTS index is generous. Backends that do not stamp
|
|
250
|
-
// `exactMatch` therefore contribute nothing here rather than contributing noise.
|
|
251
96
|
let best = null;
|
|
252
97
|
for (const candidate of localityHits) {
|
|
253
98
|
if (!candidate.exactMatch || !hasCoord(candidate))
|
|
@@ -262,64 +107,34 @@ async function coherenceIn(country, postcode, locality, backend, thresholdKm, kn
|
|
|
262
107
|
return best ? { postcodePlace, ...best } : null;
|
|
263
108
|
}
|
|
264
109
|
/**
|
|
265
|
-
*
|
|
110
|
+
* Finds the one country, other than the caller's default, in which the address's
|
|
111
|
+
* postcode and locality are consistent.
|
|
266
112
|
*
|
|
267
|
-
*
|
|
268
|
-
*
|
|
269
|
-
* make the pair consistent are the codex shape's other candidate systems tried, and only a UNIQUE coherent alternative
|
|
270
|
-
* produces a verdict — zero (no evidence) and two-or-more (a genuine tie) both abstain.
|
|
271
|
-
*
|
|
272
|
-
* The candidate set is `candidateSystemsForPostcode` — a model-free SHAPE test over each codex slice's own postcode
|
|
273
|
-
* pattern, no safelist and no prior. Its `SystemCode` values are ISO-3166 alpha-2 in lower case, so the upper-casing
|
|
274
|
-
* below is the whole conversion. Note the corollary: a country with no codex slice can never be proposed (the gazetteer
|
|
275
|
-
* holds `75001` in PL, and this pass will never return PL), which bounds the mechanism to the systems whose postcode
|
|
276
|
-
* shapes are actually specified.
|
|
113
|
+
* It returns `null` when the address has a country node, when the default country is
|
|
114
|
+
* already consistent, or when more than one alternative qualifies.
|
|
277
115
|
*/
|
|
278
116
|
export async function findPostcodeCountryScope(roots, backend, opts) {
|
|
279
117
|
const postcode = opts.postcode.trim();
|
|
280
118
|
const defaultCountry = opts.defaultCountry?.trim().toUpperCase() || undefined;
|
|
281
119
|
if (!postcode)
|
|
282
120
|
return null;
|
|
121
|
+
if (firstNodeWhere(roots, (n) => n.tag === "country" && n.value.trim()))
|
|
122
|
+
return null;
|
|
283
123
|
const localities = localityValuesInDocumentOrder(roots);
|
|
284
|
-
// Both halves of the pair are required. A postcode with no locality has nothing to be coherent WITH, which is what
|
|
285
|
-
// keeps this pass inert on "Springfield, IL 62701" (the parser tags Springfield as a `street` — a separate defect)
|
|
286
|
-
// and on every bare-postcode query.
|
|
287
124
|
if (!localities.length)
|
|
288
125
|
return null;
|
|
289
126
|
const thresholdKm = opts.thresholdKm ?? POSTCODE_COUNTRY_COHERENCE_THRESHOLD_KM;
|
|
290
|
-
// 1. Is the caller's own default country coherent with ANY of the address's locality values? If so we are done —
|
|
291
|
-
// positive evidence for the default, no override, and the common domestic path (one locality value) costs two
|
|
292
|
-
// lookups and changes nothing. With no default in force (the browser cascade) there is nothing to test — the
|
|
293
|
-
// sweep below carries the whole verdict.
|
|
294
127
|
if (defaultCountry) {
|
|
295
128
|
for (const locality of localities) {
|
|
296
129
|
if (await coherenceIn(defaultCountry, postcode, locality, backend, thresholdKm))
|
|
297
130
|
return null;
|
|
298
131
|
}
|
|
299
132
|
}
|
|
300
|
-
// 2. The default could not place this pair. Which countries could? Two sources, unioned:
|
|
301
|
-
//
|
|
302
|
-
// - the SHAPE list (`candidateSystemsForPostcode`, or the shape-coherence pass's narrowing when #31
|
|
303
|
-
// Mechanism 1 supplied one — a pure subset of it), and
|
|
304
|
-
// - the countries the GAZETTEER actually holds this postcode in (#24). Exhaustive at the measured
|
|
305
|
-
// cardinality, and the only source that can name a country codex has no slice for — which was the
|
|
306
|
-
// whole reason the CZ/CH/BE/DK/AT/NL block of the 2026-08-09 panel resolved to US namesakes while
|
|
307
|
-
// their own postcodes sat in the shipped gazetteer within 4 km of truth.
|
|
308
|
-
//
|
|
309
|
-
// The union can only ADD countries, and every added one must still pass the same geometric pair test,
|
|
310
|
-
// so this widens recall without weakening the verdict: a second coherent country turns a verdict into
|
|
311
|
-
// an abstention (safe), never into a wrong answer.
|
|
312
133
|
const pcHolders = await countriesHolding(backend, postcode, "postalcode", POSTCODE_HOLDER_FETCH);
|
|
313
134
|
const shapeSystems = (opts.candidateSystems ?? candidateSystemsForPostcode(postcode).map((system) => system.toUpperCase())).map((country) => country.trim().toUpperCase());
|
|
314
135
|
const candidates = [...new Set([...shapeSystems, ...pcHolders.keys()])]
|
|
315
136
|
.filter((country) => country !== defaultCountry)
|
|
316
137
|
.slice(0, MAX_CANDIDATE_COUNTRIES);
|
|
317
|
-
// 3. The pair sweep, per locality value in document order: the first value that produces evidence decides. A value
|
|
318
|
-
// the band cannot see (`Aravaca`, a neighbourhood) yields zero coherent countries and the NEXT value gets its
|
|
319
|
-
// turn — betting the verdict on a single value re-scoped that address to a US ZIP centroid (see
|
|
320
|
-
// localityValuesInDocumentOrder). Exactly one coherent country is a verdict. Two coherent countries mean the
|
|
321
|
-
// geometry genuinely does not decide, and guessing between them is precisely what this mechanism exists not to
|
|
322
|
-
// do — a TIE is a hard abstention, never a fall-through to a later value or the weaker rungs below.
|
|
323
138
|
for (const locality of localities) {
|
|
324
139
|
const coherent = [];
|
|
325
140
|
for (const country of candidates) {
|
|
@@ -333,41 +148,10 @@ export async function findPostcodeCountryScope(roots, backend, opts) {
|
|
|
333
148
|
if (coherent.length > 1)
|
|
334
149
|
return null;
|
|
335
150
|
}
|
|
336
|
-
// The single-sided rungs below lean on the DEFAULT country as the domestic-plausibility guard
|
|
337
|
-
// ("Vienna, VA" stays in Virginia because the default corroborates a half). With no default there
|
|
338
|
-
// is no such anchor, so the no-default path ends at the pair rung: a pair verdict (Zabiče) is
|
|
339
|
-
// in-scope, a single-sided guess is not.
|
|
340
151
|
if (!defaultCountry)
|
|
341
152
|
return null;
|
|
342
|
-
// 4. No country makes the PAIR consistent. On the 2026-08-09 panel that happened 10 times for two opposite
|
|
343
|
-
// reasons, and in both the address's country was never in doubt — only unrepresentable as a pair:
|
|
344
|
-
//
|
|
345
|
-
// - the gazetteer holds no postcodes for the country at all (CH, BE), so the postcode half can never
|
|
346
|
-
// agree with anything no matter how good the locality evidence is; or
|
|
347
|
-
// - the locality is a name no admin gazetteer carries (`Praha 3`, `Praha 9` — municipal districts).
|
|
348
|
-
//
|
|
349
|
-
// So each half gets to speak alone, under conditions that keep this from becoming a guess. The
|
|
350
|
-
// POSTCODE half is all-or-nothing: the default country holding this postcode makes the address
|
|
351
|
-
// domestically plausible outright, and nothing foreign may be proposed. The LOCALITY half is
|
|
352
|
-
// judged PER VALUE — a value the default country holds is domestic corroboration for THAT value
|
|
353
|
-
// (it is neutralized as a foreign witness) without silencing its siblings. `14 Long St, Green
|
|
354
|
-
// Point, Cape Town, 8001` is why: "Green Point" has pop-0 US namesakes, and letting that one
|
|
355
|
-
// value abstain the whole pass handed a uniquely-ZA "Cape Town" to a hard US filter (the walk
|
|
356
|
-
// answered Green Point, Pennsylvania). `123 Main St, Vienna, VA 22180` still holds in Virginia:
|
|
357
|
-
// its ONLY locality value is domestic, so no foreign witness remains. The speaking value must
|
|
358
|
-
// name EXACTLY ONE country in the entire gazetteer — the same uniqueness bar the pair rung uses,
|
|
359
|
-
// applied to one side — and all speaking values must AGREE on that country.
|
|
360
153
|
if (pcHolders.has(defaultCountry))
|
|
361
154
|
return null;
|
|
362
|
-
// 4a. A locality value names exactly one country. Stronger than the postcode rung — a place name is far
|
|
363
|
-
// more discriminative than a four-digit code — so it is tried first. A verdict stands down when that
|
|
364
|
-
// country's OWN postcode row contradicts the locality (outside the check): the two halves disagreeing
|
|
365
|
-
// inside one country is exactly what the pair test exists to catch, and a single-sided rung must not
|
|
366
|
-
// launder it.
|
|
367
|
-
// PRIMARY-keyed rows only (#1626's re-reading discipline, applied to a country verdict): "the locality
|
|
368
|
-
// names exactly one country" must mean a place NAMED that, never a place aliased to it — "NL"'s only
|
|
369
|
-
// locality-band bearer was an exact ALIAS of Nal, Afghanistan, and a single alias row steered the whole
|
|
370
|
-
// walk's country. The pair rung keeps aliases: there the postcode's own geometry corroborates them.
|
|
371
155
|
let anyDomestic = false;
|
|
372
156
|
let anyLocalityKnown = false;
|
|
373
157
|
const verdicts = new Map();
|
|
@@ -392,15 +176,10 @@ export async function findPostcodeCountryScope(roots, backend, opts) {
|
|
|
392
176
|
verdicts.set(country, { country, postcode, locality, evidence: "locality", localityPlace });
|
|
393
177
|
}
|
|
394
178
|
}
|
|
395
|
-
// Every speaking value must agree: two values uniquely naming DIFFERENT countries is a genuine
|
|
396
|
-
// ambiguity, and guessing between them is what this mechanism exists not to do.
|
|
397
179
|
if (verdicts.size === 1)
|
|
398
180
|
return [...verdicts.values()][0];
|
|
399
181
|
if (verdicts.size > 1)
|
|
400
182
|
return null;
|
|
401
|
-
// 4b. The postcode is held in exactly one country and the locality half is silent EVERYWHERE — no value
|
|
402
|
-
// is domestic and no value names any country. `Biskupcova 1843/3, 13000 Praha 3`: `13000` is a CZ
|
|
403
|
-
// code and nothing else, anywhere, and `Praha 3` (a municipal district) is in no admin gazetteer.
|
|
404
183
|
if (!anyDomestic && !anyLocalityKnown && pcHolders.size === 1) {
|
|
405
184
|
const [country, postcodePlace] = [...pcHolders.entries()][0];
|
|
406
185
|
if (country !== defaultCountry) {
|
|
@@ -409,26 +188,18 @@ export async function findPostcodeCountryScope(roots, backend, opts) {
|
|
|
409
188
|
}
|
|
410
189
|
return null;
|
|
411
190
|
}
|
|
412
|
-
/**
|
|
413
|
-
* Does `country` hold an exact, coordinate-bearing locality row for `locality`? One scoped lookup — deliberately NOT
|
|
414
|
-
* read off the unscoped holder window, which is population-first and truncated, so a small domestic bearer could fall
|
|
415
|
-
* below its fold and let a foreign scope through.
|
|
416
|
-
*/
|
|
417
191
|
async function holdsLocality(backend, locality, country) {
|
|
418
192
|
try {
|
|
419
193
|
const hits = await backend.findPlace({ text: locality, placetype: "locality", country, limit: 3 });
|
|
420
194
|
return hits.some((hit) => hit.exactMatch && hasCoord(hit));
|
|
421
195
|
}
|
|
422
196
|
catch {
|
|
423
|
-
// A backend hiccup must not be read as "the default country has nothing" — that would license a foreign
|
|
424
|
-
// scope on no evidence. Treat it as corroboration and abstain.
|
|
425
197
|
return true;
|
|
426
198
|
}
|
|
427
199
|
}
|
|
428
200
|
/**
|
|
429
|
-
*
|
|
430
|
-
*
|
|
431
|
-
* untouched, this only writes `metadata`.
|
|
201
|
+
* Records an adopted country scope and its evidence in the `metadata` of the
|
|
202
|
+
* tree's postcode and locality nodes.
|
|
432
203
|
*/
|
|
433
204
|
export function stampPostcodeCountryScope(roots, scope) {
|
|
434
205
|
for (const n of walkNodes(roots)) {
|
|
@@ -437,11 +208,7 @@ export function stampPostcodeCountryScope(roots, scope) {
|
|
|
437
208
|
n.metadata = {
|
|
438
209
|
...n.metadata,
|
|
439
210
|
postcode_country_scope: scope.country,
|
|
440
|
-
// WHICH rung spoke — a two-sided agreement and a one-sided uniqueness claim are different evidence,
|
|
441
|
-
// and a consumer that cannot tell them apart cannot weight them differently.
|
|
442
211
|
postcode_country_scope_evidence: scope.evidence,
|
|
443
|
-
// Only the `pair` rung measured a distance. Writing a 0 for the single-sided rungs would assert an
|
|
444
|
-
// agreement nobody tested.
|
|
445
212
|
...(scope.distanceKm !== undefined ? { postcode_country_scope_km: scope.distanceKm } : {}),
|
|
446
213
|
};
|
|
447
214
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"country-coherence.js","sourceRoot":"","sources":["../../lib/postcode/country-coherence.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"country-coherence.js","sourceRoot":"","sources":["../../lib/postcode/country-coherence.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,2BAA2B,EAAE,MAAM,kBAAkB,CAAA;AAC9D,OAAO,EAAE,cAAc,EAAE,SAAS,EAAoB,MAAM,yBAAyB,CAAA;AAErF,OAAO,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAA;AAEhD;;;;;GAKG;AACH,MAAM,CAAC,MAAM,uCAAuC,GAAG,EAAE,CAAA;AA6FzD,SAAS,QAAQ,CAAC,CAAgB;IACjC,OAAO,CAAC,CAAC,GAAG,KAAK,CAAC,IAAI,CAAC,CAAC,GAAG,KAAK,CAAC,CAAA;AAClC,CAAC;AAED,MAAM,mBAAmB,GAAG,CAAC,CAAA;AAE7B;;;;;GAKG;AACH,MAAM,UAAU,6BAA6B,CAAC,KAA6B;IAC1E,MAAM,GAAG,GAAa,EAAE,CAAA;IACxB,MAAM,IAAI,GAAG,IAAI,GAAG,EAAU,CAAA;IAE9B,KAAK,MAAM,GAAG,IAAI,CAAC,UAAU,EAAE,oBAAoB,CAAU,EAAE,CAAC;QAC/D,sBAAsB,CAAC,KAAK,EAAE,GAAG,EAAE,GAAG,EAAE,IAAI,CAAC,CAAA;IAC9C,CAAC;IAED,OAAO,GAAG,CAAC,KAAK,CAAC,CAAC,EAAE,mBAAmB,CAAC,CAAA;AACzC,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,kBAAkB,CAAC,KAA6B;IAC/D,OAAO,6BAA6B,CAAC,KAA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AO,CAAC,SAAS,CAAC,EAAE,IAAI,EAAE,QAAQ,EAAE,SAAS,EAAE,UAAU,EAAE,OAAO,EAAE,KAAK,EAAE,CAAC,EAAE,CAAC,CAAA;QAElG,OAAO,IAAI,CAAC,IAAI,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,CAAC,UAAU,IAAI,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAA;IAC3D,CAAC;IAAC,MAAM,CAAC;QACR,OAAO,IAAI,CAAA;IACZ,CAAC;AACF,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,yBAAyB,CAAC,KAA6B,EAAE,KAA2B;IACnG,KAAK,MAAM,CAAC,IAAI,SAAS,CAAC,KAAK,CAAC,EAAE,CAAC;QAClC,IAAI,CAAC,CAAC,GAAG,KAAK,UAAU,IAAI,CAAC,CAAC,GAAG,KAAK,UAAU,IAAI,CAAC,CAAC,GAAG,KAAK,oBAAoB;YAAE,SAAQ;QAE5F,CAAC,CAAC,QAAQ,GAAG;YACZ,GAAG,CAAC,CAAC,QAAQ;YACb,sBAAsB,EAAE,KAAK,CAAC,OAAO;YAErC,+BAA+B,EAAE,KAAK,CAAC,QAAQ;YAE/C,GAAG,CAAC,KAAK,CAAC,UAAU,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,yBAAyB,EAAE,KAAK,CAAC,UAAU,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;SAC1F,CAAA;IACF,CAAC;AACF,CAAC"}
|
package/out/postcode/prefix.d.ts
CHANGED
|
@@ -2,74 +2,24 @@
|
|
|
2
2
|
* @copyright Sister Software
|
|
3
3
|
* @license AGPL-3.0
|
|
4
4
|
* @author Teffen Ellis, et al.
|
|
5
|
-
*
|
|
6
|
-
* Postcode-prefix prior (#31, Mechanism 3) — the partial-code prior for postcodes the full-code
|
|
7
|
-
* gazetteer does not carry. #1480's abstention is the defect: a BT unit with no permissive source
|
|
8
|
-
* behind it (the NI half of Code-Point Open) misses the gazetteer, and the tree contributes
|
|
9
|
-
* NOTHING for it — no country scope, no district, no coordinate. This module derives the code's
|
|
10
|
-
* PREFIX and probes the PFX1 index (`postcode-prefix-<cc>.bin`), so the resolve "abstains on the
|
|
11
|
-
* UNIT and still contributes its DISTRICT": the node resolves from the prefix's centroid and/or
|
|
12
|
-
* ancestry, whichever the artifact carries.
|
|
13
|
-
*
|
|
14
|
-
* The index is a structural type (`PostcodePrefixIndexLike`, declared in `core/resolver/types.ts`),
|
|
15
|
-
* injected by the pipeline and never imported from `@mailwoman/neural` — B3-5: the probe touches
|
|
16
|
-
* zero model inputs. The loader lives in `@mailwoman/neural/postcode-prefix-index.ts` (PFX1 format:
|
|
17
|
-
* magic, header, dictionary, quantized i16 coordinates, `radiusP95Km`, ancestry); the resolver
|
|
18
|
-
* side only defines the derivation law and the probe.
|
|
19
|
-
*
|
|
20
|
-
* ## The derivation law (per artifact country)
|
|
21
|
-
*
|
|
22
|
-
* - **GB** — the OUTWARD code: the compact (whitespace-stripped) form minus its trailing 3 unit
|
|
23
|
-
* characters ("SW1A 2AA" → "SW1A", "BT9 5GS" → "BT9"). Never a greedy regex — the unit is
|
|
24
|
-
* exactly 3 chars in the compact form, and `outwardOf` in `mailwoman/gazetteer-pipeline/
|
|
25
|
-
* postcode-prefix.ts` is the build-side twin of this law.
|
|
26
|
-
* - **US** — the 3-digit section ("94043" → "940"). M-3 measured 3 digits as the first length
|
|
27
|
-
* where the ZIP prefix stops being free (145 km p95 median); a 5-digit artifact would be the
|
|
28
|
-
* full code the gazetteer already carries. No US artifact ships yet (B3-4 needs a ZCTA
|
|
29
|
-
* acquisition) — the branch exists so the law is tested before the data.
|
|
30
|
-
* - **Anything else** — abstain (null).
|
|
31
|
-
*
|
|
32
|
-
* ## The probe's country restriction
|
|
33
|
-
*
|
|
34
|
-
* The index is country-specific EVIDENCE: it is only probed when its `country` matches the
|
|
35
|
-
* query's country scope (or the scope is absent). A GB index must not speak under a US scope —
|
|
36
|
-
* the pipeline passes the locale's index, and the walk's country filter is the caller's declared
|
|
37
|
-
* universe.
|
|
38
|
-
*
|
|
39
|
-
* ## Metadata contract (what the node carries afterward)
|
|
40
|
-
*
|
|
41
|
-
* - `postcode_prefix` — the prefix string that resolved it.
|
|
42
|
-
* - `postcode_prefix_ancestors` — the ancestry the prefix asserts (coarsest-first; GB outward
|
|
43
|
-
* nodes carry country → constituent-country → district).
|
|
44
|
-
* - `postcode_prefix_radius_p95_km` — the artifact's measured p95 radius, when the node carries a
|
|
45
|
-
* coordinate (M-3's receipt: a 1-digit US band and a GB outward code differ by 200×; never read
|
|
46
|
-
* a coordinate without its radius).
|
|
47
|
-
* - `coordinate_source: "postcode_prefix"` — ONLY when the node actually carries a coordinate.
|
|
48
|
-
* The ancestry-only tier (NI's 80 BT districts) stays coordinate-free — the meaning-of-zero
|
|
49
|
-
* rule, and the half that decides B3-3: inventing a BT centroid would reproduce the `BT3 9QQ` →
|
|
50
|
-
* Sheffield defect #1480 just fixed.
|
|
51
|
-
*
|
|
52
|
-
* **D-rule: opt-in behind `ResolveOpts.postcodePrefixPrior`, default-OFF** (the PCN1 posture:
|
|
53
|
-
* data + loader + offline probe, no decode wiring; the header ships without `delta` until a
|
|
54
|
-
* calibration measures one). Bars: B3-2 (≥60% of held-out units within 10 km, zero worse than the
|
|
55
|
-
* abstention arm), B3-3 (NI ≥95% country scope GB + NIR ancestry + correct district named, 0%
|
|
56
|
-
* coordinate), B3-5 (structural — no model inputs touched).
|
|
57
5
|
*/
|
|
58
6
|
import type { PostcodePrefixIndexLike, PostcodePrefixNode, ResolvedPlace } from "@mailwoman/core/resolver";
|
|
59
7
|
/**
|
|
60
|
-
*
|
|
61
|
-
*
|
|
62
|
-
*
|
|
63
|
-
*
|
|
64
|
-
*
|
|
8
|
+
* Describes a resolved place whose coordinate may be absent, such as a
|
|
9
|
+
* postcode-prefix hit with ancestry only.
|
|
10
|
+
*
|
|
11
|
+
* A missing coordinate stays `undefined`.
|
|
12
|
+
* An invented centroid would place the postcode at a point the data does not support.
|
|
65
13
|
*/
|
|
66
14
|
export type CoordinateOptionalPlace = Omit<ResolvedPlace, "lat" | "lon"> & {
|
|
67
15
|
lat?: number;
|
|
68
16
|
lon?: number;
|
|
69
17
|
};
|
|
70
18
|
/**
|
|
71
|
-
*
|
|
72
|
-
*
|
|
19
|
+
* Derives the key a postcode prefix index uses for `code`: the GB outward code
|
|
20
|
+
* or the US three-digit prefix.
|
|
21
|
+
*
|
|
22
|
+
* @returns `null` for a country with no rule or a code too short to contain a prefix.
|
|
73
23
|
*/
|
|
74
24
|
export declare function derivePostcodePrefix(code: string, country?: string): string | null;
|
|
75
25
|
/**
|
|
@@ -77,25 +27,24 @@ export declare function derivePostcodePrefix(code: string, country?: string): st
|
|
|
77
27
|
*/
|
|
78
28
|
export interface PostcodePrefixProbeResult {
|
|
79
29
|
/**
|
|
80
|
-
* The prefix
|
|
30
|
+
* The prefix derived from the code that matched an index entry.
|
|
81
31
|
*/
|
|
82
32
|
prefix: string;
|
|
83
33
|
/**
|
|
84
|
-
* The index node
|
|
34
|
+
* The matched index node.
|
|
35
|
+
*
|
|
36
|
+
* Its ancestors and optional centroid and radius supply the prior.
|
|
85
37
|
*/
|
|
86
38
|
node: PostcodePrefixNode;
|
|
87
39
|
}
|
|
88
40
|
/**
|
|
89
|
-
*
|
|
90
|
-
*
|
|
91
|
-
* abstain — never throws, never guesses.
|
|
41
|
+
* Looks up `code`'s prefix in the index, returning `null` when the query's country
|
|
42
|
+
* differs from the index's or no indexed prefix can be derived.
|
|
92
43
|
*/
|
|
93
44
|
export declare function probePostcodePrefix(code: string, index: PostcodePrefixIndexLike, queryCountry?: string): PostcodePrefixProbeResult | null;
|
|
94
45
|
/**
|
|
95
|
-
*
|
|
96
|
-
*
|
|
97
|
-
* present ONLY when the node carries one, so an ancestry-only hit stays coordinate-free by construction (B3-3's 0%
|
|
98
|
-
* half).
|
|
46
|
+
* Builds the synthetic `postalcode` place for a prefix hit, with `id: 0` because it is not a gazetteer row.
|
|
47
|
+
* It includes a coordinate only when the prefix node has one.
|
|
99
48
|
*/
|
|
100
49
|
export declare function postcodePrefixResolvedPlace(prefix: string, node: PostcodePrefixNode, index: PostcodePrefixIndexLike): CoordinateOptionalPlace;
|
|
101
50
|
//# sourceMappingURL=prefix.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"prefix.d.ts","sourceRoot":"","sources":["../../lib/postcode/prefix.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"prefix.d.ts","sourceRoot":"","sources":["../../lib/postcode/prefix.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,uBAAuB,EAAE,kBAAkB,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAA;AAE1G;;;;;;GAMG;AACH,MAAM,MAAM,uBAAuB,GAAG,IAAI,CAAC,aAAa,EAAE,KAAK,GAAG,KAAK,CAAC,GAAG;IAAE,GAAG,CAAC,EAAE,MAAM,CAAC;IAAC,GAAG,CAAC,EAAE,MAAM,CAAA;CAAE,CAAA;AAMzG;;;;;GAKG;AACH,wBAAgB,oBAAoB,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAclF;AAED;;GAEG;AACH,MAAM,WAAW,yBAAyB;IACzC;;OAEG;IACH,MAAM,EAAE,MAAM,CAAA;IAEd;;;;OAIG;IACH,IAAI,EAAE,kBAAkB,CAAA;CACxB;AAED;;;GAGG;AACH,wBAAgB,mBAAmB,CAClC,IAAI,EAAE,MAAM,EACZ,KAAK,EAAE,uBAAuB,EAC9B,YAAY,CAAC,EAAE,MAAM,GACnB,yBAAyB,GAAG,IAAI,CAYlC;AAED;;;GAGG;AACH,wBAAgB,2BAA2B,CAC1C,MAAM,EAAE,MAAM,EACd,IAAI,EAAE,kBAAkB,EACxB,KAAK,EAAE,uBAAuB,GAC5B,uBAAuB,CAWzB"}
|