diff --git a/draftlogs/7366_change.md b/draftlogs/7366_change.md new file mode 100644 index 00000000000..1107ad9b893 --- /dev/null +++ b/draftlogs/7366_change.md @@ -0,0 +1 @@ + - Replace country-regex package with i18n-iso-countries [[#7366](https://github.com/plotly/plotly.js/pull/7366)] diff --git a/package-lock.json b/package-lock.json index 69a4d237494..0153a05a200 100644 --- a/package-lock.json +++ b/package-lock.json @@ -23,7 +23,6 @@ "color-normalize": "1.5.0", "color-parse": "2.0.0", "color-rgba": "3.0.0", - "country-regex": "^1.1.0", "d3-force": "^1.2.1", "d3-format": "^1.4.5", "d3-geo": "^1.12.1", @@ -37,6 +36,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", @@ -2999,11 +2999,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", @@ -3698,6 +3693,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", @@ -5591,6 +5591,17 @@ "node": ">= 14" } }, + "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 2ca45d1e63c..9cdf03a8c43 100644 --- a/package.json +++ b/package.json @@ -81,7 +81,6 @@ "color-normalize": "1.5.0", "color-parse": "2.0.0", "color-rgba": "3.0.0", - "country-regex": "^1.1.0", "d3-force": "^1.2.1", "d3-format": "^1.4.5", "d3-geo": "^1.12.1", @@ -95,6 +94,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..43e13d44136 100644 --- a/src/lib/geo_location_utils.js +++ b/src/lib/geo_location_utils.js @@ -1,10 +1,10 @@ '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'); +const countries = require("i18n-iso-countries"); var identity = require('./identity'); var loggers = require('./loggers'); @@ -12,8 +12,7 @@ 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); +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/tasks/compress_attributes.js b/tasks/compress_attributes.js index aa1ae1df7d9..1b4cf055533 100644 --- a/tasks/compress_attributes.js +++ b/tasks/compress_attributes.js @@ -35,22 +35,32 @@ function makeRegex(regexStr) { ); } -module.exports = function() { - var allChunks = []; - return through(function(chunk, enc, next) { - allChunks.push(chunk); - next(); - }, function(done) { - var str = Buffer.concat(allChunks).toString('utf-8'); - this.push( - str - .replace(makeStringRegex('description'), '') - .replace(makeJoinedArrayRegex('description'), '') - .replace(makeArrayRegex('requiredOpts'), '') - .replace(makeArrayRegex('otherOpts'), '') - .replace(makeStringRegex('role'), '') - .replace(makeStringRegex('hrName'), '') - ); - done(); - }); +module.exports = path => { + const allChunks = []; + return through( + (chunk, _, next) => { + allChunks.push(chunk); + next(); + }, + function(done) { + const str = Buffer.concat(allChunks).toString('utf-8'); + + // Return JSON as stringified JSON so that ESBuild will handle transformation + if(path.toLowerCase().endsWith('.json')) { + this.push(JSON.stringify(str)); + done(); + } + + this.push( + str + .replace(makeStringRegex('description'), '') + .replace(makeJoinedArrayRegex('description'), '') + .replace(makeArrayRegex('requiredOpts'), '') + .replace(makeArrayRegex('otherOpts'), '') + .replace(makeStringRegex('role'), '') + .replace(makeStringRegex('hrName'), '') + ); + done(); + } + ); }; diff --git a/test/image/mocks/geo_country-names-text-chart.json b/test/image/mocks/geo_country-names-text-chart.json index de5bb993889..5fd169e232f 100644 --- a/test/image/mocks/geo_country-names-text-chart.json +++ b/test/image/mocks/geo_country-names-text-chart.json @@ -6,7 +6,7 @@ "locationmode": "country names", "locations": [ "Belarus", - "Moldova", + "Moldova, Republic of", "Lithuania", "Russia", "Romania", @@ -74,7 +74,7 @@ "Georgia", "Uruguay", "Angola", - "Laos", + "Lao People's Democratic Republic", "Japan", "Mexico", "Ecuador", @@ -94,7 +94,7 @@ "Italy", "Trinidad and Tobago", "China", - "Macedonia", + "The Republic of North Macedonia", "Saint Vincent and the Grenadines", "Equatorial Guinea", "Suriname", @@ -105,12 +105,11 @@ "Colombia", "Ivory Coast", "Bolivia", - "Swaziland", + "Eswatini", "Zimbabwe", "Seychelles", "Cambodia", "Puerto Rico", - "Netherlands Antilles", "Philippines", "Costa Rica", "Armenia", @@ -140,7 +139,7 @@ "Democratic Republic of the Congo", "Nauru", "Gambia", - "Federated States of Micronesia", + "Micronesia, Federated States of", "El Salvador", "Fiji", "Papua New Guinea", @@ -154,7 +153,7 @@ "Mozambique", "Togo", "Nepal", - "Brunei", + "Brunei Darussalam", "Benin", "Singapore", "Turkey", @@ -167,19 +166,18 @@ "Vanuatu", "Djibouti", "Malaysia", - "Syria", + "Syrian Arab Republic", "Maldives", "Mali", "Eritrea", "Algeria", "Iran", "Oman", - "Brunei", "Morocco", "Jordan", "Bhutan", "Guinea", - "Burma", + "Myanmar", "Afghanistan", "Senegal", "Indonesia", @@ -199,7 +197,7 @@ ], "text": [ "Belarus", - "Moldova", + "Moldova, Republic of", "Lithuania", "Russia", "Romania", @@ -267,7 +265,7 @@ "Georgia", "Uruguay", "Angola", - "Laos", + "Lao People's Democratic Republic", "Japan", "Mexico", "Ecuador", @@ -287,7 +285,7 @@ "Italy", "Trinidad and Tobago", "China", - "Macedonia", + "The Republic of North Macedonia", "Saint Vincent and the Grenadines", "Equatorial Guinea", "Suriname", @@ -298,12 +296,11 @@ "Colombia", "Ivory Coast", "Bolivia", - "Swaziland", + "Eswatini", "Zimbabwe", "Seychelles", "Cambodia", "Puerto Rico", - "Netherlands Antilles", "Philippines", "Costa Rica", "Armenia", @@ -333,7 +330,7 @@ "Democratic Republic of the Congo", "Nauru", "Gambia", - "Federated States of Micronesia", + "Micronesia, Federated States of", "El Salvador", "Fiji", "Papua New Guinea", @@ -347,7 +344,7 @@ "Mozambique", "Togo", "Nepal", - "Brunei", + "Brunei Darussalam", "Benin", "Singapore", "Turkey", @@ -360,19 +357,18 @@ "Vanuatu", "Djibouti", "Malaysia", - "Syria", + "Syrian Arab Republic", "Maldives", "Mali", "Eritrea", "Algeria", "Iran", "Oman", - "Brunei", "Morocco", "Jordan", "Bhutan", "Guinea", - "Burma", + "Myanmar", "Afghanistan", "Senegal", "Indonesia", @@ -397,7 +393,7 @@ "locationmode": "country names", "locations": [ "Belarus", - "Moldova", + "Moldova, Republic of", "Lithuania", "Russia", "Romania", @@ -465,7 +461,7 @@ "Georgia", "Uruguay", "Angola", - "Laos", + "Lao People's Democratic Republic", "Japan", "Mexico", "Ecuador", @@ -485,7 +481,7 @@ "Italy", "Trinidad and Tobago", "China", - "Macedonia", + "The Republic of North Macedonia", "Saint Vincent and the Grenadines", "Equatorial Guinea", "Suriname", @@ -496,12 +492,11 @@ "Colombia", "Ivory Coast", "Bolivia", - "Swaziland", + "Eswatini", "Zimbabwe", "Seychelles", "Cambodia", "Puerto Rico", - "Netherlands Antilles", "Philippines", "Costa Rica", "Armenia", @@ -531,7 +526,7 @@ "Democratic Republic of the Congo", "Nauru", "Gambia", - "Federated States of Micronesia", + "Micronesia, Federated States of", "El Salvador", "Fiji", "Papua New Guinea", @@ -545,7 +540,7 @@ "Mozambique", "Togo", "Nepal", - "Brunei", + "Brunei Darussalam", "Benin", "Singapore", "Turkey", @@ -558,19 +553,18 @@ "Vanuatu", "Djibouti", "Malaysia", - "Syria", + "Syrian Arab Republic", "Maldives", "Mali", "Eritrea", "Algeria", "Iran", "Oman", - "Brunei", "Morocco", "Jordan", "Bhutan", "Guinea", - "Burma", + "Myanmar", "Afghanistan", "Senegal", "Indonesia", @@ -590,7 +584,7 @@ ], "text": [ "Belarus", - "Moldova", + "Moldova, Republic of", "Lithuania", "Russia", "Romania", @@ -658,7 +652,7 @@ "Georgia", "Uruguay", "Angola", - "Laos", + "Lao People's Democratic Republic", "Japan", "Mexico", "Ecuador", @@ -678,7 +672,7 @@ "Italy", "Trinidad and Tobago", "China", - "Macedonia", + "The Republic of North Macedonia", "Saint Vincent and the Grenadines", "Equatorial Guinea", "Suriname", @@ -689,12 +683,11 @@ "Colombia", "Ivory Coast", "Bolivia", - "Swaziland", + "Eswatini", "Zimbabwe", "Seychelles", "Cambodia", "Puerto Rico", - "Netherlands Antilles", "Philippines", "Costa Rica", "Armenia", @@ -724,7 +717,7 @@ "Democratic Republic of the Congo", "Nauru", "Gambia", - "Federated States of Micronesia", + "Micronesia, Federated States of", "El Salvador", "Fiji", "Papua New Guinea", @@ -738,7 +731,7 @@ "Mozambique", "Togo", "Nepal", - "Brunei", + "Brunei Darussalam", "Benin", "Singapore", "Turkey", @@ -751,19 +744,18 @@ "Vanuatu", "Djibouti", "Malaysia", - "Syria", + "Syrian Arab Republic", "Maldives", "Mali", "Eritrea", "Algeria", "Iran", "Oman", - "Brunei", "Morocco", "Jordan", "Bhutan", "Guinea", - "Burma", + "Myanmar", "Afghanistan", "Senegal", "Indonesia", diff --git a/test/image/mocks/geo_country-names.json b/test/image/mocks/geo_country-names.json index 43ab38d3728..01a218ba2b9 100644 --- a/test/image/mocks/geo_country-names.json +++ b/test/image/mocks/geo_country-names.json @@ -5,7 +5,7 @@ "locationmode": "country names", "locations": [ "Belarus", - "Moldova", + "Moldova, Republic of", "Lithuania", "Russia", "Romania", @@ -73,7 +73,7 @@ "Georgia", "Uruguay", "Angola", - "Laos", + "Lao People's Democratic Republic", "Japan", "Mexico", "Ecuador", @@ -93,7 +93,7 @@ "Italy", "Trinidad and Tobago", "China", - "Macedonia", + "The Republic of North Macedonia", "Saint Vincent and the Grenadines", "Equatorial Guinea", "Suriname", @@ -104,12 +104,11 @@ "Colombia", "Ivory Coast", "Bolivia", - "Swaziland", + "Eswatini", "Zimbabwe", "Seychelles", "Cambodia", "Puerto Rico", - "Netherlands Antilles", "Philippines", "Costa Rica", "Armenia", @@ -139,7 +138,7 @@ "Democratic Republic of the Congo", "Nauru", "Gambia", - "Federated States of Micronesia", + "Micronesia, Federated States of", "El Salvador", "Fiji", "Papua New Guinea", @@ -153,7 +152,7 @@ "Mozambique", "Togo", "Nepal", - "Brunei", + "Brunei Darussalam", "Benin", "Singapore", "Turkey", @@ -166,19 +165,18 @@ "Vanuatu", "Djibouti", "Malaysia", - "Syria", + "Syrian Arab Republic", "Maldives", "Mali", "Eritrea", "Algeria", "Iran", "Oman", - "Brunei", "Morocco", "Jordan", "Bhutan", "Guinea", - "Burma", + "Myanmar", "Afghanistan", "Senegal", "Indonesia", @@ -304,7 +302,6 @@ "5.4", "5.4", "5.4", - "5.4", "5.3", "5.2", "5", @@ -367,7 +364,6 @@ "1", "0.9", "0.9", - "0.9", "0.7", "0.7", "0.7", @@ -409,7 +405,7 @@ "locationmode": "country names", "locations": [ "Belarus", - "Moldova", + "Moldova, Republic of", "Lithuania", "Russia", "Romania", @@ -477,7 +473,7 @@ "Georgia", "Uruguay", "Angola", - "Laos", + "Lao People's Democratic Republic", "Japan", "Mexico", "Ecuador", @@ -497,7 +493,7 @@ "Italy", "Trinidad and Tobago", "China", - "Macedonia", + "The Republic of North Macedonia", "Saint Vincent and the Grenadines", "Equatorial Guinea", "Suriname", @@ -508,12 +504,11 @@ "Colombia", "Ivory Coast", "Bolivia", - "Swaziland", + "Eswatini", "Zimbabwe", "Seychelles", "Cambodia", "Puerto Rico", - "Netherlands Antilles", "Philippines", "Costa Rica", "Armenia", @@ -543,7 +538,7 @@ "Democratic Republic of the Congo", "Nauru", "Gambia", - "Federated States of Micronesia", + "Micronesia, Federated States of", "El Salvador", "Fiji", "Papua New Guinea", @@ -557,7 +552,7 @@ "Mozambique", "Togo", "Nepal", - "Brunei", + "Brunei Darussalam", "Benin", "Singapore", "Turkey", @@ -570,19 +565,18 @@ "Vanuatu", "Djibouti", "Malaysia", - "Syria", + "Syrian Arab Republic", "Maldives", "Mali", "Eritrea", "Algeria", "Iran", "Oman", - "Brunei", "Morocco", "Jordan", "Bhutan", "Guinea", - "Burma", + "Myanmar", "Afghanistan", "Senegal", "Indonesia", @@ -709,7 +703,6 @@ "5.4", "5.4", "5.4", - "5.4", "5.3", "5.2", "5", @@ -772,7 +765,6 @@ "1", "0.9", "0.9", - "0.9", "0.7", "0.7", "0.7", @@ -911,7 +903,6 @@ "5.4", "5.4", "5.4", - "5.4", "5.3", "5.2", "5", @@ -974,7 +965,6 @@ "1", "0.9", "0.9", - "0.9", "0.7", "0.7", "0.7", diff --git a/test/jasmine/tests/geo_test.js b/test/jasmine/tests/geo_test.js index ee512d56b64..fd76a2120c3 100644 --- a/test/jasmine/tests/geo_test.js +++ b/test/jasmine/tests/geo_test.js @@ -817,8 +817,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) {
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: