@hebcal/geo-sqlite 3.7.0 → 3.9.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.
Files changed (3) hide show
  1. package/dist/index.js +46 -12
  2. package/dist/index.mjs +46 -12
  3. package/package.json +17 -25
package/dist/index.js CHANGED
@@ -1,10 +1,11 @@
1
- /*! @hebcal/geo-sqlite v3.7.0 */
1
+ /*! @hebcal/geo-sqlite v3.9.0 */
2
2
  'use strict';
3
3
 
4
4
  Object.defineProperty(exports, '__esModule', { value: true });
5
5
 
6
6
  var Database = require('better-sqlite3');
7
7
  var core = require('@hebcal/core');
8
+ require('@hebcal/cities');
8
9
  var pino = require('pino');
9
10
  var events = require('events');
10
11
  var fs = require('fs');
@@ -141,14 +142,32 @@ class GeoDb {
141
142
  fileMustExist: true
142
143
  });
143
144
  this.zipStmt = this.zipsDb.prepare(ZIPCODE_SQL);
145
+ /** @type {Map<string, Location>} */
146
+
144
147
  this.zipCache = new Map();
145
148
  this.geonamesStmt = this.geonamesDb.prepare(GEONAME_SQL);
149
+ /** @type {Map<number, Location>} */
150
+
146
151
  this.geonamesCache = new Map();
152
+ /** @type {Map<string, Location>} */
153
+
147
154
  this.legacyCities = new Map();
148
155
 
149
156
  for (const [name, id] of Object.entries(city2geonameid)) {
150
157
  this.legacyCities.set(GeoDb.munge(name), id);
151
158
  }
159
+
160
+ const stmt = this.geonamesDb.prepare(`SELECT ISO, Country FROM country WHERE Country <> ''`);
161
+ const rows = stmt.all();
162
+ const map = new Map();
163
+
164
+ for (const row of rows) {
165
+ map.set(row.ISO, row.Country);
166
+ }
167
+ /** @type {Map<string, string>} */
168
+
169
+
170
+ this.countryNames = map;
152
171
  }
153
172
  /** Closes database handles */
154
173
 
@@ -174,19 +193,20 @@ class GeoDb {
174
193
 
175
194
 
176
195
  lookupZip(zip) {
177
- const found = this.zipCache.get(zip);
196
+ const zip5 = zip.trim().substring(0, 5);
197
+ const found = this.zipCache.get(zip5);
178
198
  if (typeof found !== 'undefined') return found;
179
- const result = this.zipStmt.get(zip);
199
+ const result = this.zipStmt.get(zip5);
180
200
 
181
201
  if (!result) {
182
- if (this.logger) this.logger.warn(`GeoDb: unknown zipcode=${zip}`);
183
- this.zipCache.set(zip, null);
202
+ if (this.logger) this.logger.warn(`GeoDb: unknown zipcode=${zip5}`);
203
+ this.zipCache.set(zip5, null);
184
204
  return null;
185
205
  }
186
206
 
187
- result.ZipCode = String(zip);
207
+ result.ZipCode = String(zip5);
188
208
  const location = this.makeZipLocation(result);
189
- this.zipCache.set(zip, location);
209
+ this.zipCache.set(zip5, location);
190
210
  return location;
191
211
  }
192
212
  /**
@@ -272,6 +292,12 @@ class GeoDb {
272
292
  if (geonameid) {
273
293
  return this.lookupGeoname(geonameid);
274
294
  } else {
295
+ const location = core.Location.lookup(cityName);
296
+
297
+ if (location) {
298
+ return location;
299
+ }
300
+
275
301
  if (this.logger) this.logger.warn(`GeoDb: unknown city=${cityName}`);
276
302
  return null;
277
303
  }
@@ -306,16 +332,21 @@ class GeoDb {
306
332
 
307
333
 
308
334
  autoComplete(qraw) {
335
+ qraw = qraw.trim();
336
+
309
337
  if (qraw.length === 0) {
310
338
  return [];
311
339
  }
312
340
 
313
- if (qraw.charCodeAt(0) >= 48 && qraw.charCodeAt(0) <= 57) {
341
+ const firstCharCode = qraw.charCodeAt(0);
342
+
343
+ if (firstCharCode >= 48 && firstCharCode <= 57) {
314
344
  if (!this.zipCompStmt) {
315
345
  this.zipCompStmt = this.zipsDb.prepare(ZIP_COMPLETE_SQL);
316
346
  }
317
347
 
318
- return this.zipCompStmt.all(qraw + '%').map(GeoDb.zipResultToObj);
348
+ const zip5 = qraw.substring(0, 5);
349
+ return this.zipCompStmt.all(zip5 + '%').map(GeoDb.zipResultToObj);
319
350
  } else {
320
351
  if (!this.geonamesCompStmt) {
321
352
  this.geonamesCompStmt = this.geonamesDb.prepare(GEONAME_COMPLETE_SQL);
@@ -426,9 +457,12 @@ class GeoDb {
426
457
  async function buildGeonamesSqlite(dbFilename, countryInfotxt, cities5000txt, citiesPatch, admin1CodesASCIItxt, ILtxt) {
427
458
  const logger = pino__default["default"]({
428
459
  // level: argv.quiet ? 'warn' : 'info',
429
- prettyPrint: {
430
- translateTime: true,
431
- ignore: 'pid,hostname'
460
+ transport: {
461
+ target: 'pino-pretty',
462
+ options: {
463
+ translateTime: 'SYS:standard',
464
+ ignore: 'pid,hostname'
465
+ }
432
466
  }
433
467
  });
434
468
  const db = new Database__default["default"](dbFilename);
package/dist/index.mjs CHANGED
@@ -1,6 +1,7 @@
1
- /*! @hebcal/geo-sqlite v3.7.0 */
1
+ /*! @hebcal/geo-sqlite v3.9.0 */
2
2
  import Database from 'better-sqlite3';
3
3
  import { Location, Locale } from '@hebcal/core';
4
+ import '@hebcal/cities';
4
5
  import pino from 'pino';
5
6
  import events from 'events';
6
7
  import fs from 'fs';
@@ -129,14 +130,32 @@ class GeoDb {
129
130
  fileMustExist: true
130
131
  });
131
132
  this.zipStmt = this.zipsDb.prepare(ZIPCODE_SQL);
133
+ /** @type {Map<string, Location>} */
134
+
132
135
  this.zipCache = new Map();
133
136
  this.geonamesStmt = this.geonamesDb.prepare(GEONAME_SQL);
137
+ /** @type {Map<number, Location>} */
138
+
134
139
  this.geonamesCache = new Map();
140
+ /** @type {Map<string, Location>} */
141
+
135
142
  this.legacyCities = new Map();
136
143
 
137
144
  for (const [name, id] of Object.entries(city2geonameid)) {
138
145
  this.legacyCities.set(GeoDb.munge(name), id);
139
146
  }
147
+
148
+ const stmt = this.geonamesDb.prepare(`SELECT ISO, Country FROM country WHERE Country <> ''`);
149
+ const rows = stmt.all();
150
+ const map = new Map();
151
+
152
+ for (const row of rows) {
153
+ map.set(row.ISO, row.Country);
154
+ }
155
+ /** @type {Map<string, string>} */
156
+
157
+
158
+ this.countryNames = map;
140
159
  }
141
160
  /** Closes database handles */
142
161
 
@@ -162,19 +181,20 @@ class GeoDb {
162
181
 
163
182
 
164
183
  lookupZip(zip) {
165
- const found = this.zipCache.get(zip);
184
+ const zip5 = zip.trim().substring(0, 5);
185
+ const found = this.zipCache.get(zip5);
166
186
  if (typeof found !== 'undefined') return found;
167
- const result = this.zipStmt.get(zip);
187
+ const result = this.zipStmt.get(zip5);
168
188
 
169
189
  if (!result) {
170
- if (this.logger) this.logger.warn(`GeoDb: unknown zipcode=${zip}`);
171
- this.zipCache.set(zip, null);
190
+ if (this.logger) this.logger.warn(`GeoDb: unknown zipcode=${zip5}`);
191
+ this.zipCache.set(zip5, null);
172
192
  return null;
173
193
  }
174
194
 
175
- result.ZipCode = String(zip);
195
+ result.ZipCode = String(zip5);
176
196
  const location = this.makeZipLocation(result);
177
- this.zipCache.set(zip, location);
197
+ this.zipCache.set(zip5, location);
178
198
  return location;
179
199
  }
180
200
  /**
@@ -260,6 +280,12 @@ class GeoDb {
260
280
  if (geonameid) {
261
281
  return this.lookupGeoname(geonameid);
262
282
  } else {
283
+ const location = Location.lookup(cityName);
284
+
285
+ if (location) {
286
+ return location;
287
+ }
288
+
263
289
  if (this.logger) this.logger.warn(`GeoDb: unknown city=${cityName}`);
264
290
  return null;
265
291
  }
@@ -294,16 +320,21 @@ class GeoDb {
294
320
 
295
321
 
296
322
  autoComplete(qraw) {
323
+ qraw = qraw.trim();
324
+
297
325
  if (qraw.length === 0) {
298
326
  return [];
299
327
  }
300
328
 
301
- if (qraw.charCodeAt(0) >= 48 && qraw.charCodeAt(0) <= 57) {
329
+ const firstCharCode = qraw.charCodeAt(0);
330
+
331
+ if (firstCharCode >= 48 && firstCharCode <= 57) {
302
332
  if (!this.zipCompStmt) {
303
333
  this.zipCompStmt = this.zipsDb.prepare(ZIP_COMPLETE_SQL);
304
334
  }
305
335
 
306
- return this.zipCompStmt.all(qraw + '%').map(GeoDb.zipResultToObj);
336
+ const zip5 = qraw.substring(0, 5);
337
+ return this.zipCompStmt.all(zip5 + '%').map(GeoDb.zipResultToObj);
307
338
  } else {
308
339
  if (!this.geonamesCompStmt) {
309
340
  this.geonamesCompStmt = this.geonamesDb.prepare(GEONAME_COMPLETE_SQL);
@@ -414,9 +445,12 @@ class GeoDb {
414
445
  async function buildGeonamesSqlite(dbFilename, countryInfotxt, cities5000txt, citiesPatch, admin1CodesASCIItxt, ILtxt) {
415
446
  const logger = pino({
416
447
  // level: argv.quiet ? 'warn' : 'info',
417
- prettyPrint: {
418
- translateTime: true,
419
- ignore: 'pid,hostname'
448
+ transport: {
449
+ target: 'pino-pretty',
450
+ options: {
451
+ translateTime: 'SYS:standard',
452
+ ignore: 'pid,hostname'
453
+ }
420
454
  }
421
455
  });
422
456
  const db = new Database(dbFilename);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hebcal/geo-sqlite",
3
- "version": "3.7.0",
3
+ "version": "3.9.0",
4
4
  "author": "Michael J. Radwin (https://github.com/mjradwin)",
5
5
  "keywords": [
6
6
  "hebcal"
@@ -28,10 +28,11 @@
28
28
  "geo-sqlite.d.ts"
29
29
  ],
30
30
  "dependencies": {
31
- "@hebcal/core": "^3.32.0",
32
- "better-sqlite3": "^7.4.6",
33
- "pino": "^7.6.2",
34
- "pino-pretty": "^7.3.0"
31
+ "@hebcal/cities": "^3.1.1",
32
+ "@hebcal/core": "^3.33.3",
33
+ "better-sqlite3": "^7.5.0",
34
+ "pino": "^7.8.0",
35
+ "pino-pretty": "^7.5.1"
35
36
  },
36
37
  "scripts": {
37
38
  "build": "rollup -c",
@@ -45,34 +46,25 @@
45
46
  ],
46
47
  "require": [
47
48
  "@babel/register",
48
- "@babel/polyfill"
49
+ "regenerator-runtime/runtime"
49
50
  ],
50
- "babel": {
51
- "testOptions": {
52
- "presets": [
53
- "@babel/env"
54
- ]
55
- }
56
- },
57
51
  "inherit": true,
58
52
  "verbose": true
59
53
  },
60
54
  "license": "BSD-2-Clause",
61
55
  "devDependencies": {
62
- "@ava/babel": "^2.0.0",
63
- "@babel/core": "^7.16.7",
64
- "@babel/polyfill": "^7.12.1",
65
- "@babel/preset-env": "^7.16.7",
66
- "@babel/register": "^7.16.7",
67
- "@rollup/plugin-babel": "^5.3.0",
68
- "@rollup/plugin-commonjs": "^21.0.1",
56
+ "@babel/core": "^7.17.5",
57
+ "@babel/preset-env": "^7.16.11",
58
+ "@babel/register": "^7.17.0",
59
+ "@rollup/plugin-babel": "^5.3.1",
60
+ "@rollup/plugin-commonjs": "^21.0.2",
69
61
  "@rollup/plugin-json": "^4.1.0",
70
62
  "@rollup/plugin-node-resolve": "^13.1.3",
71
- "ava": "^3.15.0",
72
- "eslint": "^8.6.0",
63
+ "ava": "^4.0.1",
64
+ "eslint": "^8.9.0",
73
65
  "eslint-config-google": "^0.14.0",
74
- "jsdoc": "^3.6.7",
75
- "jsdoc-to-markdown": "^7.1.0",
76
- "rollup": "^2.63.0"
66
+ "jsdoc": "^3.6.10",
67
+ "jsdoc-to-markdown": "^7.1.1",
68
+ "rollup": "^2.68.0"
77
69
  }
78
70
  }