@@ -11,13 +11,14 @@ var loggers = require('./loggers');
1111var isPlainObject = require ( './is_plain_object' ) ;
1212var nestedProperty = require ( './nested_property' ) ;
1313var polygon = require ( './polygon' ) ;
14+ const { usaLocationAbbreviations, usaLocationList } = require ( './usa_location_names' ) ;
1415
1516// make list of all country iso3 ids from at runtime
1617var countryIds = Object . keys ( countryRegex ) ;
1718
1819var locationmodeToIdFinder = {
1920 'ISO-3' : identity ,
20- 'USA-states' : identity ,
21+ 'USA-states' : usaLocationToAbbreviation ,
2122 'country names' : countryNameToISO3
2223} ;
2324
@@ -34,14 +35,25 @@ function countryNameToISO3(countryName) {
3435 return false ;
3536}
3637
38+ function usaLocationToAbbreviation ( loc ) {
39+ loc = loc . trim ( ) ;
40+ const abbreviation = usaLocationAbbreviations . has ( loc . toUpperCase ( ) )
41+ ? loc . toUpperCase ( )
42+ : usaLocationList [ loc . toLowerCase ( ) ] ;
43+
44+ if ( abbreviation ) return abbreviation ;
45+
46+ loggers . log ( 'Unrecognized US location: ' + loc + '.' ) ;
47+
48+ return false ;
49+ }
50+
3751function locationToFeature ( locationmode , location , features ) {
3852 if ( ! location || typeof location !== 'string' ) return false ;
3953
40- var locationId = locationmodeToIdFinder [ locationmode ] ( location ) ;
41- var filteredFeatures ;
42- var f , i ;
43-
54+ const locationId = locationmodeToIdFinder [ locationmode ] ( location ) ;
4455 if ( locationId ) {
56+ let filteredFeatures ;
4557 if ( locationmode === 'USA-states' ) {
4658 // Filter out features out in USA
4759 //
@@ -50,24 +62,18 @@ function locationToFeature(locationmode, location, features) {
5062 // which have some overlay in their two-letter ids. For example,
5163 // 'WA' is used for both Washington state and Western Australia.
5264 filteredFeatures = [ ] ;
53- for ( i = 0 ; i < features . length ; i ++ ) {
54- f = features [ i ] ;
55- if ( f . properties && f . properties . gu && f . properties . gu === 'USA' ) {
56- filteredFeatures . push ( f ) ;
57- }
65+ for ( const f of features ) {
66+ if ( f ?. properties ?. gu === 'USA' ) filteredFeatures . push ( f ) ;
5867 }
5968 } else {
6069 filteredFeatures = features ;
6170 }
6271
63- for ( i = 0 ; i < filteredFeatures . length ; i ++ ) {
64- f = filteredFeatures [ i ] ;
72+ for ( const f of filteredFeatures ) {
6573 if ( f . id === locationId ) return f ;
6674 }
6775
68- loggers . log (
69- [ 'Location with id' , locationId , 'does not have a matching topojson feature at this resolution.' ] . join ( ' ' )
70- ) ;
76+ loggers . log ( `Location with id ${ locationId } does not have a matching topojson feature at this resolution.` ) ;
7177 }
7278
7379 return false ;
0 commit comments