@hebcal/geo-sqlite 3.7.1 → 3.8.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/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- /*! @hebcal/geo-sqlite v3.7.1 */
1
+ /*! @hebcal/geo-sqlite v3.8.0 */
2
2
  'use strict';
3
3
 
4
4
  Object.defineProperty(exports, '__esModule', { value: true });
@@ -141,14 +141,32 @@ class GeoDb {
141
141
  fileMustExist: true
142
142
  });
143
143
  this.zipStmt = this.zipsDb.prepare(ZIPCODE_SQL);
144
+ /** @type {Map<string, Location>} */
145
+
144
146
  this.zipCache = new Map();
145
147
  this.geonamesStmt = this.geonamesDb.prepare(GEONAME_SQL);
148
+ /** @type {Map<number, Location>} */
149
+
146
150
  this.geonamesCache = new Map();
151
+ /** @type {Map<string, Location>} */
152
+
147
153
  this.legacyCities = new Map();
148
154
 
149
155
  for (const [name, id] of Object.entries(city2geonameid)) {
150
156
  this.legacyCities.set(GeoDb.munge(name), id);
151
157
  }
158
+
159
+ const stmt = this.geonamesDb.prepare(`SELECT ISO, Country FROM country WHERE Country <> ''`);
160
+ const rows = stmt.all();
161
+ const map = new Map();
162
+
163
+ for (const row of rows) {
164
+ map.set(row.ISO, row.Country);
165
+ }
166
+ /** @type {Map<string, string>} */
167
+
168
+
169
+ this.countryNames = map;
152
170
  }
153
171
  /** Closes database handles */
154
172
 
@@ -174,19 +192,20 @@ class GeoDb {
174
192
 
175
193
 
176
194
  lookupZip(zip) {
177
- const found = this.zipCache.get(zip);
195
+ const zip5 = zip.trim().substring(0, 5);
196
+ const found = this.zipCache.get(zip5);
178
197
  if (typeof found !== 'undefined') return found;
179
- const result = this.zipStmt.get(zip);
198
+ const result = this.zipStmt.get(zip5);
180
199
 
181
200
  if (!result) {
182
- if (this.logger) this.logger.warn(`GeoDb: unknown zipcode=${zip}`);
183
- this.zipCache.set(zip, null);
201
+ if (this.logger) this.logger.warn(`GeoDb: unknown zipcode=${zip5}`);
202
+ this.zipCache.set(zip5, null);
184
203
  return null;
185
204
  }
186
205
 
187
- result.ZipCode = String(zip);
206
+ result.ZipCode = String(zip5);
188
207
  const location = this.makeZipLocation(result);
189
- this.zipCache.set(zip, location);
208
+ this.zipCache.set(zip5, location);
190
209
  return location;
191
210
  }
192
211
  /**
package/dist/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- /*! @hebcal/geo-sqlite v3.7.1 */
1
+ /*! @hebcal/geo-sqlite v3.8.0 */
2
2
  import Database from 'better-sqlite3';
3
3
  import { Location, Locale } from '@hebcal/core';
4
4
  import pino from 'pino';
@@ -129,14 +129,32 @@ class GeoDb {
129
129
  fileMustExist: true
130
130
  });
131
131
  this.zipStmt = this.zipsDb.prepare(ZIPCODE_SQL);
132
+ /** @type {Map<string, Location>} */
133
+
132
134
  this.zipCache = new Map();
133
135
  this.geonamesStmt = this.geonamesDb.prepare(GEONAME_SQL);
136
+ /** @type {Map<number, Location>} */
137
+
134
138
  this.geonamesCache = new Map();
139
+ /** @type {Map<string, Location>} */
140
+
135
141
  this.legacyCities = new Map();
136
142
 
137
143
  for (const [name, id] of Object.entries(city2geonameid)) {
138
144
  this.legacyCities.set(GeoDb.munge(name), id);
139
145
  }
146
+
147
+ const stmt = this.geonamesDb.prepare(`SELECT ISO, Country FROM country WHERE Country <> ''`);
148
+ const rows = stmt.all();
149
+ const map = new Map();
150
+
151
+ for (const row of rows) {
152
+ map.set(row.ISO, row.Country);
153
+ }
154
+ /** @type {Map<string, string>} */
155
+
156
+
157
+ this.countryNames = map;
140
158
  }
141
159
  /** Closes database handles */
142
160
 
@@ -162,19 +180,20 @@ class GeoDb {
162
180
 
163
181
 
164
182
  lookupZip(zip) {
165
- const found = this.zipCache.get(zip);
183
+ const zip5 = zip.trim().substring(0, 5);
184
+ const found = this.zipCache.get(zip5);
166
185
  if (typeof found !== 'undefined') return found;
167
- const result = this.zipStmt.get(zip);
186
+ const result = this.zipStmt.get(zip5);
168
187
 
169
188
  if (!result) {
170
- if (this.logger) this.logger.warn(`GeoDb: unknown zipcode=${zip}`);
171
- this.zipCache.set(zip, null);
189
+ if (this.logger) this.logger.warn(`GeoDb: unknown zipcode=${zip5}`);
190
+ this.zipCache.set(zip5, null);
172
191
  return null;
173
192
  }
174
193
 
175
- result.ZipCode = String(zip);
194
+ result.ZipCode = String(zip5);
176
195
  const location = this.makeZipLocation(result);
177
- this.zipCache.set(zip, location);
196
+ this.zipCache.set(zip5, location);
178
197
  return location;
179
198
  }
180
199
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hebcal/geo-sqlite",
3
- "version": "3.7.1",
3
+ "version": "3.8.0",
4
4
  "author": "Michael J. Radwin (https://github.com/mjradwin)",
5
5
  "keywords": [
6
6
  "hebcal"
@@ -28,10 +28,10 @@
28
28
  "geo-sqlite.d.ts"
29
29
  ],
30
30
  "dependencies": {
31
- "@hebcal/core": "^3.32.1",
32
- "better-sqlite3": "^7.4.6",
33
- "pino": "^7.6.3",
34
- "pino-pretty": "^7.3.0"
31
+ "@hebcal/core": "^3.33.2",
32
+ "better-sqlite3": "^7.5.0",
33
+ "pino": "^7.6.4",
34
+ "pino-pretty": "^7.5.0"
35
35
  },
36
36
  "scripts": {
37
37
  "build": "rollup -c",
@@ -60,19 +60,19 @@
60
60
  "license": "BSD-2-Clause",
61
61
  "devDependencies": {
62
62
  "@ava/babel": "^2.0.0",
63
- "@babel/core": "^7.16.7",
63
+ "@babel/core": "^7.16.12",
64
64
  "@babel/polyfill": "^7.12.1",
65
- "@babel/preset-env": "^7.16.8",
66
- "@babel/register": "^7.16.8",
65
+ "@babel/preset-env": "^7.16.11",
66
+ "@babel/register": "^7.16.9",
67
67
  "@rollup/plugin-babel": "^5.3.0",
68
68
  "@rollup/plugin-commonjs": "^21.0.1",
69
69
  "@rollup/plugin-json": "^4.1.0",
70
70
  "@rollup/plugin-node-resolve": "^13.1.3",
71
71
  "ava": "^3.15.0",
72
- "eslint": "^8.6.0",
72
+ "eslint": "^8.7.0",
73
73
  "eslint-config-google": "^0.14.0",
74
- "jsdoc": "^3.6.7",
74
+ "jsdoc": "^3.6.9",
75
75
  "jsdoc-to-markdown": "^7.1.0",
76
- "rollup": "^2.63.0"
76
+ "rollup": "^2.66.0"
77
77
  }
78
78
  }