Skip to content

Commit 7a0d61e

Browse files
authored
Improve map package documentation and performance optimizations (#2276)
* Add comprehensive JSDoc comments to supported-geos.js geographic lookup tables * Document state, region, country, territory, city, and county data structures * Export pre-computed key arrays for performance optimization * Optimize displayGeoName.ts by importing pre-computed key arrays * Optimize addUIDs.ts by using imported key arrays instead of Object.keys() calls * Optimize formatLegendLocation.ts with pre-computed county key set * Update index.html to use single-state example configuration * Improve code readability with better boolean comparison in displayGeoName.ts
1 parent 16665e2 commit 7a0d61e

File tree

4 files changed

+205
-19
lines changed

4 files changed

+205
-19
lines changed

packages/map/src/data/supported-geos.js

Lines changed: 185 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
import supportedCountiesJSON from './supported-counties.json'
22

3+
/**
4+
* US States Lookup Table
5+
*
6+
* Maps ISO 3166-2 state codes to arrays of supported name variations.
7+
* Used for normalizing US state data and converting between different naming formats.
8+
*
9+
* Structure: { 'US-XX': ['FULL_NAME', 'XX'] }
10+
* - Key: ISO 3166-2 state code (e.g., 'US-CA')
11+
* - Value: [0] = Full uppercase state name, [1] = Two-letter abbreviation
12+
*
13+
* Used in:
14+
* - addUIDs.ts: Match user state data to standardized UIDs
15+
* - displayGeoName.ts: Convert state codes to display names
16+
* - UsaMap.State.tsx: State-level map rendering
17+
* - EditorPanel.tsx: Geography selection interface
18+
*/
319
export const supportedStates = {
420
// States
521
'US-AL': ['ALABAMA', 'AL'],
@@ -55,6 +71,20 @@ export const supportedStates = {
5571
'US-WY': ['WYOMING', 'WY']
5672
}
5773

74+
/**
75+
* US Regions Lookup Table
76+
*
77+
* Maps US federal regions (HHS regions) to supported name variations.
78+
* Used for regional-level data visualization and aggregation.
79+
*
80+
* Structure: { 'region N': ['REGION N', 'RN'] }
81+
* - Key: Lowercase region identifier
82+
* - Value: [0] = Uppercase region name, [1] = Region abbreviation
83+
*
84+
* Used in:
85+
* - addUIDs.ts: Match regional data to region identifiers
86+
* - Regional map visualizations
87+
*/
5888
export const supportedRegions = {
5989
'region 1': ['REGION 1', 'R1'],
6090
'region 2': ['REGION 2', 'R2'],
@@ -68,6 +98,20 @@ export const supportedRegions = {
6898
'region 10': ['REGION 10', 'R10']
6999
}
70100

101+
/**
102+
* State Name to ISO Code Mapping
103+
*
104+
* Maps proper case state names to their corresponding ISO 3166-2 codes.
105+
* Provides reverse lookup capability for the supportedStates table.
106+
*
107+
* Structure: { 'State Name': 'US-XX' }
108+
* - Key: Proper case state name (e.g., 'California')
109+
* - Value: ISO 3166-2 state code (e.g., 'US-CA')
110+
*
111+
* Used in:
112+
* - Data processing when state names need to be converted to ISO codes
113+
* - Validation and normalization of state data
114+
*/
71115
export const stateToIso = {
72116
// States
73117
Alabama: 'US-AL',
@@ -122,6 +166,22 @@ export const stateToIso = {
122166
Wyoming: 'US-WY'
123167
}
124168

169+
/**
170+
* State FIPS Code to Two-Letter Abbreviation Mapping
171+
*
172+
* Maps FIPS (Federal Information Processing Standards) state codes to two-letter abbreviations.
173+
* FIPS codes are numeric identifiers used by the US Census Bureau and other federal agencies.
174+
*
175+
* Structure: { 'XX': 'YY' } or { XX: 'YY' }
176+
* - Key: FIPS code as string or number (e.g., '06' or 6)
177+
* - Value: Two-letter state/territory abbreviation (e.g., 'CA')
178+
*
179+
* Note: Includes US territories (AS, GU, MP, PR, VI) with FIPS codes 60+
180+
*
181+
* Used in:
182+
* - formatLegendLocation.ts: Convert county FIPS codes to state abbreviations
183+
* - County-level data processing where state context is needed
184+
*/
125185
export const stateFipsToTwoDigit = {
126186
['01']: 'AL', // eslint-disable-line
127187
['02']: 'AK', // eslint-disable-line
@@ -181,6 +241,22 @@ export const stateFipsToTwoDigit = {
181241
78: 'VI'
182242
}
183243

244+
/**
245+
* State FIPS Code to Full Name Mapping
246+
*
247+
* Maps FIPS state codes to their full proper case names.
248+
* Comprehensive lookup including all 50 states, DC, and US territories.
249+
*
250+
* Structure: { 'XX': 'Full State Name' } or { XX: 'Full State Name' }
251+
* - Key: FIPS code as string or number
252+
* - Value: Full proper case state/territory name
253+
*
254+
* Used in:
255+
* - useStateZoom.tsx: State identification for zoom functionality
256+
* - getStatesPicked.ts: State selection processing
257+
* - useApplyTooltipsToGeo.tsx: Tooltip content generation
258+
* - EditorPanel.tsx: Geographic selection interfaces
259+
*/
184260
export const supportedStatesFipsCodes = {
185261
'01': 'Alabama',
186262
'02': 'Alaska',
@@ -240,6 +316,28 @@ export const supportedStatesFipsCodes = {
240316
78: 'United States Virgin Islands'
241317
}
242318

319+
/**
320+
* World Countries Lookup Table
321+
*
322+
* Maps ISO 3166-1 alpha-3 country codes to arrays of supported name variations.
323+
* Comprehensive international geography support for world maps.
324+
*
325+
* Structure: { 'XXX': ['Name Variation 1', 'Name Variation 2', ...] }
326+
* - Key: ISO 3166-1 alpha-3 country code (e.g., 'USA', 'GBR', 'CHN')
327+
* - Value: Array of alternative country names and common variations
328+
*
329+
* Features:
330+
* - Multiple name variations per country (official names, common names, abbreviations)
331+
* - Special territories and dependencies included
332+
* - Handles disputed territories and special cases
333+
* - Supports historical and alternative country names
334+
*
335+
* Used in:
336+
* - WorldMap.tsx: International map rendering and interaction
337+
* - addUIDs.ts: Match international geographic data
338+
* - displayGeoName.ts: Convert country codes to display names
339+
* - generateRuntimeLegend.ts: Legend generation for world maps
340+
*/
243341
export const supportedCountries = {
244342
AFG: ['Afghanistan'],
245343
ALA: ['Åland', 'Åland Islands'],
@@ -521,6 +619,25 @@ export const supportedCountries = {
521619
'US-HI': ['Hawaii']
522620
}
523621

622+
/**
623+
* US Territories Lookup Table
624+
*
625+
* Maps US territory identifiers to arrays of supported name variations.
626+
* Covers US territories, freely associated states, and commonwealths.
627+
*
628+
* Structure: { 'US-XX': ['FULL_NAME', 'XX', ...] }
629+
* - Key: ISO-style territory identifier (following US-XX pattern)
630+
* - Value: [0] = Full uppercase territory name, [1] = Abbreviation, [...] = Variations
631+
*
632+
* Note: Some keys (US-FM, US-PW, US-MH) are not official ISO codes but follow
633+
* the pattern for consistency with the Freely Associated States.
634+
*
635+
* Used in:
636+
* - addUIDs.ts: Match territory data to identifiers
637+
* - displayGeoName.ts: Convert territory codes to display names
638+
* - UsaMap.Region.tsx: Territory rendering on regional maps
639+
* - UsaMap.State.tsx: Territory handling in state-level maps
640+
*/
524641
export const supportedTerritories = {
525642
'US-AS': ['AMERICAN SAMOA', 'AS'],
526643
'US-GU': ['GUAM', 'GU'],
@@ -532,8 +649,31 @@ export const supportedTerritories = {
532649
'US-MH': ['MARSHALL ISLANDS', 'MH', 'RMI'] // Note: Key is not an official ISO code
533650
}
534651

535-
// ! INFO: coordinates are [negative longitude, latitude]
536-
// ie. Albuquerque coordinates are actually [35, 106]
652+
/**
653+
* US Cities Coordinate Lookup Table
654+
*
655+
* Maps city names to their geographic coordinates for location-based mapping.
656+
* Includes cities, tribal health organizations, and other geographic entities.
657+
*
658+
* Structure: { 'CITY_NAME': [longitude, latitude] }
659+
* - Key: Uppercase city name, often with state (e.g., 'ALBANY, NEW YORK')
660+
* - Value: [longitude, latitude] coordinate pair
661+
*
662+
* Coordinate Format:
663+
* - Longitude: Negative values for locations west of Prime Meridian
664+
* - Latitude: Positive values for locations north of Equator
665+
* - Format: [longitude, latitude] (NOTE: longitude comes first, opposite of lat/lng)
666+
*
667+
* Special Entries:
668+
* - Tribal health organizations and consortiums
669+
* - Multi-state metropolitan areas
670+
* - Federal districts and territories
671+
*
672+
* Used in:
673+
* - addUIDs.ts: Match city-based geographic data
674+
* - CityList.tsx: City selection and display functionality
675+
* - Location-based map features requiring precise coordinates
676+
*/
537677
// prettier-ignore
538678
export const supportedCities = {
539679
'ALASKA NATIVE TRIBAL HEALTH CONSORTIUM': [-149.8067, 61.1827],
@@ -724,4 +864,47 @@ export const supportedCities = {
724864
'YUKON-KUSKOKWIM HEALTH CORPORATION': [-161.7849, 60.7881]
725865
};
726866

867+
/**
868+
* US Counties Lookup Table
869+
*
870+
* Maps 5-digit FIPS county codes to county names.
871+
* Imported from external JSON file for maintainability and size management.
872+
*
873+
* Structure: { 'SSCCC': 'County Name' }
874+
* - Key: 5-digit FIPS code where SS = state FIPS, CCC = county FIPS
875+
* - Value: County name in proper case
876+
*
877+
* Coverage:
878+
* - All 3,143+ US counties and county-equivalents
879+
* - Includes parishes (Louisiana), boroughs (Alaska), independent cities (Virginia)
880+
* - Census areas, municipalities, and other county-level entities
881+
*
882+
* Used in:
883+
* - addUIDs.ts: Match county FIPS codes to identifiers
884+
* - formatLegendLocation.ts: Display county names in legends with state context
885+
* - displayGeoName.ts: Convert county codes to display names
886+
* - County-level map rendering and data processing
887+
*/
727888
export const supportedCounties = supportedCountiesJSON
889+
890+
/**
891+
* Pre-computed Key Arrays
892+
*
893+
* Performance optimization: Pre-compute key arrays to avoid repeated Object.keys() calls
894+
* in performance-critical functions like displayGeoName() and addUIDs().
895+
*
896+
* Used by:
897+
* - displayGeoName.ts: Geographic name resolution
898+
* - addUIDs.ts: Geographic data processing
899+
* - generateRuntimeLegend.ts: Legend generation
900+
* - formatLegendLocation.ts: Legend formatting
901+
*/
902+
export const stateKeys = Object.keys(supportedStates)
903+
export const territoryKeys = Object.keys(supportedTerritories)
904+
export const regionKeys = Object.keys(supportedRegions)
905+
export const countryKeys = Object.keys(supportedCountries)
906+
export const countyKeys = Object.keys(supportedCounties)
907+
export const cityKeys = Object.keys(supportedCities)
908+
909+
// Pre-computed Sets for O(1) lookup performance
910+
export const countyKeySet = new Set(countyKeys)

packages/map/src/helpers/addUIDs.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,19 @@ import {
44
supportedCountries,
55
supportedRegions,
66
supportedStates,
7-
supportedTerritories
7+
supportedTerritories,
8+
stateKeys,
9+
territoryKeys,
10+
regionKeys,
11+
countryKeys,
12+
countyKeys,
13+
cityKeys
814
} from './../data/supported-geos'
915

1016
import { SUPPORTED_DC_NAMES, GEO_TYPES, GEOCODE_TYPES } from './constants'
1117
import { DataRow, MapConfig } from '../types/MapConfig'
1218

13-
// Data props
14-
const stateKeys = Object.keys(supportedStates)
15-
const territoryKeys = Object.keys(supportedTerritories)
16-
const regionKeys = Object.keys(supportedRegions)
17-
const countryKeys = Object.keys(supportedCountries)
18-
const countyKeys = Object.keys(supportedCounties)
19-
const cityKeys = Object.keys(supportedCities)
19+
// Note: Key arrays are now imported from supported-geos for better performance
2020

2121
const geoLookups: Record<string, GeoLookup> = {
2222
state: { keys: stateKeys, data: supportedStates },

packages/map/src/helpers/displayGeoName.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
import { titleCase } from './titleCase'
2-
import { supportedStates, supportedTerritories, supportedCountries, supportedCounties } from '../data/supported-geos'
2+
import {
3+
supportedStates,
4+
supportedTerritories,
5+
supportedCountries,
6+
supportedCounties,
7+
stateKeys,
8+
territoryKeys,
9+
countryKeys,
10+
countyKeys
11+
} from '../data/supported-geos'
312

413
/**
514
* Converts a geographic key to its display name.
@@ -10,10 +19,6 @@ import { supportedStates, supportedTerritories, supportedCountries, supportedCou
1019
*/
1120
export const displayGeoName = (key: string, convertFipsCodes = true): string => {
1221
if (!convertFipsCodes) return key
13-
const stateKeys = Object.keys(supportedStates)
14-
const territoryKeys = Object.keys(supportedTerritories)
15-
const countryKeys = Object.keys(supportedCountries)
16-
const countyKeys = Object.keys(supportedCounties)
1722
let value = key
1823

1924
// Map to first item in values array which is the preferred label
@@ -44,7 +49,7 @@ export const displayGeoName = (key: string, convertFipsCodes = true): string =>
4449
Congo: 'Republic of the Congo'
4550
}
4651

47-
if (true === Object.keys(dict).includes(value)) {
52+
if (Object.keys(dict).includes(value)) {
4853
value = dict[value]
4954
}
5055
// if you get here and it's 2 letters then DONT titleCase state abbreviations like "AL"

packages/map/src/helpers/formatLegendLocation.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
import { stateFipsToTwoDigit, supportedCounties } from '../data/supported-geos'
1+
import { stateFipsToTwoDigit, supportedCounties, countyKeySet } from '../data/supported-geos'
22
import { titleCase } from './titleCase'
33

4-
const countyKeySet = new Set(Object.keys(supportedCounties))
5-
64
export const formatLegendLocation = (key, runtimeLookup) => {
75
let formattedName = ''
86

0 commit comments

Comments
 (0)