@mccaskie-labs/workbench-places 0.0.0-stage → 0.1.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.
- package/CHANGELOG.md +13 -0
- package/README.md +16 -2
- package/dist/geocode.d.ts +53 -0
- package/dist/geocode.js +76 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +7 -0
- package/dist/normal.d.ts +2 -0
- package/dist/normal.js +28 -0
- package/dist/ph-data.d.ts +4 -0
- package/dist/ph-data.js +1650 -0
- package/dist/ph.d.ts +33 -0
- package/dist/ph.js +89 -0
- package/dist/route.d.ts +14 -0
- package/dist/route.js +45 -0
- package/dist/types.d.ts +35 -0
- package/dist/types.js +4 -0
- package/package.json +51 -4
package/dist/ph.d.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { PSGC_RELEASE } from "./ph-data.ts";
|
|
2
|
+
import type { Place } from "./types.ts";
|
|
3
|
+
export { PSGC_RELEASE };
|
|
4
|
+
/** One barangay, with the town, province and region it is in. */
|
|
5
|
+
export interface Barangay {
|
|
6
|
+
/** Its ten-digit PSGC code. */
|
|
7
|
+
code: string;
|
|
8
|
+
name: string;
|
|
9
|
+
/** The city or municipality, as people write it ("Cebu City", "Laur"). */
|
|
10
|
+
town: string;
|
|
11
|
+
townCode: string;
|
|
12
|
+
/** Whether the town is a city. */
|
|
13
|
+
city: boolean;
|
|
14
|
+
/** The province people say it is in: "Metro Manila" in the capital region, Cebu for Cebu City. */
|
|
15
|
+
province: string;
|
|
16
|
+
region: string;
|
|
17
|
+
regionCode: string;
|
|
18
|
+
}
|
|
19
|
+
/** Every barangay in the list (42,010 in the October 2026 release). */
|
|
20
|
+
export declare function barangays(): readonly Barangay[];
|
|
21
|
+
/** The barangay with this PSGC code, or undefined. */
|
|
22
|
+
export declare function barangay(code: string): Barangay | undefined;
|
|
23
|
+
/** A barangay as a place: "San Isidro, Laur, Nueva Ecija" (Metro Manila's towns name no province). */
|
|
24
|
+
export declare function barangayPlace(b: Barangay): Place;
|
|
25
|
+
/**
|
|
26
|
+
* The barangays matching what someone typed, best first. Every word typed
|
|
27
|
+
* must start one of the barangay's words, its town's or its province's, so
|
|
28
|
+
* "san isidro laur", "lahug cebu" and "teachers" all work. Barangays whose own
|
|
29
|
+
* name holds the words come first, then those found through their town.
|
|
30
|
+
*/
|
|
31
|
+
export declare function searchBarangays(text: string, limit?: number): Place[];
|
|
32
|
+
/** Place search for a project's place route (placesRoute's `lists`): the Philippines' barangays, nothing for other countries. */
|
|
33
|
+
export declare function searchPlaceLists(country: string, text: string, limit: number): Place[];
|
package/dist/ph.js
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
// The Philippines' official place list (the PSA's PSGC): every region,
|
|
2
|
+
// province, city, municipality and barangay, kept in the project itself, so
|
|
3
|
+
// searching it costs nothing and needs no look-up. People here say where they
|
|
4
|
+
// live as their barangay and town ("La Torre, Talavera"), so search starts
|
|
5
|
+
// there: "san isidro laur" finds San Isidro in Laur, Nueva Ecija, and the
|
|
6
|
+
// pick fills in the rest. The list is about 235 KB compressed, so only code
|
|
7
|
+
// that needs it imports this entry ("@mccaskie-labs/workbench-places/ph").
|
|
8
|
+
import { words } from "./normal.js";
|
|
9
|
+
import { PH_PROVINCES, PH_REGIONS, PH_TOWNS, PSGC_RELEASE } from "./ph-data.js";
|
|
10
|
+
export { PSGC_RELEASE };
|
|
11
|
+
let list;
|
|
12
|
+
let byCode;
|
|
13
|
+
function load() {
|
|
14
|
+
if (list)
|
|
15
|
+
return list;
|
|
16
|
+
const out = [];
|
|
17
|
+
for (const [townCode, town, p, r, city, packed] of PH_TOWNS) {
|
|
18
|
+
const province = PH_PROVINCES[p];
|
|
19
|
+
const [regionCode, region] = PH_REGIONS[r];
|
|
20
|
+
const around = [...words(town), ...(city && !/\bcity\b/i.test(town) ? ["city"] : []), ...words(province)];
|
|
21
|
+
for (const item of packed.split("|")) {
|
|
22
|
+
const full = item.startsWith("#");
|
|
23
|
+
const code = full ? item.slice(1, 11) : `${townCode.slice(0, 7)}${item.slice(0, 3)}`;
|
|
24
|
+
const name = full ? item.slice(11) : item.slice(3);
|
|
25
|
+
out.push({ code, name, town, townCode, city: city === 1, province, region, regionCode, own: words(name), around });
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
list = out;
|
|
29
|
+
byCode = new Map(out.map((e) => [e.code, e]));
|
|
30
|
+
return out;
|
|
31
|
+
}
|
|
32
|
+
const strip = ({ own: _o, around: _a, ...b }) => b;
|
|
33
|
+
/** Every barangay in the list (42,010 in the October 2026 release). */
|
|
34
|
+
export function barangays() {
|
|
35
|
+
return load().map(strip);
|
|
36
|
+
}
|
|
37
|
+
/** The barangay with this PSGC code, or undefined. */
|
|
38
|
+
export function barangay(code) {
|
|
39
|
+
load();
|
|
40
|
+
const e = byCode.get(code);
|
|
41
|
+
return e ? strip(e) : undefined;
|
|
42
|
+
}
|
|
43
|
+
/** A barangay as a place: "San Isidro, Laur, Nueva Ecija" (Metro Manila's towns name no province). */
|
|
44
|
+
export function barangayPlace(b) {
|
|
45
|
+
const label = b.province === "Metro Manila" ? `${b.name}, ${b.town}` : `${b.name}, ${b.town}, ${b.province}`;
|
|
46
|
+
return {
|
|
47
|
+
label,
|
|
48
|
+
parts: { suburb: b.name, city: b.town, state: b.province, region: b.region, country: "PH" },
|
|
49
|
+
source: "psgc",
|
|
50
|
+
code: b.code,
|
|
51
|
+
precision: "suburb",
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* The barangays matching what someone typed, best first. Every word typed
|
|
56
|
+
* must start one of the barangay's words, its town's or its province's, so
|
|
57
|
+
* "san isidro laur", "lahug cebu" and "teachers" all work. Barangays whose own
|
|
58
|
+
* name holds the words come first, then those found through their town.
|
|
59
|
+
*/
|
|
60
|
+
export function searchBarangays(text, limit = 8) {
|
|
61
|
+
const typed = words(text);
|
|
62
|
+
if (!typed.length || limit < 1)
|
|
63
|
+
return [];
|
|
64
|
+
const scored = [];
|
|
65
|
+
const starts = (pool, w) => pool.some((p) => p.startsWith(w));
|
|
66
|
+
for (const e of load()) {
|
|
67
|
+
let own = 0;
|
|
68
|
+
let ok = true;
|
|
69
|
+
for (const w of typed) {
|
|
70
|
+
if (starts(e.own, w))
|
|
71
|
+
own++;
|
|
72
|
+
else if (!starts(e.around, w)) {
|
|
73
|
+
ok = false;
|
|
74
|
+
break;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
if (!ok)
|
|
78
|
+
continue;
|
|
79
|
+
// Own words first; then a name typed in full ("lahug", not "lahu"); then shorter names.
|
|
80
|
+
const exact = e.own.join(" ") === typed.slice(0, e.own.length).join(" ") ? 1 : 0;
|
|
81
|
+
scored.push({ e, score: own * 100 + exact * 50 - e.own.length });
|
|
82
|
+
}
|
|
83
|
+
scored.sort((a, b) => b.score - a.score || a.e.name.localeCompare(b.e.name) || a.e.town.localeCompare(b.e.town));
|
|
84
|
+
return scored.slice(0, limit).map(({ e }) => barangayPlace(strip(e)));
|
|
85
|
+
}
|
|
86
|
+
/** Place search for a project's place route (placesRoute's `lists`): the Philippines' barangays, nothing for other countries. */
|
|
87
|
+
export function searchPlaceLists(country, text, limit) {
|
|
88
|
+
return country.toUpperCase() === "PH" ? searchBarangays(text, limit) : [];
|
|
89
|
+
}
|
package/dist/route.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { type Geocoder } from "./geocode.ts";
|
|
2
|
+
import type { Place } from "./types.ts";
|
|
3
|
+
export interface PlacesRouteOptions {
|
|
4
|
+
/** Searches the project's place lists: searchPlaceLists from "@mccaskie-labs/workbench-places/ph". */
|
|
5
|
+
lists?: (country: string, text: string, limit: number) => Place[];
|
|
6
|
+
/** The Gateway's address look-up (createGeocoder), for full address suggestions. */
|
|
7
|
+
geocoder?: Geocoder;
|
|
8
|
+
/** The countries this project takes addresses in; any other is refused. */
|
|
9
|
+
countries: readonly string[];
|
|
10
|
+
/** X-Workbench-Job for look-ups (forms.address by default). */
|
|
11
|
+
job?: string;
|
|
12
|
+
}
|
|
13
|
+
/** Answers one GET from a page's address box. */
|
|
14
|
+
export declare function placesRoute(request: Request, o: PlacesRouteOptions): Promise<Response>;
|
package/dist/route.js
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
// The project's own place route, which its pages call as someone types:
|
|
2
|
+
// GET <route>?q=san+isidro+laur&country=PH&kind=place
|
|
3
|
+
// GET <route>?q=12+saltbush&country=AU&kind=address&session=<per box>
|
|
4
|
+
// `place` searches the project's own place lists (free, no look-up); `address`
|
|
5
|
+
// asks the Gateway's address look-up (the project's search budget). The
|
|
6
|
+
// answer is { results: Place[] }, or { results: [], unavailable: true } when
|
|
7
|
+
// a look-up can't be made, so the page offers "Type it in instead" and
|
|
8
|
+
// nobody is stuck. The Gateway key stays in the Worker.
|
|
9
|
+
import { GeocodeError } from "./geocode.js";
|
|
10
|
+
const MAX_TEXT = 200;
|
|
11
|
+
const LIMIT = 8;
|
|
12
|
+
const answer = (body, status = 200) => new Response(JSON.stringify(body), { status, headers: { "Content-Type": "application/json", "Cache-Control": "no-store", "X-Content-Type-Options": "nosniff" } });
|
|
13
|
+
/** Answers one GET from a page's address box. */
|
|
14
|
+
export async function placesRoute(request, o) {
|
|
15
|
+
if (request.method !== "GET")
|
|
16
|
+
return answer({ error: "use GET" }, 405);
|
|
17
|
+
const url = new URL(request.url);
|
|
18
|
+
const q = (url.searchParams.get("q") ?? "").trim().replace(/\s+/g, " ");
|
|
19
|
+
const country = (url.searchParams.get("country") ?? "").toUpperCase();
|
|
20
|
+
const kind = url.searchParams.get("kind") === "address" ? "address" : "place";
|
|
21
|
+
const allowed = o.countries.map((c) => c.toUpperCase());
|
|
22
|
+
if (!/^[A-Z]{2}$/.test(country) || !allowed.includes(country))
|
|
23
|
+
return answer({ error: "country isn't one this project takes" }, 400);
|
|
24
|
+
if (q.length > MAX_TEXT)
|
|
25
|
+
return answer({ error: `q is at most ${MAX_TEXT} characters` }, 400);
|
|
26
|
+
if (kind === "place") {
|
|
27
|
+
if (q.length < 2 || !o.lists)
|
|
28
|
+
return answer({ results: [] });
|
|
29
|
+
return answer({ results: o.lists(country, q, LIMIT) });
|
|
30
|
+
}
|
|
31
|
+
if (q.length < 3)
|
|
32
|
+
return answer({ results: [] });
|
|
33
|
+
if (!o.geocoder)
|
|
34
|
+
return answer({ results: [], unavailable: true });
|
|
35
|
+
const session = url.searchParams.get("session") ?? undefined;
|
|
36
|
+
try {
|
|
37
|
+
const results = await o.geocoder.suggest({ text: q, countries: [country], limit: 5, ...(session && session.length <= 64 ? { session } : {}), ...(o.job ? { job: o.job } : {}) });
|
|
38
|
+
return answer({ results });
|
|
39
|
+
}
|
|
40
|
+
catch (e) {
|
|
41
|
+
// Out of budget, busy or not set up: the person types it in instead.
|
|
42
|
+
console.log(JSON.stringify({ at: "workbench-places", error: "address look-up failed", code: e instanceof GeocodeError ? e.code : "error" }));
|
|
43
|
+
return answer({ results: [], unavailable: true });
|
|
44
|
+
}
|
|
45
|
+
}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/** A country, as its two-letter ISO code ("PH", "AU"). */
|
|
2
|
+
export type CountryCode = string;
|
|
3
|
+
/**
|
|
4
|
+
* An address in parts. Each country names them its own way (ADDRESS_PARTS):
|
|
5
|
+
* in the Philippines `suburb` is the barangay, `city` the city or
|
|
6
|
+
* municipality and `state` the province; in Australia `suburb` is the suburb
|
|
7
|
+
* and `state` NSW, VIC and so on, with `city` left out.
|
|
8
|
+
*/
|
|
9
|
+
export interface AddressParts {
|
|
10
|
+
/** House or unit number, building and street. */
|
|
11
|
+
line1?: string;
|
|
12
|
+
line2?: string;
|
|
13
|
+
suburb?: string;
|
|
14
|
+
city?: string;
|
|
15
|
+
state?: string;
|
|
16
|
+
/** The Philippines' region ("Region III (Central Luzon)"). */
|
|
17
|
+
region?: string;
|
|
18
|
+
postcode?: string;
|
|
19
|
+
country: CountryCode;
|
|
20
|
+
}
|
|
21
|
+
/** How exact a place is: a front door, a street, or only the suburb, town or region around it. */
|
|
22
|
+
export type Precision = "address" | "street" | "suburb" | "city" | "region";
|
|
23
|
+
export interface Place {
|
|
24
|
+
/** The place on one line, as people would write it: "San Isidro, Laur, Nueva Ecija". */
|
|
25
|
+
label: string;
|
|
26
|
+
parts: AddressParts;
|
|
27
|
+
/** Where it came from: "psgc" (the Philippine place list) or "lookup" (the Gateway's address look-up). */
|
|
28
|
+
source: "psgc" | "lookup";
|
|
29
|
+
/** The official code, for a place from a place list: a barangay's ten-digit PSGC code. */
|
|
30
|
+
code?: string;
|
|
31
|
+
/** Where it is, to 6 decimals, when known. The place lists have no points. */
|
|
32
|
+
lat?: number;
|
|
33
|
+
lng?: number;
|
|
34
|
+
precision: Precision;
|
|
35
|
+
}
|
package/dist/types.js
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,53 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mccaskie-labs/workbench-places",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "Places for Workbench apps: the Philippines' official place list (every barangay, from the PSGC), place search that runs in the project for free, each country's address parts, and the client for the Gateway's address look-up. No forms or maps in it, so both can use it.",
|
|
5
|
+
"license": "UNLICENSED",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/mccaskie-labs/workbench.git",
|
|
10
|
+
"directory": "packages/places"
|
|
11
|
+
},
|
|
12
|
+
"engines": {
|
|
13
|
+
"node": ">=24"
|
|
14
|
+
},
|
|
15
|
+
"sideEffects": false,
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"types": "./dist/index.d.ts",
|
|
19
|
+
"default": "./dist/index.js"
|
|
20
|
+
},
|
|
21
|
+
"./ph": {
|
|
22
|
+
"types": "./dist/ph.d.ts",
|
|
23
|
+
"default": "./dist/ph.js"
|
|
24
|
+
},
|
|
25
|
+
"./route": {
|
|
26
|
+
"types": "./dist/route.d.ts",
|
|
27
|
+
"default": "./dist/route.js"
|
|
28
|
+
},
|
|
29
|
+
"./geocode": {
|
|
30
|
+
"types": "./dist/geocode.d.ts",
|
|
31
|
+
"default": "./dist/geocode.js"
|
|
32
|
+
},
|
|
33
|
+
"./package.json": "./package.json"
|
|
34
|
+
},
|
|
35
|
+
"files": [
|
|
36
|
+
"dist",
|
|
37
|
+
"README.md",
|
|
38
|
+
"CHANGELOG.md"
|
|
39
|
+
],
|
|
40
|
+
"publishConfig": {
|
|
41
|
+
"access": "public"
|
|
42
|
+
},
|
|
43
|
+
"scripts": {
|
|
44
|
+
"typecheck": "tsc -p tsconfig.json --noEmit && tsc -p test/tsconfig.json",
|
|
45
|
+
"test": "vitest run",
|
|
46
|
+
"build": "node scripts/build.mjs",
|
|
47
|
+
"prepack": "node scripts/build.mjs"
|
|
48
|
+
},
|
|
49
|
+
"devDependencies": {
|
|
50
|
+
"@types/node": "24.19.2",
|
|
51
|
+
"vitest": "^5.1.0"
|
|
52
|
+
}
|
|
53
|
+
}
|