8000 Replace country-regex with i18n-iso-countries by dimitrov570 · Pull Request #7366 · plotly/plotly.js · GitHub
[go: up one dir, main page]

Skip to content

Replace country-regex with i18n-iso-countries #7366

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 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
replace country-regex with i18n-iso-countries
  • Loading branch information
dimitrov570 committed Feb 13, 2025
commit e803c5e487e185b6444e6afac9defe2a43a16b81
23 changes: 17 additions & 6 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 @@ -81,7 +81,6 @@
"color-normalize": "1.5.0",
"color-parse": "2.0.0",
"color-rgba": "2.1.1",
"country-regex": "^1.1.0",
"css-loader": "^7.1.2",
"d3-force": "^1.2.1",
"d3-format": "^1.4.5",
Expand All @@ -97,6 +96,7 @@
"gl-text": "^1.4.0",
"has-hover": "^1.0.1",
"has-passive-events": "^1.0.0",
"i18n-iso-countries": "^7.13.0",
"is-mobile": "^4.0.0",
"maplibre-gl": "^4.7.1",
"mouse-change": "^1.4.0",
Expand Down
16 changes: 8 additions & 8 deletions src/lib/geo_location_utils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

var d3 = require('@plotly/d3');
var countryRegex = require('country-regex');
var { area: turfArea } = require('@turf/area');
var { centroid: turfCentroid } = require('@turf/centroid');
var { bbox: turfBbox } = require('@turf/bbox');
Expand All @@ -12,8 +11,8 @@ var isPlainObject = require('./is_plain_object');
var nestedProperty = require('./nested_property');
var polygon = require('./polygon');

// make list of all country iso3 ids from at runtime
var countryIds = Object.keys(countryRegex);
const countries = require("i18n-iso-countries");
countries.registerLocale(require("i18n-iso-countries/langs/en.json"));

var locationmodeToIdFinder = {
'ISO-3': identity,
Expand All @@ -22,11 +21,12 @@ 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 sequences of whitespaces
var cleanName = countryName.replace(/\s+/g, ' ').trim();
var alpha3Code = countries.getAlpha3Code(cleanName, "en")

if (alpha3Code !== undefined) {
return alpha3Code;
}

loggers.log('Unrecognized country name: ' + countryName + '.');
Expand Down
4 changes: 2 additions & 2 deletions test/jasmine/tests/geo_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -819,8 +819,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
0