From b6202c0574748a541c0f374b6d6a2ff06aa62626 Mon Sep 17 00:00:00 2001 From: Keith Robertson Date: Sat, 8 Jun 2019 12:22:56 -0700 Subject: [PATCH] Remove country-regex and replace it with a more modern standards based dictionary --- package-lock.json | 10 +++++----- package.json | 2 +- src/lib/geo_location_utils.js | 22 ++++++++++------------ test/jasmine/tests/geo_test.js | 6 +++--- 4 files changed, 19 insertions(+), 21 deletions(-) diff --git a/package-lock.json b/package-lock.json index b936c2e605a..04250fa668b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2095,11 +2095,6 @@ "resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.2.tgz", "integrity": "sha1-tf1UIgqivFq1eqtxQMlAdUUDwac=" }, - "country-regex": { - "version": "1.1.0", - "resolved": "https://registry.npmjs.org/country-regex/-/country-regex-1.1.0.tgz", - "integrity": "sha1-UcMz3N8Sknt+XuucEKyBEqYSCJY=" - }, "create-ecdh": { "version": "4.0.3", "resolved": "https://registry.npmjs.org/create-ecdh/-/create-ecdh-4.0.3.tgz", @@ -5864,6 +5859,11 @@ "integrity": "sha1-7AbBDgo0wPL68Zn3/X/Hj//QPHM=", "dev": true }, + "i18n-iso-countries": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/i18n-iso-countries/-/i18n-iso-countries-4.1.0.tgz", + "integrity": "sha512-ttqCFBUvVSwUCgyjjIG95lilFg/61INah89ih/znBYHrZAcD5HsFUr8CJBmEgIOPbw0jZFgAPAsYRPGQexMTeA==" + }, "iconv-lite": { "version": "0.4.24", "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.24.tgz", diff --git a/package.json b/package.json index bcab01a9489..95408aefbb3 100644 --- a/package.json +++ b/package.json @@ -61,7 +61,6 @@ "canvas-fit": "^1.5.0", "color-normalize": "^1.3.0", "convex-hull": "^1.0.3", - "country-regex": "^1.1.0", "d3": "^3.5.12", "d3-force": "^1.0.6", "d3-hierarchy": "^1.1.8", @@ -90,6 +89,7 @@ "glslify": "^7.0.0", "has-hover": "^1.0.1", "has-passive-events": "^1.0.0", + "i18n-iso-countries": "^4.1.0", "mapbox-gl": "0.45.0", "matrix-camera-controller": "^2.1.3", "mouse-change": "^1.4.0", diff --git a/src/lib/geo_location_utils.js b/src/lib/geo_location_utils.js index 9790c3c7dee..72c00dcae6b 100644 --- a/src/lib/geo_location_utils.js +++ b/src/lib/geo_location_utils.js @@ -8,11 +8,11 @@ 'use strict'; -var countryRegex = require('country-regex'); +var countries = require("i18n-iso-countries"); var Lib = require('../lib'); -// make list of all country iso3 ids from at runtime -var countryIds = Object.keys(countryRegex); +// Minimize for browser. +countries.registerLocale(require("i18n-iso-countries/langs/en.json")); var locationmodeToIdFinder = { 'ISO-3': Lib.identity, @@ -21,16 +21,14 @@ var locationmodeToIdFinder = { }; function countryNameToISO3(countryName) { - for(var i = 0; i < countryIds.length; i++) { - var iso3 = countryIds[i]; - var regex = new RegExp(countryRegex[iso3]); - - if(regex.test(countryName.trim().toLowerCase())) return iso3; + // Remove trailing and double spaces + countryName = countryName.trim().replace(/ +(?= )/g, ''); + var iso3 = countries.getAlpha3Code(countryName, 'en'); + if(!iso3) { + Lib.log('Unrecognized country name: ' + countryName + '.'); + return false; } - - Lib.log('Unrecognized country name: ' + countryName + '.'); - - return false; + return iso3; } function locationToFeature(locationmode, location, features) { diff --git a/test/jasmine/tests/geo_test.js b/test/jasmine/tests/geo_test.js index b2a5ebbd0b0..5fa9e36c0b2 100644 --- a/test/jasmine/tests/geo_test.js +++ b/test/jasmine/tests/geo_test.js @@ -490,7 +490,7 @@ describe('geojson / topojson utils', function() { }); it('with *country names* locationmode', function() { - var out = _locationToFeature(topojson, 'United States', 'country names'); + var out = _locationToFeature(topojson, 'United States of America', 'country names'); expect(Object.keys(out)).toEqual(['type', 'id', 'properties', 'geometry']); expect(out.id).toEqual('USA'); @@ -509,8 +509,8 @@ describe('geojson / topojson utils', function() { var topojson = GeoAssets.topojson[topojsonName]; var shouldPass = [ - 'Virgin Islands (U.S.)', - ' Virgin Islands (U.S.) ' + 'Virgin Islands, U.S.', + ' Virgin Islands, U.S. ' ]; shouldPass.forEach(function(str) {