From e803c5e487e185b6444e6afac9defe2a43a16b81 Mon Sep 17 00:00:00 2001 From: Ivajlo Dimitrov Date: Thu, 13 Feb 2025 16:55:10 +0200 Subject: [PATCH 1/8] replace country-regex with i18n-iso-countries --- package-lock.json | 23 +++++++++++++++++------ package.json | 2 +- src/lib/geo_location_utils.js | 16 ++++++++-------- test/jasmine/tests/geo_test.js | 4 ++-- 4 files changed, 28 insertions(+), 17 deletions(-) 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) { From a31d3602b7d0d5b380b3a226d03fbc30fda70b2b Mon Sep 17 00:00:00 2001 From: Ivajlo Dimitrov Date: Thu, 13 Feb 2025 17:40:24 +0200 Subject: [PATCH 2/8] add draftlog readme --- draftlogs/7366_change.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 draftlogs/7366_change.md 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 From 238ac94b503376642da5a7c915034cef78a0cfcb Mon Sep 17 00:00:00 2001 From: Cameron DeCoster Date: Fri, 1 Aug 2025 16:05:08 -0600 Subject: [PATCH 3/8] Update per PR feedback --- draftlogs/7366_change.md | 2 +- src/lib/geo_location_utils.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/draftlogs/7366_change.md b/draftlogs/7366_change.md index 90205f72986..1107ad9b893 100644 --- a/draftlogs/7366_change.md +++ b/draftlogs/7366_change.md @@ -1 +1 @@ - - Replace country-regex with i18n-iso-countries [[#7366](https://github.com/plotly/plotly.js/pull/7366)] \ No newline at end of file + - Replace country-regex package with i18n-iso-countries [[#7366](https://github.com/plotly/plotly.js/pull/7366)] diff --git a/src/lib/geo_location_utils.js b/src/lib/geo_location_utils.js index 35e3e77553d..43e13d44136 100644 --- a/src/lib/geo_location_utils.js +++ b/src/lib/geo_location_utils.js @@ -4,6 +4,7 @@ var d3 = require('@plotly/d3'); 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'); @@ -11,7 +12,6 @@ var isPlainObject = require('./is_plain_object'); var nestedProperty = require('./nested_property'); var polygon = require('./polygon'); -const countries = require("i18n-iso-countries"); countries.registerLocale(require("i18n-iso-countries/langs/en.json")); var locationmodeToIdFinder = { From 767412cf87bd348f2c216748a9e0d3cb496fc5ba Mon Sep 17 00:00:00 2001 From: Cameron DeCoster Date: Fri, 1 Aug 2025 16:09:10 -0600 Subject: [PATCH 4/8] Fix incorrect merge conflict resolution --- package-lock.json | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/package-lock.json b/package-lock.json index ae467b0bda3..0153a05a200 100644 --- a/package-lock.json +++ b/package-lock.json @@ -22,7 +22,7 @@ "color-alpha": "1.0.4", "color-normalize": "1.5.0", "color-parse": "2.0.0", - "color-rgba": "2.1.1", + "color-rgba": "3.0.0", "d3-force": "^1.2.1", "d3-format": "^1.4.5", "d3-geo": "^1.12.1", diff --git a/package.json b/package.json index bb98d817859..9cdf03a8c43 100644 --- a/package.json +++ b/package.json @@ -80,7 +80,7 @@ "color-alpha": "1.0.4", "color-normalize": "1.5.0", "color-parse": "2.0.0", - "color-rgba": "2.1.1", + "color-rgba": "3.0.0", "d3-force": "^1.2.1", "d3-format": "^1.4.5", "d3-geo": "^1.12.1", From c5693133983be6b52581813f72fbff8c87dcb078 Mon Sep 17 00:00:00 2001 From: Cameron DeCoster Date: Mon, 4 Aug 2025 15:52:13 -0600 Subject: [PATCH 5/8] Update Browserify adapter transform function to pass JSON through --- tasks/compress_attributes.js | 46 ++++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 18 deletions(-) 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(); + } + ); }; From 86002d2f21c32caa92631ad312b3095ea5eda310 Mon Sep 17 00:00:00 2001 From: Cameron DeCoster Date: Tue, 5 Aug 2025 15:58:33 -0600 Subject: [PATCH 6/8] Update country names in test mock --- test/image/mocks/geo_country-names.json | 36 +++++++++++-------------- 1 file changed, 16 insertions(+), 20 deletions(-) diff --git a/test/image/mocks/geo_country-names.json b/test/image/mocks/geo_country-names.json index 43ab38d3728..c682eb3d879 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", @@ -409,7 +407,7 @@ "locationmode": "country names", "locations": [ "Belarus", - "Moldova", + "Moldova, Republic of", "Lithuania", "Russia", "Romania", @@ -477,7 +475,7 @@ "Georgia", "Uruguay", "Angola", - "Laos", + "Lao People's Democratic Republic", "Japan", "Mexico", "Ecuador", @@ -497,7 +495,7 @@ "Italy", "Trinidad and Tobago", "China", - "Macedonia", + "The Republic of North Macedonia", "Saint Vincent and the Grenadines", "Equatorial Guinea", "Suriname", @@ -508,12 +506,11 @@ "Colombia", "Ivory Coast", "Bolivia", - "Swaziland", + "Eswatini", "Zimbabwe", "Seychelles", "Cambodia", "Puerto Rico", - "Netherlands Antilles", "Philippines", "Costa Rica", "Armenia", @@ -543,7 +540,7 @@ "Democratic Republic of the Congo", "Nauru", "Gambia", - "Federated States of Micronesia", + "Micronesia, Federated States of", "El Salvador", "Fiji", "Papua New Guinea", @@ -557,7 +554,7 @@ "Mozambique", "Togo", "Nepal", - "Brunei", + "Brunei Darussalam", "Benin", "Singapore", "Turkey", @@ -570,19 +567,18 @@ "Vanuatu", "Djibouti", "Malaysia", - "Syria", + "Syrian Arab Republic", "Maldives", "Mali", "Eritrea", "Algeria", "Iran", "Oman", - "Brunei", "Morocco", "Jordan", "Bhutan", "Guinea", - "Burma", + "Myanmar", "Afghanistan", "Senegal", "Indonesia", From d33e9c9e39f97fcbc176e5bbd5711ae0e7e80c8a Mon Sep 17 00:00:00 2001 From: Cameron DeCoster Date: Wed, 6 Aug 2025 10:21:37 -0600 Subject: [PATCH 7/8] Update additional test mock --- .../mocks/geo_country-names-text-chart.json | 72 +++++++++---------- 1 file changed, 32 insertions(+), 40 deletions(-) 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", From 49fac99f7df89ee7298a00f2ec4a42f6094bac6e Mon Sep 17 00:00:00 2001 From: Cameron DeCoster Date: Wed, 6 Aug 2025 14:38:03 -0600 Subject: [PATCH 8/8] Update mock data per previous changes --- test/image/mocks/geo_country-names.json | 6 ------ 1 file changed, 6 deletions(-) diff --git a/test/image/mocks/geo_country-names.json b/test/image/mocks/geo_country-names.json index c682eb3d879..01a218ba2b9 100644 --- a/test/image/mocks/geo_country-names.json +++ b/test/image/mocks/geo_country-names.json @@ -302,7 +302,6 @@ "5.4", "5.4", "5.4", - "5.4", "5.3", "5.2", "5", @@ -365,7 +364,6 @@ "1", "0.9", "0.9", - "0.9", "0.7", "0.7", "0.7", @@ -705,7 +703,6 @@ "5.4", "5.4", "5.4", - "5.4", "5.3", "5.2", "5", @@ -768,7 +765,6 @@ "1", "0.9", "0.9", - "0.9", "0.7", "0.7", "0.7", @@ -907,7 +903,6 @@ "5.4", "5.4", "5.4", - "5.4", "5.3", "5.2", "5", @@ -970,7 +965,6 @@ "1", "0.9", "0.9", - "0.9", "0.7", "0.7", "0.7",