@mailwoman/un-locode-lookup 8.0.0 → 8.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/build.ts +8 -1
- package/cli.ts +7 -0
- package/index.ts +22 -18
- package/out/build.d.ts +3 -1
- package/out/build.d.ts.map +1 -1
- package/out/build.js +3 -1
- package/out/build.js.map +1 -1
- package/out/cli.js.map +1 -1
- package/out/index.d.ts +15 -5
- package/out/index.d.ts.map +1 -1
- package/out/index.js +18 -15
- package/out/index.js.map +1 -1
- package/package.json +3 -2
package/build.ts
CHANGED
|
@@ -23,18 +23,23 @@ interface CSVRow {
|
|
|
23
23
|
Coordinates: string
|
|
24
24
|
}
|
|
25
25
|
|
|
26
|
-
/**
|
|
26
|
+
/**
|
|
27
|
+
* Read the code-list CSV at `csvPath` and write the lookup DB to `dbPath`.
|
|
28
|
+
*/
|
|
27
29
|
export function buildUnLocodeDB(csvPath: string, dbPath: string): { rows: number; withCoords: number } {
|
|
28
30
|
const records = parse(readFileSync(csvPath), {
|
|
29
31
|
columns: true,
|
|
30
32
|
skip_empty_lines: true,
|
|
31
33
|
relax_quotes: true,
|
|
32
34
|
}) as CSVRow[]
|
|
35
|
+
|
|
33
36
|
const db = new DatabaseSync(dbPath)
|
|
34
37
|
db.exec("DROP TABLE IF EXISTS un_locode")
|
|
38
|
+
|
|
35
39
|
db.exec(
|
|
36
40
|
"CREATE TABLE un_locode (country TEXT NOT NULL, location TEXT NOT NULL, name TEXT, nameNorm TEXT, lat REAL, lon REAL)"
|
|
37
41
|
)
|
|
42
|
+
|
|
38
43
|
const insert = db.prepare("INSERT INTO un_locode (country, location, name, nameNorm, lat, lon) VALUES (?,?,?,?,?,?)")
|
|
39
44
|
|
|
40
45
|
let withCoords = 0
|
|
@@ -47,9 +52,11 @@ export function buildUnLocodeDB(csvPath: string, dbPath: string): { rows: number
|
|
|
47
52
|
if (coords) {
|
|
48
53
|
withCoords++
|
|
49
54
|
}
|
|
55
|
+
|
|
50
56
|
const name = r.NameWoDiacritics || r.Name || ""
|
|
51
57
|
insert.run(r.Country, r.Location, r.Name || name, foldName(name), coords?.lat ?? null, coords?.lon ?? null)
|
|
52
58
|
}
|
|
59
|
+
|
|
53
60
|
db.exec("COMMIT")
|
|
54
61
|
db.exec("CREATE INDEX idx_locode_name ON un_locode (country, nameNorm)")
|
|
55
62
|
db.exec("CREATE INDEX idx_locode_bbox ON un_locode (lat, lon)")
|
package/cli.ts
CHANGED
|
@@ -35,9 +35,12 @@ const { values, positionals } = parseArgs({
|
|
|
35
35
|
if (positionals[0] === "build") {
|
|
36
36
|
if (!values.csv || !values.out) {
|
|
37
37
|
console.error("Usage: mailwoman-un-locode build --csv <code-list.csv> --out <db>")
|
|
38
|
+
|
|
38
39
|
process.exit(1)
|
|
39
40
|
}
|
|
41
|
+
|
|
40
42
|
const { rows, withCoords } = buildUnLocodeDB(values.csv, values.out)
|
|
43
|
+
|
|
41
44
|
console.error(`built ${values.out} (${rows} rows, ${withCoords} with coordinates)`)
|
|
42
45
|
} else {
|
|
43
46
|
const lat = Number(positionals[0])
|
|
@@ -47,10 +50,14 @@ if (positionals[0] === "build") {
|
|
|
47
50
|
|
|
48
51
|
if (!values.db || (!byName && !byCoord)) {
|
|
49
52
|
console.error("Usage: mailwoman-un-locode --db <db> (--country CC --name NAME | -- <lat> <lon>)")
|
|
53
|
+
|
|
50
54
|
process.exit(1)
|
|
51
55
|
}
|
|
56
|
+
|
|
52
57
|
const lookup = new UnLocodeLookup({ databasePath: values.db })
|
|
53
58
|
const code = byName ? lookup.byName(values.country!, values.name!) : lookup.nearest(lat, lon)
|
|
59
|
+
|
|
54
60
|
console.log(JSON.stringify({ unLocode: code }))
|
|
61
|
+
|
|
55
62
|
lookup.close()
|
|
56
63
|
}
|
package/index.ts
CHANGED
|
@@ -12,18 +12,23 @@
|
|
|
12
12
|
import { DatabaseSync } from "node:sqlite"
|
|
13
13
|
|
|
14
14
|
import type { AnnotationSet, Annotator } from "@mailwoman/annotations"
|
|
15
|
+
import { haversineKm } from "@mailwoman/spatial"
|
|
15
16
|
|
|
16
|
-
/**
|
|
17
|
+
/**
|
|
18
|
+
* Fold a place name to its match key: strip diacritics, lowercase, collapse whitespace.
|
|
19
|
+
*/
|
|
17
20
|
export function foldName(name: string): string {
|
|
18
21
|
return name
|
|
19
22
|
.normalize("NFD")
|
|
20
|
-
.
|
|
23
|
+
.replaceAll(/[̀-ͯ]/g, "")
|
|
21
24
|
.toLowerCase()
|
|
22
25
|
.trim()
|
|
23
|
-
.
|
|
26
|
+
.replaceAll(/\s+/g, " ")
|
|
24
27
|
}
|
|
25
28
|
|
|
26
|
-
/**
|
|
29
|
+
/**
|
|
30
|
+
* Parse a UN/LOCODE coordinate (`"4923N 01522E"`) to decimal degrees, or null if absent/malformed.
|
|
31
|
+
*/
|
|
27
32
|
export function parseUnLocodeCoords(raw: string): { lat: number; lon: number } | null {
|
|
28
33
|
const m = raw.trim().match(/^(\d{2})(\d{2})([NS])\s+(\d{3})(\d{2})([EW])$/)
|
|
29
34
|
|
|
@@ -34,18 +39,9 @@ export function parseUnLocodeCoords(raw: string): { lat: number; lon: number } |
|
|
|
34
39
|
return { lat, lon }
|
|
35
40
|
}
|
|
36
41
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
const dLon = ((bLon - aLon) * Math.PI) / 180
|
|
41
|
-
const s =
|
|
42
|
-
Math.sin(dLat / 2) ** 2 +
|
|
43
|
-
Math.cos((aLat * Math.PI) / 180) * Math.cos((bLat * Math.PI) / 180) * Math.sin(dLon / 2) ** 2
|
|
44
|
-
|
|
45
|
-
return 2 * EARTH_R_KM * Math.asin(Math.sqrt(s))
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
/** A UN/LOCODE lookup over a built `node:sqlite` table. */
|
|
42
|
+
/**
|
|
43
|
+
* A UN/LOCODE lookup over a built `node:sqlite` table.
|
|
44
|
+
*/
|
|
49
45
|
export class UnLocodeLookup {
|
|
50
46
|
#db: DatabaseSync
|
|
51
47
|
#byName: ReturnType<DatabaseSync["prepare"]>
|
|
@@ -53,15 +49,19 @@ export class UnLocodeLookup {
|
|
|
53
49
|
|
|
54
50
|
constructor(opts: { databasePath: string } | { database: DatabaseSync }) {
|
|
55
51
|
this.#db = "database" in opts ? opts.database : new DatabaseSync(opts.databasePath, { readOnly: true })
|
|
52
|
+
|
|
56
53
|
this.#byName = this.#db.prepare(
|
|
57
54
|
"SELECT country, location FROM un_locode WHERE country = ? AND nameNorm = ? LIMIT 1"
|
|
58
55
|
)
|
|
56
|
+
|
|
59
57
|
this.#byBox = this.#db.prepare(
|
|
60
58
|
"SELECT location, country, lat, lon FROM un_locode WHERE lat BETWEEN ? AND ? AND lon BETWEEN ? AND ?"
|
|
61
59
|
)
|
|
62
60
|
}
|
|
63
61
|
|
|
64
|
-
/**
|
|
62
|
+
/**
|
|
63
|
+
* The UN/LOCODE (`"US NYC"`) for a country + place name, or null.
|
|
64
|
+
*/
|
|
65
65
|
byName(country: string, name: string): string | null {
|
|
66
66
|
const row = this.#byName.get(country.toUpperCase(), foldName(name)) as
|
|
67
67
|
| { country: string; location: string }
|
|
@@ -70,16 +70,20 @@ export class UnLocodeLookup {
|
|
|
70
70
|
return row ? `${row.country} ${row.location}` : null
|
|
71
71
|
}
|
|
72
72
|
|
|
73
|
-
/**
|
|
73
|
+
/**
|
|
74
|
+
* The nearest coordinate-bearing UN/LOCODE within `maxKm`, or null.
|
|
75
|
+
*/
|
|
74
76
|
nearest(lat: number, lon: number, maxKm = 25): string | null {
|
|
75
77
|
const dLat = maxKm / 111
|
|
76
78
|
const dLon = maxKm / (111 * Math.max(0.01, Math.cos((lat * Math.PI) / 180)))
|
|
79
|
+
|
|
77
80
|
const rows = this.#byBox.all(lat - dLat, lat + dLat, lon - dLon, lon + dLon) as Array<{
|
|
78
81
|
location: string
|
|
79
82
|
country: string
|
|
80
83
|
lat: number
|
|
81
84
|
lon: number
|
|
82
85
|
}>
|
|
86
|
+
|
|
83
87
|
let best: { code: string; km: number } | null = null
|
|
84
88
|
|
|
85
89
|
for (const r of rows) {
|
package/out/build.d.ts
CHANGED
|
@@ -7,7 +7,9 @@
|
|
|
7
7
|
* columns Change, Country, Location, Name, NameWoDiacritics, Subdivision, Status, Function, Date,
|
|
8
8
|
* IATA, Coordinates, Remarks). One row per assigned location; coordinates parsed where present.
|
|
9
9
|
*/
|
|
10
|
-
/**
|
|
10
|
+
/**
|
|
11
|
+
* Read the code-list CSV at `csvPath` and write the lookup DB to `dbPath`.
|
|
12
|
+
*/
|
|
11
13
|
export declare function buildUnLocodeDB(csvPath: string, dbPath: string): {
|
|
12
14
|
rows: number;
|
|
13
15
|
withCoords: number;
|
package/out/build.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"build.d.ts","sourceRoot":"","sources":["../build.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAiBH
|
|
1
|
+
{"version":3,"file":"build.d.ts","sourceRoot":"","sources":["../build.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAiBH;;GAEG;AACH,wBAAgB,eAAe,CAAC,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,UAAU,EAAE,MAAM,CAAA;CAAE,CAqCrG"}
|
package/out/build.js
CHANGED
|
@@ -11,7 +11,9 @@ import { readFileSync } from "node:fs";
|
|
|
11
11
|
import { DatabaseSync } from "node:sqlite";
|
|
12
12
|
import { parse } from "csv-parse/sync";
|
|
13
13
|
import { foldName, parseUnLocodeCoords } from "./index.js";
|
|
14
|
-
/**
|
|
14
|
+
/**
|
|
15
|
+
* Read the code-list CSV at `csvPath` and write the lookup DB to `dbPath`.
|
|
16
|
+
*/
|
|
15
17
|
export function buildUnLocodeDB(csvPath, dbPath) {
|
|
16
18
|
const records = parse(readFileSync(csvPath), {
|
|
17
19
|
columns: true,
|
package/out/build.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"build.js","sourceRoot":"","sources":["../build.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAA;AACtC,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAA;AAE1C,OAAO,EAAE,KAAK,EAAE,MAAM,gBAAgB,CAAA;AAEtC,OAAO,EAAE,QAAQ,EAAE,mBAAmB,EAAE,MAAM,YAAY,CAAA;AAU1D
|
|
1
|
+
{"version":3,"file":"build.js","sourceRoot":"","sources":["../build.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAA;AACtC,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAA;AAE1C,OAAO,EAAE,KAAK,EAAE,MAAM,gBAAgB,CAAA;AAEtC,OAAO,EAAE,QAAQ,EAAE,mBAAmB,EAAE,MAAM,YAAY,CAAA;AAU1D;;GAEG;AACH,MAAM,UAAU,eAAe,CAAC,OAAe,EAAE,MAAc;IAC9D,MAAM,OAAO,GAAG,KAAK,CAAC,YAAY,CAAC,OAAO,CAAC,EAAE;QAC5C,OAAO,EAAE,IAAI;QACb,gBAAgB,EAAE,IAAI;QACtB,YAAY,EAAE,IAAI;KAClB,CAAa,CAAA;IAEd,MAAM,EAAE,GAAG,IAAI,YAAY,CAAC,MAAM,CAAC,CAAA;IACnC,EAAE,CAAC,IAAI,CAAC,gCAAgC,CAAC,CAAA;IAEzC,EAAE,CAAC,IAAI,CACN,sHAAsH,CACtH,CAAA;IAED,MAAM,MAAM,GAAG,EAAE,CAAC,OAAO,CAAC,0FAA0F,CAAC,CAAA;IAErH,IAAI,UAAU,GAAG,CAAC,CAAA;IAClB,EAAE,CAAC,IAAI,CAAC,OAAO,CAAC,CAAA;IAEhB,KAAK,MAAM,CAAC,IAAI,OAAO,EAAE,CAAC;QACzB,IAAI,CAAC,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,IAAI,CAAC,SAAS,CAAC,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC,CAAC,CAAA;IAE/C,MAAM,CAAC,KAAK,EAAE,CAAA;AACf,CAAC"}
|
package/out/index.d.ts
CHANGED
|
@@ -10,14 +10,20 @@
|
|
|
10
10
|
*/
|
|
11
11
|
import { DatabaseSync } from "node:sqlite";
|
|
12
12
|
import type { Annotator } from "@mailwoman/annotations";
|
|
13
|
-
/**
|
|
13
|
+
/**
|
|
14
|
+
* Fold a place name to its match key: strip diacritics, lowercase, collapse whitespace.
|
|
15
|
+
*/
|
|
14
16
|
export declare function foldName(name: string): string;
|
|
15
|
-
/**
|
|
17
|
+
/**
|
|
18
|
+
* Parse a UN/LOCODE coordinate (`"4923N 01522E"`) to decimal degrees, or null if absent/malformed.
|
|
19
|
+
*/
|
|
16
20
|
export declare function parseUnLocodeCoords(raw: string): {
|
|
17
21
|
lat: number;
|
|
18
22
|
lon: number;
|
|
19
23
|
} | null;
|
|
20
|
-
/**
|
|
24
|
+
/**
|
|
25
|
+
* A UN/LOCODE lookup over a built `node:sqlite` table.
|
|
26
|
+
*/
|
|
21
27
|
export declare class UnLocodeLookup {
|
|
22
28
|
#private;
|
|
23
29
|
constructor(opts: {
|
|
@@ -25,9 +31,13 @@ export declare class UnLocodeLookup {
|
|
|
25
31
|
} | {
|
|
26
32
|
database: DatabaseSync;
|
|
27
33
|
});
|
|
28
|
-
/**
|
|
34
|
+
/**
|
|
35
|
+
* The UN/LOCODE (`"US NYC"`) for a country + place name, or null.
|
|
36
|
+
*/
|
|
29
37
|
byName(country: string, name: string): string | null;
|
|
30
|
-
/**
|
|
38
|
+
/**
|
|
39
|
+
* The nearest coordinate-bearing UN/LOCODE within `maxKm`, or null.
|
|
40
|
+
*/
|
|
31
41
|
nearest(lat: number, lon: number, maxKm?: number): string | null;
|
|
32
42
|
close(): void;
|
|
33
43
|
}
|
package/out/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAA;AAE1C,OAAO,KAAK,EAAiB,SAAS,EAAE,MAAM,wBAAwB,CAAA;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAA;AAE1C,OAAO,KAAK,EAAiB,SAAS,EAAE,MAAM,wBAAwB,CAAA;AAGtE;;GAEG;AACH,wBAAgB,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAO7C;AAED;;GAEG;AACH,wBAAgB,mBAAmB,CAAC,GAAG,EAAE,MAAM,GAAG;IAAE,GAAG,EAAE,MAAM,CAAC;IAAC,GAAG,EAAE,MAAM,CAAA;CAAE,GAAG,IAAI,CAQpF;AAED;;GAEG;AACH,qBAAa,cAAc;;gBAKd,IAAI,EAAE;QAAE,YAAY,EAAE,MAAM,CAAA;KAAE,GAAG;QAAE,QAAQ,EAAE,YAAY,CAAA;KAAE;IAYvE;;OAEG;IACH,MAAM,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI;IAQpD;;OAEG;IACH,OAAO,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,KAAK,SAAK,GAAG,MAAM,GAAG,IAAI;IAwB5D,KAAK,IAAI,IAAI;CAGb;AAED;;;GAGG;AACH,wBAAgB,qBAAqB,CAAC,MAAM,EAAE,cAAc,EAAE,IAAI,GAAE;IAAE,KAAK,CAAC,EAAE,MAAM,CAAA;CAAO,GAAG,SAAS,CAOtG"}
|
package/out/index.js
CHANGED
|
@@ -9,16 +9,21 @@
|
|
|
9
9
|
* list. An `@mailwoman/annotations` `Annotator`.
|
|
10
10
|
*/
|
|
11
11
|
import { DatabaseSync } from "node:sqlite";
|
|
12
|
-
|
|
12
|
+
import { haversineKm } from "@mailwoman/spatial";
|
|
13
|
+
/**
|
|
14
|
+
* Fold a place name to its match key: strip diacritics, lowercase, collapse whitespace.
|
|
15
|
+
*/
|
|
13
16
|
export function foldName(name) {
|
|
14
17
|
return name
|
|
15
18
|
.normalize("NFD")
|
|
16
|
-
.
|
|
19
|
+
.replaceAll(/[̀-ͯ]/g, "")
|
|
17
20
|
.toLowerCase()
|
|
18
21
|
.trim()
|
|
19
|
-
.
|
|
22
|
+
.replaceAll(/\s+/g, " ");
|
|
20
23
|
}
|
|
21
|
-
/**
|
|
24
|
+
/**
|
|
25
|
+
* Parse a UN/LOCODE coordinate (`"4923N 01522E"`) to decimal degrees, or null if absent/malformed.
|
|
26
|
+
*/
|
|
22
27
|
export function parseUnLocodeCoords(raw) {
|
|
23
28
|
const m = raw.trim().match(/^(\d{2})(\d{2})([NS])\s+(\d{3})(\d{2})([EW])$/);
|
|
24
29
|
if (!m)
|
|
@@ -27,15 +32,9 @@ export function parseUnLocodeCoords(raw) {
|
|
|
27
32
|
const lon = (Number(m[4]) + Number(m[5]) / 60) * (m[6] === "W" ? -1 : 1);
|
|
28
33
|
return { lat, lon };
|
|
29
34
|
}
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
const dLon = ((bLon - aLon) * Math.PI) / 180;
|
|
34
|
-
const s = Math.sin(dLat / 2) ** 2 +
|
|
35
|
-
Math.cos((aLat * Math.PI) / 180) * Math.cos((bLat * Math.PI) / 180) * Math.sin(dLon / 2) ** 2;
|
|
36
|
-
return 2 * EARTH_R_KM * Math.asin(Math.sqrt(s));
|
|
37
|
-
}
|
|
38
|
-
/** A UN/LOCODE lookup over a built `node:sqlite` table. */
|
|
35
|
+
/**
|
|
36
|
+
* A UN/LOCODE lookup over a built `node:sqlite` table.
|
|
37
|
+
*/
|
|
39
38
|
export class UnLocodeLookup {
|
|
40
39
|
#db;
|
|
41
40
|
#byName;
|
|
@@ -45,12 +44,16 @@ export class UnLocodeLookup {
|
|
|
45
44
|
this.#byName = this.#db.prepare("SELECT country, location FROM un_locode WHERE country = ? AND nameNorm = ? LIMIT 1");
|
|
46
45
|
this.#byBox = this.#db.prepare("SELECT location, country, lat, lon FROM un_locode WHERE lat BETWEEN ? AND ? AND lon BETWEEN ? AND ?");
|
|
47
46
|
}
|
|
48
|
-
/**
|
|
47
|
+
/**
|
|
48
|
+
* The UN/LOCODE (`"US NYC"`) for a country + place name, or null.
|
|
49
|
+
*/
|
|
49
50
|
byName(country, name) {
|
|
50
51
|
const row = this.#byName.get(country.toUpperCase(), foldName(name));
|
|
51
52
|
return row ? `${row.country} ${row.location}` : null;
|
|
52
53
|
}
|
|
53
|
-
/**
|
|
54
|
+
/**
|
|
55
|
+
* The nearest coordinate-bearing UN/LOCODE within `maxKm`, or null.
|
|
56
|
+
*/
|
|
54
57
|
nearest(lat, lon, maxKm = 25) {
|
|
55
58
|
const dLat = maxKm / 111;
|
|
56
59
|
const dLon = maxKm / (111 * Math.max(0.01, Math.cos((lat * Math.PI) / 180)));
|
package/out/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAA;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAA;AAG1C,OAAO,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAA;AAEhD;;GAEG;AACH,MAAM,UAAU,QAAQ,CAAC,IAAY;IACpC,OAAO,IAAI;SACT,SAAS,CAAC,KAAK,CAAC;SAChB,UAAU,CAAC,QAAQ,EAAE,EAAE,CAAC;SACxB,WAAW,EAAE;SACb,IAAI,EAAE;SACN,U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AAC,CAAC,QAAQ,EAAE,EAAE,EAAE,EAAE,CAAA;YAClD,CAAC;QACF,CAAC;QAED,OAAO,IAAI,EAAE,IAAI,IAAI,IAAI,CAAA;IAC1B,CAAC;IAED,KAAK;QACJ,IAAI,CAAC,GAAG,CAAC,KAAK,EAAE,CAAA;IACjB,CAAC;CACD;AAED;;;GAGG;AACH,MAAM,UAAU,qBAAqB,CAAC,MAAsB,EAAE,OAA2B,EAAE;IAC1F,OAAO,CAAC,EAAE,GAAG,EAAE,GAAG,EAAE,WAAW,EAAE,SAAS,EAAE,EAA0B,EAAE;QACvE,MAAM,MAAM,GAAG,WAAW,IAAI,SAAS,CAAC,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,WAAW,EAAE,SAAS,CAAC,CAAC,CAAC,CAAC,IAAI,CAAA;QACtF,MAAM,IAAI,GAAG,MAAM,IAAI,MAAM,CAAC,OAAO,CAAC,GAAG,EAAE,GAAG,EAAE,IAAI,CAAC,KAAK,CAAC,CAAA;QAE3D,OAAO,IAAI,CAAC,CAAC,CAAC,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC,EAAE,CAAA;IACtC,CAAC,CAAA;AACF,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mailwoman/un-locode-lookup",
|
|
3
|
-
"version": "8.
|
|
3
|
+
"version": "8.2.0",
|
|
4
4
|
"description": "Place → UN/LOCODE (UNECE Code for Trade and Transport Locations), by country + name or nearest coordinate. node:sqlite over the UNECE code list. An @mailwoman/annotations Annotator.",
|
|
5
5
|
"license": "AGPL-3.0-only OR LicenseRef-Commercial",
|
|
6
6
|
"repository": {
|
|
@@ -51,7 +51,8 @@
|
|
|
51
51
|
}
|
|
52
52
|
},
|
|
53
53
|
"dependencies": {
|
|
54
|
-
"@mailwoman/annotations": "8.
|
|
54
|
+
"@mailwoman/annotations": "8.2.0",
|
|
55
|
+
"@mailwoman/spatial": "8.2.0",
|
|
55
56
|
"csv-parse": "^7.0.1"
|
|
56
57
|
}
|
|
57
58
|
}
|