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 7 commits 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
1 change: 1 addition & 0 deletions draftlogs/7366_change.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Replace country-regex package with i18n-iso-countries [[#7366](https://github.com/plotly/plotly.js/pull/7366)]
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": "3.0.0",
"country-regex": "^1.1.0",
"d3-force": "^1.2.1",
"d3-format": "^1.4.5",
"d3-geo": "^1.12.1",
Expand All @@ -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",
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,19 +1,18 @@
'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');
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,
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
46 changes: 28 additions & 18 deletions tasks/compress_attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
);
};
36 changes: 16 additions & 20 deletions test/image/mocks/geo_country-names.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"locationmode": "country names",
"locations": [
"Belarus",
"Moldova",
"Moldova, Republic of",
"Lithuania",
"Russia",
"Romania",
Expand Down Expand Up @@ -73,7 +73,7 @@
"Georgia",
"Uruguay",
"Angola",
"Laos",
"Lao People's Democratic Republic",
"Japan",
"Mexico",
"Ecuador",
Expand All @@ -93,7 +93,7 @@
"Italy",
"Trinidad and Tobago",
"China",
"Macedonia",
"The Republic of North Macedonia",
"Saint Vincent and the Grenadines",
"Equatorial Guinea",
"Suriname",
Expand All @@ -104,12 +104,11 @@
"Colombia",
"Ivory Coast",
"Bolivia",
"Swaziland",
"Eswatini",
"Zimbabwe",
"Seychelles",
"Cambodia",
"Puerto Rico",
"Netherlands Antilles",
"Philippines",
"Costa Rica",
"Armenia",
Expand Down Expand Up @@ -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",
Expand All @@ -153,7 +152,7 @@
"Mozambique",
"Togo",
"Nepal",
"Brunei",
"Brunei Darussalam",
"Benin",
"Singapore",
"Turkey",
Expand All @@ -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",
Expand Down Expand Up @@ -409,7 +407,7 @@
"locationmode": "country names",
"locations": [
"Belarus",
"Moldova",
"Moldova, Republic of",
"Lithuania",
"Russia",
"Romania",
Expand Down Expand Up @@ -477,7 +475,7 @@
"Georgia",
"Uruguay",
"Angola",
"Laos",
"Lao People's Democratic Republic",
"Japan",
"Mexico",
"Ecuador",
Expand All @@ -497,7 +495,7 @@
"Italy",
"Trinidad and Tobago",
"China",
"Macedonia",
"The Republic of North Macedonia",
"Saint Vincent and the Grenadines",
"Equatorial Guinea",
"Suriname",
Expand All @@ -508,12 +506,11 @@
"Colombia",
"Ivory Coast",
"Bolivia",
"Swaziland",
"Eswatini",
"Zimbabwe",
"Seychelles",
"Cambodia",
"Puerto Rico",
"Netherlands Antilles",
"Philippines",
"Costa Rica",
"Armenia",
Expand Down Expand Up @@ -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",
Expand All @@ -557,7 +554,7 @@
"Mozambique",
"Togo",
"Nepal",
"Brunei",
"Brunei Darussalam",
"Benin",
"Singapore",
"Turkey",
Expand All @@ -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",
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 @@ -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) {
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

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:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy