diff --git a/draftlogs/7366_change.md b/draftlogs/7366_change.md new file mode 100644 index 00000000000..90205f72986 --- /dev/null +++ b/draftlogs/7366_change.md @@ -0,0 +1 @@ + - Replace country-regex with i18n-iso-countries [[#7366](https://github.com/plotly/plotly.js/pull/7366)] \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 276409becdc..8083bd43f1b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -22,7 +22,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", @@ -38,6 +37,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", @@ -2882,11 +2882,6 @@ "node": ">= 0.10" } }, - "node_modules/country-regex": { - "version": "1.1.0", - "resolved": "https://registry.npmjs.org/country-regex/-/country-regex-1.1.0.tgz", - "integrity": "sha1-UcMz3N8Sknt+XuucEKyBEqYSCJY=" - }, "node_modules/cross-spawn": { "version": "7.0.6", "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.6.tgz", @@ -3581,6 +3576,11 @@ "integrity": "sha1-gGZJMmzqp8qjMG112YXqJ0i6kTw=", "dev": true }, + "node_modules/diacritics": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/diacritics/-/diacritics-1.3.0.tgz", + "integrity": "sha512-wlwEkqcsaxvPJML+rDh/2iS824jbREk6DUMUKkEaSlxdYHeS43cClJtsWglvw2RfeXGm6ohKDqsXteJ5sP5enA==" + }, "node_modules/dir-glob": { "version": "3.0.1", "resolved": "https://registry.npmjs.org/dir-glob/-/dir-glob-3.0.1.tgz", @@ -5423,6 +5423,17 @@ "node": ">= 6" } }, + "node_modules/i18n-iso-countries": { + "version": "7.13.0", + "resolved": "https://registry.npmjs.org/i18n-iso-countries/-/i18n-iso-countries-7.13.0.tgz", + "integrity": "sha512-pVh4CjdgAHZswI98hzG+1BItQlsQfR+yGDsjDISoWIV/jHDAvCmSyZ5vj2YWwAjfVZ8/BhBDqWcFvuGOyHe4vg==", + "dependencies": { + "diacritics": "1.3.0" + }, + "engines": { + "node": ">= 12" + } + }, "node_modules/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 66df7e0854a..eb0db2b76ea 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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", diff --git a/src/lib/geo_location_utils.js b/src/lib/geo_location_utils.js index e15659f8f45..35e3e77553d 100644 --- a/src/lib/geo_location_utils.js +++ b/src/lib/geo_location_utils.js @@ -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'); @@ -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, @@ -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 + '.'); diff --git a/test/jasmine/tests/geo_test.js b/test/jasmine/tests/geo_test.js index 16ff8e10138..5a32254abd6 100644 --- a/test/jasmine/tests/geo_test.js +++ b/test/jasmine/tests/geo_test.js @@ -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) {