diff --git a/package-lock.json b/package-lock.json index 1a18d6cc08a..af2d06bdf21 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2232,11 +2232,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", @@ -5526,6 +5521,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 1bddc774a1b..16644a20150 100644 --- a/package.json +++ b/package.json @@ -66,7 +66,6 @@ "color-normalize": "^1.5.0", "color-rgba": "^2.1.1", "convex-hull": "^1.0.3", - "country-regex": "^1.1.0", "d3": "^3.5.17", "d3-force": "^1.2.1", "d3-hierarchy": "^1.1.9", @@ -95,6 +94,7 @@ "has-passive-events": "^1.0.0", "is-mobile": "^2.2.1", "mapbox-gl": "1.10.1", + "i18n-iso-countries": "^4.1.0", "matrix-camera-controller": "^2.1.3", "mouse-change": "^1.4.0", "mouse-event-offset": "^3.0.2", diff --git a/src/lib/geo_location_utils.js b/src/lib/geo_location_utils.js index 21521f058b9..6921ef000a3 100644 --- a/src/lib/geo_location_utils.js +++ b/src/lib/geo_location_utils.js @@ -9,7 +9,6 @@ 'use strict'; var d3 = require('d3'); -var countryRegex = require('country-regex'); var turfArea = require('@turf/area'); var turfCentroid = require('@turf/centroid'); var turfBbox = require('@turf/bbox'); @@ -19,9 +18,10 @@ var loggers = require('./loggers'); var isPlainObject = require('./is_plain_object'); var nestedProperty = require('./nested_property'); var polygon = require('./polygon'); +var countries = require("i18n-iso-countries"); -// 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': identity, @@ -30,16 +30,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) { + loggers.log('Unrecognized country name: ' + countryName + '.'); + return false; } - - loggers.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 426c9a7e745..fb8755df349 100644 --- a/test/jasmine/tests/geo_test.js +++ b/test/jasmine/tests/geo_test.js @@ -769,7 +769,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'); @@ -788,8 +788,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) {