@hebcal/geo-sqlite 4.6.0 → 4.7.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 +110 -71
  2. package/dist/index.mjs +110 -71
  3. package/package.json +2 -2
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- /*! @hebcal/geo-sqlite v4.6.0 */
1
+ /*! @hebcal/geo-sqlite v4.7.0 */
2
2
  'use strict';
3
3
 
4
4
  Object.defineProperty(exports, '__esModule', { value: true });
@@ -372,42 +372,7 @@ class GeoDb {
372
372
 
373
373
  const geoMatches = geoRows.map(res => {
374
374
  const loc = this.lookupGeoname(res.geonameid);
375
- const cc = loc.getCountryCode();
376
- const country = res.country || this.countryNames.get(cc) || '';
377
- const admin1 = res.admin || loc.admin1 || '';
378
- const obj = {
379
- id: res.geonameid,
380
- value: res.longname,
381
- admin1,
382
- country,
383
- cc,
384
- latitude: loc.latitude,
385
- longitude: loc.longitude,
386
- timezone: loc.getTzid(),
387
- geo: 'geoname'
388
- };
389
-
390
- if (loc.population) {
391
- obj.population = loc.population;
392
- }
393
-
394
- if (res.city !== loc.asciiname) {
395
- obj.name = res.city;
396
- }
397
-
398
- if (loc.asciiname) {
399
- obj.asciiname = loc.asciiname;
400
- }
401
-
402
- if (country) {
403
- obj.country = country;
404
- }
405
-
406
- if (admin1) {
407
- obj.admin1 = admin1;
408
- }
409
-
410
- return obj;
375
+ return this.geonameLocToAutocomplete(loc, res);
411
376
  });
412
377
 
413
378
  if (!this.zipFulltextCompStmt) {
@@ -416,42 +381,12 @@ class GeoDb {
416
381
 
417
382
  const zipRows = this.zipFulltextCompStmt.all(`{longname} : "${qraw}" *`);
418
383
  const zipMatches = zipRows.map(res => {
419
- const zipCode = res.ZipCode;
420
- const loc = this.lookupZip(zipCode);
421
- const obj = {
422
- id: zipCode,
423
- value: loc.getName(),
424
- admin1: loc.admin1,
425
- asciiname: loc.getShortName(),
426
- country: 'United States',
427
- cc: 'US',
428
- latitude: loc.latitude,
429
- longitude: loc.longitude,
430
- timezone: loc.getTzid(),
431
- population: loc.population,
432
- geo: 'zip'
433
- };
434
- return obj;
384
+ const loc = this.lookupZip(res.ZipCode);
385
+ return GeoDb.zipLocToAutocomplete(loc);
435
386
  });
436
- const map = new Map();
437
-
438
- for (const obj of zipMatches) {
439
- const key = [obj.asciiname, stateNames[obj.admin1], obj.cc].join('|');
440
-
441
- if (!map.has(key)) {
442
- map.set(key, obj);
443
- }
444
- } // GeoNames takes priority over USA ZIP code matches
445
-
446
-
447
- for (const obj of geoMatches) {
448
- const key = [obj.asciiname, obj.admin1, obj.cc].join('|');
449
- map.set(key, obj);
450
- }
451
-
452
- const values = Array.from(map.values());
387
+ const values = this.mergeZipGeo(zipMatches, geoMatches);
453
388
  values.sort((a, b) => b.population - a.population);
454
- const topN = values.slice(0, 15);
389
+ const topN = values.slice(0, 12);
455
390
 
456
391
  if (!latlong) {
457
392
  for (const val of topN) {
@@ -465,6 +400,110 @@ class GeoDb {
465
400
  return topN;
466
401
  }
467
402
  }
403
+ /**
404
+ * @private
405
+ * @param {Location} loc
406
+ * @param {any} res
407
+ * @return {any}
408
+ */
409
+
410
+
411
+ geonameLocToAutocomplete(loc, res) {
412
+ const cc = loc.getCountryCode();
413
+ const country = res.country || this.countryNames.get(cc) || '';
414
+ const admin1 = res.admin || loc.admin1 || '';
415
+ const obj = {
416
+ id: res.geonameid,
417
+ value: res.longname,
418
+ admin1,
419
+ country,
420
+ cc,
421
+ latitude: loc.latitude,
422
+ longitude: loc.longitude,
423
+ timezone: loc.getTzid(),
424
+ geo: 'geoname'
425
+ };
426
+
427
+ if (loc.population) {
428
+ obj.population = loc.population;
429
+ }
430
+
431
+ if (res.city !== loc.asciiname) {
432
+ obj.name = res.city;
433
+ }
434
+
435
+ if (loc.asciiname) {
436
+ obj.asciiname = loc.asciiname;
437
+ }
438
+
439
+ if (country) {
440
+ obj.country = country;
441
+ }
442
+
443
+ if (admin1) {
444
+ obj.admin1 = admin1;
445
+ }
446
+
447
+ return obj;
448
+ }
449
+ /**
450
+ * @private
451
+ * @param {Location} loc
452
+ * @return {any}
453
+ */
454
+
455
+
456
+ static zipLocToAutocomplete(loc) {
457
+ return {
458
+ id: loc.zip,
459
+ value: loc.getName(),
460
+ admin1: loc.admin1,
461
+ asciiname: loc.getShortName(),
462
+ country: 'United States',
463
+ cc: 'US',
464
+ latitude: loc.latitude,
465
+ longitude: loc.longitude,
466
+ timezone: loc.getTzid(),
467
+ population: loc.population,
468
+ geo: 'zip'
469
+ };
470
+ }
471
+ /**
472
+ * GeoNames matches takes priority over USA ZIP code matches
473
+ * @private
474
+ * @param {any[]} zipMatches
475
+ * @param {any[]} geoMatches
476
+ * @return {any[]}
477
+ */
478
+
479
+
480
+ mergeZipGeo(zipMatches, geoMatches) {
481
+ const zlen = zipMatches.length;
482
+ const glen = geoMatches.length;
483
+
484
+ if (zlen && !glen) {
485
+ return zipMatches;
486
+ } else if (glen && !zlen) {
487
+ return geoMatches;
488
+ }
489
+
490
+ const map = new Map();
491
+
492
+ for (const obj of zipMatches) {
493
+ const key = [obj.asciiname, stateNames[obj.admin1], obj.cc].join('|');
494
+
495
+ if (!map.has(key)) {
496
+ map.set(key, obj);
497
+ }
498
+ }
499
+
500
+ for (const obj of geoMatches) {
501
+ const key = [obj.asciiname, obj.admin1, obj.cc].join('|');
502
+ map.set(key, obj);
503
+ }
504
+
505
+ return Array.from(map.values());
506
+ }
468
507
  /** Reads entire ZIP database and caches in-memory */
469
508
 
470
509
 
package/dist/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- /*! @hebcal/geo-sqlite v4.6.0 */
1
+ /*! @hebcal/geo-sqlite v4.7.0 */
2
2
  import Database from 'better-sqlite3';
3
3
  import { Location, Locale } from '@hebcal/core';
4
4
  import '@hebcal/cities';
@@ -360,42 +360,7 @@ class GeoDb {
360
360
 
361
361
  const geoMatches = geoRows.map(res => {
362
362
  const loc = this.lookupGeoname(res.geonameid);
363
- const cc = loc.getCountryCode();
364
- const country = res.country || this.countryNames.get(cc) || '';
365
- const admin1 = res.admin || loc.admin1 || '';
366
- const obj = {
367
- id: res.geonameid,
368
- value: res.longname,
369
- admin1,
370
- country,
371
- cc,
372
- latitude: loc.latitude,
373
- longitude: loc.longitude,
374
- timezone: loc.getTzid(),
375
- geo: 'geoname'
376
- };
377
-
378
- if (loc.population) {
379
- obj.population = loc.population;
380
- }
381
-
382
- if (res.city !== loc.asciiname) {
383
- obj.name = res.city;
384
- }
385
-
386
- if (loc.asciiname) {
387
- obj.asciiname = loc.asciiname;
388
- }
389
-
390
- if (country) {
391
- obj.country = country;
392
- }
393
-
394
- if (admin1) {
395
- obj.admin1 = admin1;
396
- }
397
-
398
- return obj;
363
+ return this.geonameLocToAutocomplete(loc, res);
399
364
  });
400
365
 
401
366
  if (!this.zipFulltextCompStmt) {
@@ -404,42 +369,12 @@ class GeoDb {
404
369
 
405
370
  const zipRows = this.zipFulltextCompStmt.all(`{longname} : "${qraw}" *`);
406
371
  const zipMatches = zipRows.map(res => {
407
- const zipCode = res.ZipCode;
408
- const loc = this.lookupZip(zipCode);
409
- const obj = {
410
- id: zipCode,
411
- value: loc.getName(),
412
- admin1: loc.admin1,
413
- asciiname: loc.getShortName(),
414
- country: 'United States',
415
- cc: 'US',
416
- latitude: loc.latitude,
417
- longitude: loc.longitude,
418
- timezone: loc.getTzid(),
419
- population: loc.population,
420
- geo: 'zip'
421
- };
422
- return obj;
372
+ const loc = this.lookupZip(res.ZipCode);
373
+ return GeoDb.zipLocToAutocomplete(loc);
423
374
  });
424
- const map = new Map();
425
-
426
- for (const obj of zipMatches) {
427
- const key = [obj.asciiname, stateNames[obj.admin1], obj.cc].join('|');
428
-
429
- if (!map.has(key)) {
430
- map.set(key, obj);
431
- }
432
- } // GeoNames takes priority over USA ZIP code matches
433
-
434
-
435
- for (const obj of geoMatches) {
436
- const key = [obj.asciiname, obj.admin1, obj.cc].join('|');
437
- map.set(key, obj);
438
- }
439
-
440
- const values = Array.from(map.values());
375
+ const values = this.mergeZipGeo(zipMatches, geoMatches);
441
376
  values.sort((a, b) => b.population - a.population);
442
- const topN = values.slice(0, 15);
377
+ const topN = values.slice(0, 12);
443
378
 
444
379
  if (!latlong) {
445
380
  for (const val of topN) {
@@ -453,6 +388,110 @@ class GeoDb {
453
388
  return topN;
454
389
  }
455
390
  }
391
+ /**
392
+ * @private
393
+ * @param {Location} loc
394
+ * @param {any} res
395
+ * @return {any}
396
+ */
397
+
398
+
399
+ geonameLocToAutocomplete(loc, res) {
400
+ const cc = loc.getCountryCode();
401
+ const country = res.country || this.countryNames.get(cc) || '';
402
+ const admin1 = res.admin || loc.admin1 || '';
403
+ const obj = {
404
+ id: res.geonameid,
405
+ value: res.longname,
406
+ admin1,
407
+ country,
408
+ cc,
409
+ latitude: loc.latitude,
410
+ longitude: loc.longitude,
411
+ timezone: loc.getTzid(),
412
+ geo: 'geoname'
413
+ };
414
+
415
+ if (loc.population) {
416
+ obj.population = loc.population;
417
+ }
418
+
419
+ if (res.city !== loc.asciiname) {
420
+ obj.name = res.city;
421
+ }
422
+
423
+ if (loc.asciiname) {
424
+ obj.asciiname = loc.asciiname;
425
+ }
426
+
427
+ if (country) {
428
+ obj.country = country;
429
+ }
430
+
431
+ if (admin1) {
432
+ obj.admin1 = admin1;
433
+ }
434
+
435
+ return obj;
436
+ }
437
+ /**
438
+ * @private
439
+ * @param {Location} loc
440
+ * @return {any}
441
+ */
442
+
443
+
444
+ static zipLocToAutocomplete(loc) {
445
+ return {
446
+ id: loc.zip,
447
+ value: loc.getName(),
448
+ admin1: loc.admin1,
449
+ asciiname: loc.getShortName(),
450
+ country: 'United States',
451
+ cc: 'US',
452
+ latitude: loc.latitude,
453
+ longitude: loc.longitude,
454
+ timezone: loc.getTzid(),
455
+ population: loc.population,
456
+ geo: 'zip'
457
+ };
458
+ }
459
+ /**
460
+ * GeoNames matches takes priority over USA ZIP code matches
461
+ * @private
462
+ * @param {any[]} zipMatches
463
+ * @param {any[]} geoMatches
464
+ * @return {any[]}
465
+ */
466
+
467
+
468
+ mergeZipGeo(zipMatches, geoMatches) {
469
+ const zlen = zipMatches.length;
470
+ const glen = geoMatches.length;
471
+
472
+ if (zlen && !glen) {
473
+ return zipMatches;
474
+ } else if (glen && !zlen) {
475
+ return geoMatches;
476
+ }
477
+
478
+ const map = new Map();
479
+
480
+ for (const obj of zipMatches) {
481
+ const key = [obj.asciiname, stateNames[obj.admin1], obj.cc].join('|');
482
+
483
+ if (!map.has(key)) {
484
+ map.set(key, obj);
485
+ }
486
+ }
487
+
488
+ for (const obj of geoMatches) {
489
+ const key = [obj.asciiname, obj.admin1, obj.cc].join('|');
490
+ map.set(key, obj);
491
+ }
492
+
493
+ return Array.from(map.values());
494
+ }
456
495
  /** Reads entire ZIP database and caches in-memory */
457
496
 
458
497
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hebcal/geo-sqlite",
3
- "version": "4.6.0",
3
+ "version": "4.7.0",
4
4
  "author": "Michael J. Radwin (https://github.com/mjradwin)",
5
5
  "keywords": [
6
6
  "hebcal"
@@ -61,7 +61,7 @@
61
61
  "@rollup/plugin-json": "^4.1.0",
62
62
  "@rollup/plugin-node-resolve": "^13.3.0",
63
63
  "ava": "^4.3.0",
64
- "eslint": "^8.16.0",
64
+ "eslint": "^8.17.0",
65
65
  "eslint-config-google": "^0.14.0",
66
66
  "jsdoc": "^3.6.10",
67
67
  "jsdoc-to-markdown": "^7.1.1",