Skip to content

Commit fa33793

Browse files
authored
Merge pull request #7856 from plotly/cam/7026/replace-country-regex
feat: Replace country-regex with country-iso-search
2 parents 401e72c + 48cc7b8 commit fa33793

11 files changed

Lines changed: 77 additions & 46 deletions

File tree

draftlogs/7856_add.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Replace `country-regex` with `country-iso-search` to search for country names in choropleth, scattergeo traces [[#7856](https://github.com/plotly/plotly.js/pull/7856)]

package-lock.json

Lines changed: 9 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@
8383
"color": "^5.0.0",
8484
"color-normalize": "1.5.0",
8585
"color-rgba": "3.0.0",
86-
"country-regex": "^1.1.0",
86+
"country-iso-search": "^0.1.1",
8787
"d3-force": "^1.2.1",
8888
"d3-format": "^1.4.5",
8989
"d3-geo": "^1.12.1",

src/lib/custom_country_codes.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import type { CountryRecord } from 'country-iso-search';
2+
3+
/**
4+
* Plotly-specific country records for disputed and unrecognized territories
5+
* that are not part of ISO 3166-1. Each entry uses an ISO3-like code in the
6+
* `X` range (reserved by ISO 3166-1 for user-assigned codes) so these regions
7+
* can be looked up alongside standard countries from `country-iso-search`.
8+
*/
9+
export const COUNTRIES_X: ReadonlyArray<CountryRecord> = [
10+
{
11+
iso3: 'XAC',
12+
iso2: '',
13+
m49: '',
14+
name: 'Aksai Chin',
15+
aliases: []
16+
},
17+
{
18+
iso3: 'XAP',
19+
iso2: '',
20+
m49: '',
21+
name: 'Arunachal Pradesh',
22+
aliases: []
23+
},
24+
{
25+
iso3: 'XBT',
26+
iso2: '',
27+
m49: '',
28+
name: 'Bir Tawil',
29+
aliases: []
30+
},
31+
{
32+
iso3: 'XHT',
33+
iso2: '',
34+
m49: '',
35+
name: 'Halaib Triangle',
36+
aliases: []
37+
},
38+
{
39+
iso3: 'XJK',
40+
iso2: '',
41+
m49: '',
42+
name: 'Jammu and Kashmir',
43+
aliases: []
44+
}
45+
];

src/lib/geo_location_utils.js

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
var d3 = require('@plotly/d3');
4-
var countryRegex = require('country-regex');
4+
const { COUNTRIES, createLookup } = require('country-iso-search');
55
var { area: turfArea } = require('@turf/area');
66
var { centroid: turfCentroid } = require('@turf/centroid');
77
var { bbox: turfBbox } = require('@turf/bbox');
@@ -12,9 +12,9 @@ var isPlainObject = require('./is_plain_object');
1212
var nestedProperty = require('./nested_property');
1313
var polygon = require('./polygon');
1414
const { usaLocationAbbreviations, usaLocationList } = require('./usa_location_names');
15+
const { COUNTRIES_X } = require('./custom_country_codes');
1516

16-
// make list of all country iso3 ids from at runtime
17-
var countryIds = Object.keys(countryRegex);
17+
const { lookupAlpha3 } = createLookup([...COUNTRIES, ...COUNTRIES_X]);
1818

1919
var locationmodeToIdFinder = {
2020
'ISO-3': identity,
@@ -23,13 +23,8 @@ var locationmodeToIdFinder = {
2323
};
2424

2525
function countryNameToISO3(countryName) {
26-
for (var i = 0; i < countryIds.length; i++) {
27-
var iso3 = countryIds[i];
28-
var regex = new RegExp(countryRegex[iso3]);
29-
30-
if (regex.test(countryName.trim().toLowerCase())) return iso3;
31-
}
32-
26+
const iso3 = lookupAlpha3(countryName);
27+
if (iso3) return iso3;
3328
loggers.log('Unrecognized country name: ' + countryName + '.');
3429

3530
return false;

src/traces/choropleth/defaults.js

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,6 @@ var Lib = require('../../lib');
44
var colorscaleDefaults = require('../../components/colorscale/defaults');
55
var attributes = require('./attributes');
66

7-
const locationmodeBreakingChangeWarning = [
8-
'The library used by the *country names* `locationmode` option is changing in the next major version.',
9-
'Some country names in existing plots may not work in the new version.',
10-
'To ensure consistent behavior, consider setting `locationmode` to *ISO-3*.'
11-
].join(' ');
12-
137
module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout) {
148
function coerce(attr, dflt) {
159
return Lib.coerce(traceIn, traceOut, attributes, attr, dflt);
@@ -34,10 +28,6 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
3428

3529
var locationMode = coerce('locationmode', locationmodeDflt);
3630

37-
if (locationMode === 'country names') {
38-
Lib.warn(locationmodeBreakingChangeWarning);
39-
}
40-
4131
if (locationMode === 'geojson-id') {
4232
coerce('featureidkey');
4333
}

src/traces/scattergeo/attributes.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,6 @@ var scatterMarkerAttrs = scatterAttrs.marker;
1414
var scatterLineAttrs = scatterAttrs.line;
1515
var scatterMarkerLineAttrs = scatterMarkerAttrs.line;
1616

17-
const breakingChangeWarning = [
18-
'The library used by the *country names* `locationmode` option is changing in an upcoming version.',
19-
'Country names in existing plots may not work in the new version.'
20-
].join(' ');
21-
2217
module.exports = overrideAll(
2318
{
2419
lon: {
@@ -43,7 +38,6 @@ module.exports = overrideAll(
4338
values: ['ISO-3', 'USA-states', 'country names', 'geojson-id'],
4439
dflt: 'ISO-3',
4540
description: [
46-
breakingChangeWarning,
4741
'Determines the set of locations used to match entries in `locations`',
4842
'to regions on the map.',
4943
'Values *ISO-3*, *USA-states*, *country names* correspond to features on',

src/traces/scattergeo/defaults.js

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,6 @@ var handleFillColorDefaults = require('../scatter/fillcolor_defaults');
1010

1111
var attributes = require('./attributes');
1212

13-
const locationmodeBreakingChangeWarning = [
14-
'The library used by the *country names* `locationmode` option is changing in the next major version.',
15-
'Some country names in existing plots may not work in the new version.',
16-
'To ensure consistent behavior, consider setting `locationmode` to *ISO-3*.'
17-
].join(' ');
18-
1913
module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout) {
2014
function coerce(attr, dflt) {
2115
return Lib.coerce(traceIn, traceOut, attributes, attr, dflt);
@@ -33,10 +27,6 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
3327

3428
var locationMode = coerce('locationmode', locationmodeDflt);
3529

36-
if (locationMode === 'country names') {
37-
Lib.warn(locationmodeBreakingChangeWarning);
38-
}
39-
4030
if (locationMode === 'geojson-id') {
4131
coerce('featureidkey');
4232
}

src/types/generated/schema.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2346,7 +2346,7 @@ export interface ChoroplethData {
23462346
*/
23472347
legendwidth?: number;
23482348
/**
2349-
* The library used by the *country names* `locationmode` option is changing in an upcoming version. Country names in existing plots may not work in the new version. Determines the set of locations used to match entries in `locations` to regions on the map. Values *ISO-3*, *USA-states*, *country names* correspond to features on the base map and value *geojson-id* corresponds to features from a custom GeoJSON linked to the `geojson` attribute. *USA-states* accepts both two-letter abbreviations (e.g. *CA*) and full state names (e.g. *California*).
2349+
* Determines the set of locations used to match entries in `locations` to regions on the map. Values *ISO-3*, *USA-states*, *country names* correspond to features on the base map and value *geojson-id* corresponds to features from a custom GeoJSON linked to the `geojson` attribute. *USA-states* accepts both two-letter abbreviations (e.g. *CA*) and full state names (e.g. *California*).
23502350
* @default 'ISO-3'
23512351
*/
23522352
locationmode?: 'ISO-3' | 'USA-states' | 'country names' | 'geojson-id';
@@ -8240,7 +8240,7 @@ export interface ScattergeoData {
82408240
width?: number;
82418241
};
82428242
/**
8243-
* The library used by the *country names* `locationmode` option is changing in an upcoming version. Country names in existing plots may not work in the new version. Determines the set of locations used to match entries in `locations` to regions on the map. Values *ISO-3*, *USA-states*, *country names* correspond to features on the base map and value *geojson-id* corresponds to features from a custom GeoJSON linked to the `geojson` attribute. *USA-states* accepts both two-letter abbreviations (e.g. *CA*) and full state names (e.g. *California*).
8243+
* Determines the set of locations used to match entries in `locations` to regions on the map. Values *ISO-3*, *USA-states*, *country names* correspond to features on the base map and value *geojson-id* corresponds to features from a custom GeoJSON linked to the `geojson` attribute. *USA-states* accepts both two-letter abbreviations (e.g. *CA*) and full state names (e.g. *California*).
82448244
* @default 'ISO-3'
82458245
*/
82468246
locationmode?: 'ISO-3' | 'USA-states' | 'country names' | 'geojson-id';

test/jasmine/tests/geo_test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -886,6 +886,18 @@ describe('geojson / topojson utils', function() {
886886

887887
expect(out).toEqual(false);
888888
});
889+
890+
it('with *country names* locationmode and an ISO 3166-1 short-name suffix', () => {
891+
const out = _locationToFeature(topojson, 'Korea, Republic of', 'country names');
892+
893+
expect(out.id).toEqual('KOR');
894+
});
895+
896+
it('with *country names* locationmode and a custom country code', () => {
897+
const out = _locationToFeature(topojson, 'Aksai Chin', 'country names');
898+
899+
expect(out.id).toEqual('XAC');
900+
});
889901
});
890902

891903
describe('should distinguish between US and US Virgin Island', function() {

0 commit comments

Comments
 (0)