@chofter/geo 0.1.0 → 0.2.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 +55 -7
- package/dist/geo.d.ts +12 -1
- package/dist/geo.global.js +18 -4
- package/dist/geo.global.min.js +2 -2
- package/dist/geo.js +18 -4
- package/dist/geo.min.js +2 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -79,7 +79,9 @@ Returns an object with:
|
|
|
79
79
|
the user picks a country.
|
|
80
80
|
|
|
81
81
|
`countryCode` is an ISO 3166-1 alpha-2 code such as `"US"` or `"ie"`.
|
|
82
|
-
Results include `matched` when the query matched an alternate name
|
|
82
|
+
Results include `matched` when the query matched an alternate name,
|
|
83
|
+
`district` when another place in the country has the same name and region, and
|
|
84
|
+
`localName` when the country's own language has a different name for the place.
|
|
83
85
|
|
|
84
86
|
## Examples
|
|
85
87
|
|
|
@@ -126,7 +128,8 @@ await geo.search("IE", "dub", { limit: 3 });
|
|
|
126
128
|
"lat": 53.3015,
|
|
127
129
|
"lng": -6.1778,
|
|
128
130
|
"population": 8821,
|
|
129
|
-
"label": "Blackrock, Leinster, Ireland",
|
|
131
|
+
"label": "Blackrock, Dún Laoghaire-Rathdown, Leinster, Ireland",
|
|
132
|
+
"district": "Dún Laoghaire-Rathdown",
|
|
130
133
|
"matched": "Carraig Dubh"
|
|
131
134
|
}
|
|
132
135
|
]
|
|
@@ -134,7 +137,8 @@ await geo.search("IE", "dub", { limit: 3 });
|
|
|
134
137
|
|
|
135
138
|
`id` is the GeoNames id. `label` is ready to display. Douglas and Blackrock are
|
|
136
139
|
found by their Irish names, so `matched` says which name was hit. You might show
|
|
137
|
-
it as "Douglas (Dúbhglas)".
|
|
140
|
+
it as "Douglas (Dúbhglas)". Blackrock has a `district` because there is another
|
|
141
|
+
Blackrock in Leinster (see below).
|
|
138
142
|
|
|
139
143
|
### Local spellings and other scripts
|
|
140
144
|
|
|
@@ -145,16 +149,39 @@ await geo.search("RU", "Москва", { limit: 1 });
|
|
|
145
149
|
|
|
146
150
|
```jsonc
|
|
147
151
|
[
|
|
148
|
-
{ "name": "Munich", "label": "Munich, Bavaria, Germany", "
|
|
152
|
+
{ "name": "Munich", "label": "Munich, Bavaria, Germany", "localName": "München", "matched": "München" /* … */ },
|
|
149
153
|
{ "name": "Münchenbernsdorf", "label": "Münchenbernsdorf, Thuringia, Germany" /* … */ }
|
|
150
154
|
]
|
|
151
155
|
[
|
|
152
|
-
{ "name": "Moscow", "label": "Moscow, Moscow, Russia", "
|
|
156
|
+
{ "name": "Moscow", "label": "Moscow, Moscow, Russia", "localName": "Москва", "matched": "Москва" /* … */ }
|
|
153
157
|
]
|
|
154
158
|
```
|
|
155
159
|
|
|
156
160
|
Accents are optional: `"munchen"` and `"sao pau"` work just as well.
|
|
157
161
|
|
|
162
|
+
### Local names
|
|
163
|
+
|
|
164
|
+
`name` is in English where English has its own name for the place, and
|
|
165
|
+
otherwise in the local spelling in Latin letters. `localName` gives the name in
|
|
166
|
+
the country's own language, whatever you searched for:
|
|
167
|
+
|
|
168
|
+
```js
|
|
169
|
+
(await geo.search("KR", "", { limit: 3 })).map((p) => [p.name, p.localName]);
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
```json
|
|
173
|
+
[
|
|
174
|
+
["Seoul", "서울"],
|
|
175
|
+
["Busan", "부산"],
|
|
176
|
+
["Incheon", "인천"]
|
|
177
|
+
]
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
It is left out when there is nothing to add: when the country's first language
|
|
181
|
+
is English (the US, Ireland, India), when GeoNames has no name in it, or when it
|
|
182
|
+
is the same as `name` (Paris, Berlin). Countries with several languages use the
|
|
183
|
+
first one GeoNames lists, so Geneva's `localName` is German, "Genf".
|
|
184
|
+
|
|
158
185
|
### Same name, different places
|
|
159
186
|
|
|
160
187
|
```js
|
|
@@ -170,8 +197,29 @@ Accents are optional: `"munchen"` and `"sao pau"` work just as well.
|
|
|
170
197
|
]
|
|
171
198
|
```
|
|
172
199
|
|
|
173
|
-
|
|
174
|
-
|
|
200
|
+
Exact matches come first, largest first. When two places share a name and a
|
|
201
|
+
region, they also get a `district` (a county or district), and the label
|
|
202
|
+
includes it:
|
|
203
|
+
|
|
204
|
+
```js
|
|
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
|
+
```
|
|
208
|
+
|
|
209
|
+
```json
|
|
210
|
+
[
|
|
211
|
+
"Plantation, Broward County, Florida, United States",
|
|
212
|
+
"Plantation, Sarasota County, Florida, United States",
|
|
213
|
+
"Plantation, Monroe County, Florida, United States"
|
|
214
|
+
]
|
|
215
|
+
[
|
|
216
|
+
"Blackrock, Dún Laoghaire-Rathdown, Leinster, Ireland",
|
|
217
|
+
"Blackrock, Louth, Leinster, Ireland"
|
|
218
|
+
]
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
So `label` tells namesakes apart in nearly every case. A few small places share
|
|
222
|
+
their name, region and district too; use `lat` and `lng` (or `id`) for those.
|
|
175
223
|
|
|
176
224
|
### Ranking: exact names, then prefixes and later words
|
|
177
225
|
|
package/dist/geo.d.ts
CHANGED
|
@@ -4,12 +4,23 @@ export interface Place {
|
|
|
4
4
|
name: string;
|
|
5
5
|
/** First-level admin division (state, province, county), may be empty. */
|
|
6
6
|
region: string;
|
|
7
|
+
/**
|
|
8
|
+
* Second-level admin division (county, district), set only when another place
|
|
9
|
+
* in the country has the same name and region.
|
|
10
|
+
*/
|
|
11
|
+
district?: string;
|
|
12
|
+
/**
|
|
13
|
+
* The name in the country's own language, such as "서울" for Seoul or "München"
|
|
14
|
+
* for Munich. Absent when that language is English, GeoNames has no name in
|
|
15
|
+
* it, or it is the same as `name`.
|
|
16
|
+
*/
|
|
17
|
+
localName?: string;
|
|
7
18
|
country: string;
|
|
8
19
|
countryCode: string;
|
|
9
20
|
lat: number;
|
|
10
21
|
lng: number;
|
|
11
22
|
population: number;
|
|
12
|
-
/** "Name, Region, Country". */
|
|
23
|
+
/** "Name, Region, Country", or "Name, District, Region, Country" when `district` is set. */
|
|
13
24
|
label: string;
|
|
14
25
|
/** Set when the query matched an alternate name rather than `name`. */
|
|
15
26
|
matched?: string;
|
package/dist/geo.global.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// @chofter/geo 0.
|
|
1
|
+
// @chofter/geo 0.2.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,
|
|
@@ -61,10 +61,15 @@ function fileKey(prefix) {
|
|
|
61
61
|
}
|
|
62
62
|
|
|
63
63
|
// A place in the JSON files is an array:
|
|
64
|
-
// [name, region, lat, lng, population, id, altNames?]
|
|
64
|
+
// [name, region, lat, lng, population, id, altNames?, district?, localName?]
|
|
65
|
+
// `district` is set only when another place in the country has the same name and
|
|
66
|
+
// region. `localName` is the name in the country's own language, when that is not
|
|
67
|
+
// English and differs from `name`. Trailing items are left out; one that is
|
|
68
|
+
// missing before a present one is [] (altNames) or "".
|
|
65
69
|
const NAME = 0;
|
|
66
70
|
const POPULATION = 4;
|
|
67
71
|
const ALT_NAMES = 6;
|
|
72
|
+
const LOCAL_NAME = 8;
|
|
68
73
|
|
|
69
74
|
const EXACT_MATCH = 0;
|
|
70
75
|
const PREFIX_MATCH = 1;
|
|
@@ -74,6 +79,9 @@ const NO_MATCH = -1;
|
|
|
74
79
|
function prepare(records) {
|
|
75
80
|
return records.map((record) => {
|
|
76
81
|
const names = [record[NAME], ...(record[ALT_NAMES] || [])];
|
|
82
|
+
if (record[LOCAL_NAME]) {
|
|
83
|
+
names.push(record[LOCAL_NAME]);
|
|
84
|
+
}
|
|
77
85
|
return { record, names, searchKeys: names.map(normalize) };
|
|
78
86
|
});
|
|
79
87
|
}
|
|
@@ -142,7 +150,7 @@ function createGeo(options = {}) {
|
|
|
142
150
|
}
|
|
143
151
|
|
|
144
152
|
function toResult(country, place, nameIndex) {
|
|
145
|
-
const [name, region, lat, lng, population, id] = place.record;
|
|
153
|
+
const [name, region, lat, lng, population, id, , district, localName] = place.record;
|
|
146
154
|
const result = {
|
|
147
155
|
id,
|
|
148
156
|
name,
|
|
@@ -152,8 +160,14 @@ function createGeo(options = {}) {
|
|
|
152
160
|
lat,
|
|
153
161
|
lng,
|
|
154
162
|
population,
|
|
155
|
-
label: [name, region, country.name].filter(Boolean).join(", "),
|
|
163
|
+
label: [name, district, region, country.name].filter(Boolean).join(", "),
|
|
156
164
|
};
|
|
165
|
+
if (district) {
|
|
166
|
+
result.district = district;
|
|
167
|
+
}
|
|
168
|
+
if (localName) {
|
|
169
|
+
result.localName = localName;
|
|
170
|
+
}
|
|
157
171
|
if (nameIndex > 0) {
|
|
158
172
|
result.matched = place.names[nameIndex];
|
|
159
173
|
}
|
package/dist/geo.global.min.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
// @chofter/geo 0.
|
|
2
|
-
(()=>{var
|
|
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};})();
|
package/dist/geo.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// @chofter/geo 0.
|
|
1
|
+
// @chofter/geo 0.2.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
|
//
|
|
@@ -59,10 +59,15 @@ function fileKey(prefix) {
|
|
|
59
59
|
}
|
|
60
60
|
|
|
61
61
|
// A place in the JSON files is an array:
|
|
62
|
-
// [name, region, lat, lng, population, id, altNames?]
|
|
62
|
+
// [name, region, lat, lng, population, id, altNames?, district?, localName?]
|
|
63
|
+
// `district` is set only when another place in the country has the same name and
|
|
64
|
+
// region. `localName` is the name in the country's own language, when that is not
|
|
65
|
+
// English and differs from `name`. Trailing items are left out; one that is
|
|
66
|
+
// missing before a present one is [] (altNames) or "".
|
|
63
67
|
const NAME = 0;
|
|
64
68
|
const POPULATION = 4;
|
|
65
69
|
const ALT_NAMES = 6;
|
|
70
|
+
const LOCAL_NAME = 8;
|
|
66
71
|
|
|
67
72
|
const EXACT_MATCH = 0;
|
|
68
73
|
const PREFIX_MATCH = 1;
|
|
@@ -72,6 +77,9 @@ const NO_MATCH = -1;
|
|
|
72
77
|
function prepare(records) {
|
|
73
78
|
return records.map((record) => {
|
|
74
79
|
const names = [record[NAME], ...(record[ALT_NAMES] || [])];
|
|
80
|
+
if (record[LOCAL_NAME]) {
|
|
81
|
+
names.push(record[LOCAL_NAME]);
|
|
82
|
+
}
|
|
75
83
|
return { record, names, searchKeys: names.map(normalize) };
|
|
76
84
|
});
|
|
77
85
|
}
|
|
@@ -140,7 +148,7 @@ export function createGeo(options = {}) {
|
|
|
140
148
|
}
|
|
141
149
|
|
|
142
150
|
function toResult(country, place, nameIndex) {
|
|
143
|
-
const [name, region, lat, lng, population, id] = place.record;
|
|
151
|
+
const [name, region, lat, lng, population, id, , district, localName] = place.record;
|
|
144
152
|
const result = {
|
|
145
153
|
id,
|
|
146
154
|
name,
|
|
@@ -150,8 +158,14 @@ export function createGeo(options = {}) {
|
|
|
150
158
|
lat,
|
|
151
159
|
lng,
|
|
152
160
|
population,
|
|
153
|
-
label: [name, region, country.name].filter(Boolean).join(", "),
|
|
161
|
+
label: [name, district, region, country.name].filter(Boolean).join(", "),
|
|
154
162
|
};
|
|
163
|
+
if (district) {
|
|
164
|
+
result.district = district;
|
|
165
|
+
}
|
|
166
|
+
if (localName) {
|
|
167
|
+
result.localName = localName;
|
|
168
|
+
}
|
|
155
169
|
if (nameIndex > 0) {
|
|
156
170
|
result.matched = place.names[nameIndex];
|
|
157
171
|
}
|
package/dist/geo.min.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
// @chofter/geo 0.
|
|
2
|
-
var
|
|
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};
|