Skip to content

Remove country-regex and replace it with a more modern version #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
22 changes: 10 additions & 12 deletions src/lib/geo_location_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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) {
Expand Down
6 changes: 3 additions & 3 deletions test/jasmine/tests/geo_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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) {
Expand Down