@chofter/geo 0.2.0 → 0.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.
package/README.md CHANGED
@@ -21,9 +21,9 @@ import { createGeo } from "@chofter/geo";
21
21
 
22
22
  const geo = createGeo();
23
23
  const places = await geo.search("IE", "dub", { limit: 5 });
24
- // [{ id: 2964574, name: "Dublin", region: "Leinster", country: "Ireland",
24
+ // [{ id: 2964574, name: "Dublin", region: "County Dublin", country: "Ireland",
25
25
  // countryCode: "IE", lat: 53.3331, lng: -6.2489, population: 1024027,
26
- // label: "Dublin, Leinster, Ireland" }, ...]
26
+ // label: "Dublin, County Dublin, Ireland" }, ...]
27
27
  ```
28
28
 
29
29
  Without a bundler, import it straight from the CDN:
@@ -74,11 +74,13 @@ Returns an object with:
74
74
  Munich) starts with the query, or has a later word that does. Case, accents
75
75
  and punctuation are ignored. Exact matches come first, then larger places.
76
76
  An empty query returns the largest places.
77
- - `countries()`: `[{ code, name, count }]` for a country picker.
77
+ - `countries()`: `[{ code, name, count, altNames? }]` for a country picker.
78
78
  - `preload(countryCode)`: fetches the country's index early, for example when
79
79
  the user picks a country.
80
80
 
81
- `countryCode` is an ISO 3166-1 alpha-2 code such as `"US"` or `"ie"`.
81
+ `countryCode` is an ISO 3166-1 alpha-2 code such as `"US"` or `"ie"`, or for the
82
+ UK one of its nations (`"GB-ENG"`, `"GB-SCT"`, `"GB-WLS"`, `"GB-NIR"`) or `"GB"`
83
+ for all four.
82
84
  Results include `matched` when the query matched an alternate name,
83
85
  `district` when another place in the country has the same name and region, and
84
86
  `localName` when the country's own language has a different name for the place.
@@ -99,37 +101,36 @@ await geo.search("IE", "dub", { limit: 3 });
99
101
  {
100
102
  "id": 2964574,
101
103
  "name": "Dublin",
102
- "region": "Leinster",
104
+ "region": "County Dublin",
103
105
  "country": "Ireland",
104
106
  "countryCode": "IE",
105
107
  "lat": 53.3331,
106
108
  "lng": -6.2489,
107
109
  "population": 1024027,
108
- "label": "Dublin, Leinster, Ireland"
110
+ "label": "Dublin, County Dublin, Ireland"
109
111
  },
110
112
  {
111
113
  "id": 2964690,
112
114
  "name": "Douglas",
113
- "region": "Munster",
115
+ "region": "County Cork",
114
116
  "country": "Ireland",
115
117
  "countryCode": "IE",
116
118
  "lat": 51.8744,
117
119
  "lng": -8.435,
118
120
  "population": 26883,
119
- "label": "Douglas, Munster, Ireland",
121
+ "label": "Douglas, County Cork, Ireland",
120
122
  "matched": "Dúbhglas"
121
123
  },
122
124
  {
123
125
  "id": 2966131,
124
126
  "name": "Blackrock",
125
- "region": "Leinster",
127
+ "region": "County Dublin",
126
128
  "country": "Ireland",
127
129
  "countryCode": "IE",
128
130
  "lat": 53.3015,
129
131
  "lng": -6.1778,
130
132
  "population": 8821,
131
- "label": "Blackrock, Dún Laoghaire-Rathdown, Leinster, Ireland",
132
- "district": "Dún Laoghaire-Rathdown",
133
+ "label": "Blackrock, County Dublin, Ireland",
133
134
  "matched": "Carraig Dubh"
134
135
  }
135
136
  ]
@@ -137,8 +138,7 @@ await geo.search("IE", "dub", { limit: 3 });
137
138
 
138
139
  `id` is the GeoNames id. `label` is ready to display. Douglas and Blackrock are
139
140
  found by their Irish names, so `matched` says which name was hit. You might show
140
- it as "Douglas (Dúbhglas)". Blackrock has a `district` because there is another
141
- Blackrock in Leinster (see below).
141
+ it as "Douglas (Dúbhglas)".
142
142
 
143
143
  ### Local spellings and other scripts
144
144
 
@@ -203,7 +203,6 @@ includes it:
203
203
 
204
204
  ```js
205
205
  (await geo.search("US", "plantation", { limit: 3 })).map((p) => p.label);
206
- (await geo.search("IE", "blackrock", { limit: 2 })).map((p) => p.label);
207
206
  ```
208
207
 
209
208
  ```json
@@ -212,15 +211,32 @@ includes it:
212
211
  "Plantation, Sarasota County, Florida, United States",
213
212
  "Plantation, Monroe County, Florida, United States"
214
213
  ]
215
- [
216
- "Blackrock, Dún Laoghaire-Rathdown, Leinster, Ireland",
217
- "Blackrock, Louth, Leinster, Ireland"
218
- ]
219
214
  ```
220
215
 
221
216
  So `label` tells namesakes apart in nearly every case. A few small places share
222
217
  their name, region and district too; use `lat` and `lng` (or `id`) for those.
223
218
 
219
+ ### Ireland and the UK
220
+
221
+ In Ireland the region is the county. The UK is four countries, England,
222
+ Scotland, Wales and Northern Ireland, with codes `GB-ENG`, `GB-SCT`, `GB-WLS`
223
+ and `GB-NIR`, and the region is the council area. Searching `"GB"`, which is
224
+ what browsers and IP lookups report, covers all four, and each result says
225
+ which nation it is in:
226
+
227
+ ```js
228
+ (await geo.search("IE", "blackrock", { limit: 2 })).map((p) => p.label);
229
+ (await geo.search("GB", "newport", { limit: 2 })).map((p) => [p.label, p.countryCode]);
230
+ ```
231
+
232
+ ```json
233
+ ["Blackrock, County Dublin, Ireland", "Blackrock, County Louth, Ireland"]
234
+ [
235
+ ["Newport, Newport, Wales", "GB-WLS"],
236
+ ["Newport, Isle of Wight, England", "GB-ENG"]
237
+ ]
238
+ ```
239
+
224
240
  ### Ranking: exact names, then prefixes and later words
225
241
 
226
242
  ```js
@@ -269,11 +285,15 @@ await geo.countries();
269
285
  { "code": "AF", "name": "Afghanistan", "count": 319 },
270
286
  { "code": "AX", "name": "Aland Islands", "count": 21 },
271
287
  { "code": "AL", "name": "Albania", "count": 418 },
272
- // … 246 countries in all, sorted by name
288
+ // …
289
+ { "code": "GB-SCT", "name": "Scotland", "count": 667, "altNames": ["United Kingdom", "UK"] },
290
+ // … 249 in all, sorted by name
273
291
  ]
274
292
  ```
275
293
 
276
- `count` is the number of places in that country.
294
+ `count` is the number of places in that country. The UK's four nations are
295
+ listed instead of the UK itself, each with `altNames` "United Kingdom" and "UK", so
296
+ a picker that matches those can offer all four.
277
297
 
278
298
  ### No match and errors
279
299
 
package/dist/geo.d.ts CHANGED
@@ -2,7 +2,10 @@ export interface Place {
2
2
  /** GeoNames id. */
3
3
  id: number;
4
4
  name: string;
5
- /** First-level admin division (state, province, county), may be empty. */
5
+ /**
6
+ * First-level admin division (state, province), may be empty. In Ireland it
7
+ * is the county, and in the UK the council area.
8
+ */
6
9
  region: string;
7
10
  /**
8
11
  * Second-level admin division (county, district), set only when another place
@@ -16,6 +19,10 @@ export interface Place {
16
19
  */
17
20
  localName?: string;
18
21
  country: string;
22
+ /**
23
+ * ISO 3166-1 alpha-2, except for the UK's nations, which use ISO 3166-2:
24
+ * "GB-ENG", "GB-SCT", "GB-WLS" and "GB-NIR" (also for a search of "GB").
25
+ */
19
26
  countryCode: string;
20
27
  lat: number;
21
28
  lng: number;
@@ -30,6 +37,8 @@ export interface Country {
30
37
  code: string;
31
38
  name: string;
32
39
  count: number;
40
+ /** Other names to match in a country picker, such as "United Kingdom" and "UK" for England. */
41
+ altNames?: string[];
33
42
  }
34
43
 
35
44
  export interface GeoOptions {
@@ -1,4 +1,4 @@
1
- // @chofter/geo 0.2.0 · MIT · https://geo.chofter.com · data © GeoNames (CC BY 4.0)
1
+ // @chofter/geo 0.3.0 · MIT · https://geo.chofter.com · data © GeoNames (CC BY 4.0)
2
2
  (function () {
3
3
  "use strict";
4
4
  // Client for the static place-search files. No dependencies; works in browsers,
@@ -146,21 +146,32 @@ function createGeo(options = {}) {
146
146
  count: index.count,
147
147
  prefixFiles: new Set(index.shards),
148
148
  topPlaces: prepare(index.top),
149
+ // Set for a code covering several countries ("GB" for the UK's four
150
+ // nations): each region's country code, and each code's name.
151
+ parts: index.parts,
152
+ regionParts: index.regionParts,
149
153
  }));
150
154
  }
151
155
 
156
+ /** The country a place belongs to: its own part of `country`, if it has parts. */
157
+ function countryOf(country, region) {
158
+ const partCode = country.regionParts && country.regionParts[region];
159
+ return partCode ? { code: partCode, name: country.parts[partCode] } : country;
160
+ }
161
+
152
162
  function toResult(country, place, nameIndex) {
153
163
  const [name, region, lat, lng, population, id, , district, localName] = place.record;
164
+ const { code, name: countryName } = countryOf(country, region);
154
165
  const result = {
155
166
  id,
156
167
  name,
157
168
  region,
158
- country: country.name,
159
- countryCode: country.code,
169
+ country: countryName,
170
+ countryCode: code,
160
171
  lat,
161
172
  lng,
162
173
  population,
163
- label: [name, district, region, country.name].filter(Boolean).join(", "),
174
+ label: [name, district, region, countryName].filter(Boolean).join(", "),
164
175
  };
165
176
  if (district) {
166
177
  result.district = district;
@@ -217,10 +228,12 @@ function createGeo(options = {}) {
217
228
  return matches.slice(0, limit).map((match) => toResult(country, match.place, match.nameIndex));
218
229
  }
219
230
 
220
- /** Countries that have data: [{ code, name, count }]. */
231
+ /** Countries that have data: [{ code, name, count, altNames? }]. */
221
232
  function countries() {
222
233
  return load("countries.json", (rows) =>
223
- rows.map(([code, name, count]) => ({ code, name, count })),
234
+ rows.map(([code, name, count, altNames]) =>
235
+ altNames ? { code, name, count, altNames } : { code, name, count },
236
+ ),
224
237
  );
225
238
  }
226
239
 
@@ -1,2 +1,2 @@
1
- // @chofter/geo 0.2.0 · MIT · https://geo.chofter.com · data © GeoNames (CC BY 4.0)
2
- (()=>{var x={ß:"ss",æ:"ae",œ:"oe",ø:"o",đ:"d",ð:"d",þ:"th",ł:"l",ı:"i",ħ:"h"};function M(o){return String(o).toLowerCase().normalize("NFD").replace(/\p{M}+/gu,"").replace(/[ßæœøđðþłıħ]/g,(e)=>x[e]).replace(/['’‘ʼ`]/g,"").replace(/[^\p{L}\p{N}]+/gu," ").trim()}function P(o,e){return Array.from(o).slice(0,e).join("")}function U(o){let e="";for(let r of o)if(/[a-z0-9]/.test(r))e+=r;else if(r===" ")e+="-";else e+="~"+r.codePointAt(0).toString(16);return e}var j=0,E=4,I=6,C=8,O=0,z=1,d=-1;function F(o){return o.map((e)=>{let r=[e[j],...e[I]||[]];if(e[C])r.push(e[C]);return{record:e,names:r,searchKeys:r.map(M)}})}function D(o,e){if(o===e)return O;if(o.startsWith(e)||o.includes(" "+e))return z;return d}function R(o,e){let r=d,f=0;return o.searchKeys.forEach((p,g)=>{let u=D(p,e);if(u!==d&&(r===d||u<r))r=u,f=g}),{tier:r,nameIndex:f}}var H="https://geo.chofter.com/v1",y=10;function w(o={}){let e=String(o.baseUrl||H).replace(/\/+$/,""),r=o.fetch||globalThis.fetch.bind(globalThis),f=new Map;function p(t,n){let a=f.get(t);if(!a)a=r(`${e}/${t}`).then((c)=>{if(!c.ok)throw Error(`geo: ${t} returned ${c.status}`);return c.json()}).then(n),a.catch(()=>f.delete(t)),f.set(t,a);return a}function g(t){return p(`${t}/_.json`,(n)=>({code:t.toUpperCase(),name:n.name,depth:n.depth,count:n.count,prefixFiles:new Set(n.shards),topPlaces:F(n.top)}))}function u(t,n,a){let[c,s,h,A,m,i,,l,L]=n.record,T={id:i,name:c,region:s,country:t.name,countryCode:t.code,lat:h,lng:A,population:m,label:[c,l,s,t.name].filter(Boolean).join(", ")};if(l)T.district=l;if(L)T.localName=L;if(a>0)T.matched=n.names[a];return T}function N(t,n){let a=Math.min(t.depth,Array.from(n).length);for(let c=a;c>0;c--){let s=U(P(n,c));if(t.prefixFiles.has(s))return s}return null}async function _(t,n,a={}){let c=a.limit||y,s=await g(String(t).toLowerCase()),h=M(n||"");if(!h)return s.topPlaces.slice(0,c).map((i)=>u(s,i,0));let A=s.topPlaces;if(s.depth>0){let i=N(s,h);if(i===null)return[];A=await p(`${s.code.toLowerCase()}/${i}.json`,F)}let m=[];for(let i of A){let{tier:l,nameIndex:L}=R(i,h);if(l!==d)m.push({place:i,tier:l,nameIndex:L})}return m.sort((i,l)=>i.tier-l.tier||l.place.record[E]-i.place.record[E]),m.slice(0,c).map((i)=>u(s,i.place,i.nameIndex))}function S(){return p("countries.json",(t)=>t.map(([n,a,c])=>({code:n,name:a,count:c})))}function b(t){return g(String(t).toLowerCase()).then(()=>{return})}return{search:_,countries:S,preload:b}}globalThis.choftergeo={createGeo:w};})();
1
+ // @chofter/geo 0.3.0 · MIT · https://geo.chofter.com · data © GeoNames (CC BY 4.0)
2
+ (()=>{var j={ß:"ss",æ:"ae",œ:"oe",ø:"o",đ:"d",ð:"d",þ:"th",ł:"l",ı:"i",ħ:"h"};function w(c){return String(c).toLowerCase().normalize("NFD").replace(/\p{M}+/gu,"").replace(/[ßæœøđðþłıħ]/g,(e)=>j[e]).replace(/['’‘ʼ`]/g,"").replace(/[^\p{L}\p{N}]+/gu," ").trim()}function I(c,e){return Array.from(c).slice(0,e).join("")}function x(c){let e="";for(let s of c)if(/[a-z0-9]/.test(s))e+=s;else if(s===" ")e+="-";else e+="~"+s.codePointAt(0).toString(16);return e}var z=0,F=4,D=6,M=8,R=0,y=1,d=-1;function P(c){return c.map((e)=>{let s=[e[z],...e[D]||[]];if(e[M])s.push(e[M]);return{record:e,names:s,searchKeys:s.map(w)}})}function H(c,e){if(c===e)return R;if(c.startsWith(e)||c.includes(" "+e))return y;return d}function B(c,e){let s=d,f=0;return c.searchKeys.forEach((u,g)=>{let p=H(u,e);if(p!==d&&(s===d||p<s))s=p,f=g}),{tier:s,nameIndex:f}}var G="https://geo.chofter.com/v1",X=10;function N(c={}){let e=String(c.baseUrl||G).replace(/\/+$/,""),s=c.fetch||globalThis.fetch.bind(globalThis),f=new Map;function u(t,n){let r=f.get(t);if(!r)r=s(`${e}/${t}`).then((i)=>{if(!i.ok)throw Error(`geo: ${t} returned ${i.status}`);return i.json()}).then(n),r.catch(()=>f.delete(t)),f.set(t,r);return r}function g(t){return u(`${t}/_.json`,(n)=>({code:t.toUpperCase(),name:n.name,depth:n.depth,count:n.count,prefixFiles:new Set(n.shards),topPlaces:P(n.top),parts:n.parts,regionParts:n.regionParts}))}function p(t,n){let r=t.regionParts&&t.regionParts[n];return r?{code:r,name:t.parts[r]}:t}function C(t,n,r){let[i,o,h,A,m,a,,l,L]=n.record,{code:U,name:E}=p(t,o),T={id:a,name:i,region:o,country:E,countryCode:U,lat:h,lng:A,population:m,label:[i,l,o,E].filter(Boolean).join(", ")};if(l)T.district=l;if(L)T.localName=L;if(r>0)T.matched=n.names[r];return T}function _(t,n){let r=Math.min(t.depth,Array.from(n).length);for(let i=r;i>0;i--){let o=x(I(n,i));if(t.prefixFiles.has(o))return o}return null}async function S(t,n,r={}){let i=r.limit||X,o=await g(String(t).toLowerCase()),h=w(n||"");if(!h)return o.topPlaces.slice(0,i).map((a)=>C(o,a,0));let A=o.topPlaces;if(o.depth>0){let a=_(o,h);if(a===null)return[];A=await u(`${o.code.toLowerCase()}/${a}.json`,P)}let m=[];for(let a of A){let{tier:l,nameIndex:L}=B(a,h);if(l!==d)m.push({place:a,tier:l,nameIndex:L})}return m.sort((a,l)=>a.tier-l.tier||l.place.record[F]-a.place.record[F]),m.slice(0,i).map((a)=>C(o,a.place,a.nameIndex))}function b(){return u("countries.json",(t)=>t.map(([n,r,i,o])=>o?{code:n,name:r,count:i,altNames:o}:{code:n,name:r,count:i}))}function O(t){return g(String(t).toLowerCase()).then(()=>{return})}return{search:S,countries:b,preload:O}}globalThis.choftergeo={createGeo:N};})();
package/dist/geo.js CHANGED
@@ -1,4 +1,4 @@
1
- // @chofter/geo 0.2.0 · MIT · https://geo.chofter.com · data © GeoNames (CC BY 4.0)
1
+ // @chofter/geo 0.3.0 · MIT · https://geo.chofter.com · data © GeoNames (CC BY 4.0)
2
2
  // Client for the static place-search files. No dependencies; works in browsers,
3
3
  // Node 18+, Workers and React Native.
4
4
  //
@@ -144,21 +144,32 @@ export function createGeo(options = {}) {
144
144
  count: index.count,
145
145
  prefixFiles: new Set(index.shards),
146
146
  topPlaces: prepare(index.top),
147
+ // Set for a code covering several countries ("GB" for the UK's four
148
+ // nations): each region's country code, and each code's name.
149
+ parts: index.parts,
150
+ regionParts: index.regionParts,
147
151
  }));
148
152
  }
149
153
 
154
+ /** The country a place belongs to: its own part of `country`, if it has parts. */
155
+ function countryOf(country, region) {
156
+ const partCode = country.regionParts && country.regionParts[region];
157
+ return partCode ? { code: partCode, name: country.parts[partCode] } : country;
158
+ }
159
+
150
160
  function toResult(country, place, nameIndex) {
151
161
  const [name, region, lat, lng, population, id, , district, localName] = place.record;
162
+ const { code, name: countryName } = countryOf(country, region);
152
163
  const result = {
153
164
  id,
154
165
  name,
155
166
  region,
156
- country: country.name,
157
- countryCode: country.code,
167
+ country: countryName,
168
+ countryCode: code,
158
169
  lat,
159
170
  lng,
160
171
  population,
161
- label: [name, district, region, country.name].filter(Boolean).join(", "),
172
+ label: [name, district, region, countryName].filter(Boolean).join(", "),
162
173
  };
163
174
  if (district) {
164
175
  result.district = district;
@@ -215,10 +226,12 @@ export function createGeo(options = {}) {
215
226
  return matches.slice(0, limit).map((match) => toResult(country, match.place, match.nameIndex));
216
227
  }
217
228
 
218
- /** Countries that have data: [{ code, name, count }]. */
229
+ /** Countries that have data: [{ code, name, count, altNames? }]. */
219
230
  function countries() {
220
231
  return load("countries.json", (rows) =>
221
- rows.map(([code, name, count]) => ({ code, name, count })),
232
+ rows.map(([code, name, count, altNames]) =>
233
+ altNames ? { code, name, count, altNames } : { code, name, count },
234
+ ),
222
235
  );
223
236
  }
224
237
 
package/dist/geo.min.js CHANGED
@@ -1,2 +1,2 @@
1
- // @chofter/geo 0.2.0 · MIT · https://geo.chofter.com · data © GeoNames (CC BY 4.0)
2
- var x={ß:"ss",æ:"ae",œ:"oe",ø:"o",đ:"d",ð:"d",þ:"th",ł:"l",ı:"i",ħ:"h"};function M(o){return String(o).toLowerCase().normalize("NFD").replace(/\p{M}+/gu,"").replace(/[ßæœøđðþłıħ]/g,(e)=>x[e]).replace(/['’‘ʼ`]/g,"").replace(/[^\p{L}\p{N}]+/gu," ").trim()}function P(o,e){return Array.from(o).slice(0,e).join("")}function U(o){let e="";for(let r of o)if(/[a-z0-9]/.test(r))e+=r;else if(r===" ")e+="-";else e+="~"+r.codePointAt(0).toString(16);return e}var b=0,E=4,j=6,C=8,I=0,O=1,h=-1;function F(o){return o.map((e)=>{let r=[e[b],...e[j]||[]];if(e[C])r.push(e[C]);return{record:e,names:r,searchKeys:r.map(M)}})}function z(o,e){if(o===e)return I;if(o.startsWith(e)||o.includes(" "+e))return O;return h}function D(o,e){let r=h,f=0;return o.searchKeys.forEach((p,g)=>{let u=z(p,e);if(u!==h&&(r===h||u<r))r=u,f=g}),{tier:r,nameIndex:f}}var R="https://geo.chofter.com/v1",H=10;function y(o={}){let e=String(o.baseUrl||R).replace(/\/+$/,""),r=o.fetch||globalThis.fetch.bind(globalThis),f=new Map;function p(t,n){let a=f.get(t);if(!a)a=r(`${e}/${t}`).then((c)=>{if(!c.ok)throw Error(`geo: ${t} returned ${c.status}`);return c.json()}).then(n),a.catch(()=>f.delete(t)),f.set(t,a);return a}function g(t){return p(`${t}/_.json`,(n)=>({code:t.toUpperCase(),name:n.name,depth:n.depth,count:n.count,prefixFiles:new Set(n.shards),topPlaces:F(n.top)}))}function u(t,n,a){let[c,s,m,A,d,i,,l,L]=n.record,T={id:i,name:c,region:s,country:t.name,countryCode:t.code,lat:m,lng:A,population:d,label:[c,l,s,t.name].filter(Boolean).join(", ")};if(l)T.district=l;if(L)T.localName=L;if(a>0)T.matched=n.names[a];return T}function w(t,n){let a=Math.min(t.depth,Array.from(n).length);for(let c=a;c>0;c--){let s=U(P(n,c));if(t.prefixFiles.has(s))return s}return null}async function N(t,n,a={}){let c=a.limit||H,s=await g(String(t).toLowerCase()),m=M(n||"");if(!m)return s.topPlaces.slice(0,c).map((i)=>u(s,i,0));let A=s.topPlaces;if(s.depth>0){let i=w(s,m);if(i===null)return[];A=await p(`${s.code.toLowerCase()}/${i}.json`,F)}let d=[];for(let i of A){let{tier:l,nameIndex:L}=D(i,m);if(l!==h)d.push({place:i,tier:l,nameIndex:L})}return d.sort((i,l)=>i.tier-l.tier||l.place.record[E]-i.place.record[E]),d.slice(0,c).map((i)=>u(s,i.place,i.nameIndex))}function _(){return p("countries.json",(t)=>t.map(([n,a,c])=>({code:n,name:a,count:c})))}function S(t){return g(String(t).toLowerCase()).then(()=>{return})}return{search:N,countries:_,preload:S}}export{y as createGeo};
1
+ // @chofter/geo 0.3.0 · MIT · https://geo.chofter.com · data © GeoNames (CC BY 4.0)
2
+ var b={ß:"ss",æ:"ae",œ:"oe",ø:"o",đ:"d",ð:"d",þ:"th",ł:"l",ı:"i",ħ:"h"};function w(c){return String(c).toLowerCase().normalize("NFD").replace(/\p{M}+/gu,"").replace(/[ßæœøđðþłıħ]/g,(e)=>b[e]).replace(/['’‘ʼ`]/g,"").replace(/[^\p{L}\p{N}]+/gu," ").trim()}function j(c,e){return Array.from(c).slice(0,e).join("")}function x(c){let e="";for(let s of c)if(/[a-z0-9]/.test(s))e+=s;else if(s===" ")e+="-";else e+="~"+s.codePointAt(0).toString(16);return e}var I=0,F=4,z=6,M=8,D=0,R=1,d=-1;function P(c){return c.map((e)=>{let s=[e[I],...e[z]||[]];if(e[M])s.push(e[M]);return{record:e,names:s,searchKeys:s.map(w)}})}function y(c,e){if(c===e)return D;if(c.startsWith(e)||c.includes(" "+e))return R;return d}function H(c,e){let s=d,f=0;return c.searchKeys.forEach((u,g)=>{let p=y(u,e);if(p!==d&&(s===d||p<s))s=p,f=g}),{tier:s,nameIndex:f}}var B="https://geo.chofter.com/v1",G=10;function X(c={}){let e=String(c.baseUrl||B).replace(/\/+$/,""),s=c.fetch||globalThis.fetch.bind(globalThis),f=new Map;function u(t,n){let r=f.get(t);if(!r)r=s(`${e}/${t}`).then((i)=>{if(!i.ok)throw Error(`geo: ${t} returned ${i.status}`);return i.json()}).then(n),r.catch(()=>f.delete(t)),f.set(t,r);return r}function g(t){return u(`${t}/_.json`,(n)=>({code:t.toUpperCase(),name:n.name,depth:n.depth,count:n.count,prefixFiles:new Set(n.shards),topPlaces:P(n.top),parts:n.parts,regionParts:n.regionParts}))}function p(t,n){let r=t.regionParts&&t.regionParts[n];return r?{code:r,name:t.parts[r]}:t}function C(t,n,r){let[i,o,m,A,h,a,,l,L]=n.record,{code:U,name:E}=p(t,o),T={id:a,name:i,region:o,country:E,countryCode:U,lat:m,lng:A,population:h,label:[i,l,o,E].filter(Boolean).join(", ")};if(l)T.district=l;if(L)T.localName=L;if(r>0)T.matched=n.names[r];return T}function N(t,n){let r=Math.min(t.depth,Array.from(n).length);for(let i=r;i>0;i--){let o=x(j(n,i));if(t.prefixFiles.has(o))return o}return null}async function _(t,n,r={}){let i=r.limit||G,o=await g(String(t).toLowerCase()),m=w(n||"");if(!m)return o.topPlaces.slice(0,i).map((a)=>C(o,a,0));let A=o.topPlaces;if(o.depth>0){let a=N(o,m);if(a===null)return[];A=await u(`${o.code.toLowerCase()}/${a}.json`,P)}let h=[];for(let a of A){let{tier:l,nameIndex:L}=H(a,m);if(l!==d)h.push({place:a,tier:l,nameIndex:L})}return h.sort((a,l)=>a.tier-l.tier||l.place.record[F]-a.place.record[F]),h.slice(0,i).map((a)=>C(o,a.place,a.nameIndex))}function S(){return u("countries.json",(t)=>t.map(([n,r,i,o])=>o?{code:n,name:r,count:i,altNames:o}:{code:n,name:r,count:i}))}function O(t){return g(String(t).toLowerCase()).then(()=>{return})}return{search:_,countries:S,preload:O}}export{X as createGeo};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@chofter/geo",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "Tiny, dependency-free town and city search over static GeoNames files. No API, no key.",
5
5
  "keywords": [
6
6
  "geonames",