Repository navigation
feat: resolve alternative and former country names in getCountryCode - #178
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
🚧 Files skipped from review as they are similar to previous changes (2)
📝 WalkthroughWalkthroughThe country data model now supports optional aliases for selected entries. Country records include common and former names, while Myanmar uses Sequence Diagram(s)sequenceDiagram
participant Caller
participant getCountryCode
participant CountryDataList
Caller->>getCountryCode: countryName
getCountryCode->>CountryDataList: inspect name, native, and alias
CountryDataList-->>getCountryCode: matching iso2 or no match
getCountryCode-->>Caller: iso2 or false
Possibly related PRs
Suggested reviewers: Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Add an optional `alias` field to country data, matched by getCountryCode,
so former ISO short names and common alternates resolve to the current
code instead of breaking on rename:
getCountryCode('Czech Republic') // 'CZ'
getCountryCode('Turkey') // 'TR'
getCountryCode('Burma') // 'MM'
- Curated aliases for 23 countries: recent ISO renames (Czechia, Cabo
Verde, Turkiye, Eswatini, North Macedonia, Myanmar), common short forms
(UK, USA, UAE), and former single-country names (Persia, Ceylon, Siam).
Adds ~0.6 KB to the core data; no separate module needed.
- Clean the ad-hoc name hack: 'Myanmar (Burma)' -> name 'Myanmar' with
'Burma' as an alias.
- A guard test fails the build if any name, native, or alias ever resolves
to more than one country, so a returned code stays unambiguous.
Refs #35
27ac454 to
c8a55da
Compare
There was a problem hiding this comment.
Pull request overview
This PR addresses a name→code lookup regression by extending getCountryCode(name) to resolve exact, case-insensitive matches against curated alternative/former country names via a new optional alias?: string[] field on country records.
Changes:
- Add optional
alias?: string[]toICountryand populate it for selected countries (including renames and common short forms). - Update
getCountryCode()to match againstname,native, or anyaliasentry (exact, case-insensitive, trimmed). - Add tests for alias resolution and add a “no ambiguous lookup strings” guard across
name/native/alias.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| packages/test-js/getCountryCode.test.ts | Updates expectations to reflect the new alias-backed resolution behavior. |
| packages/test-js/getCountryCode.aliases.test.ts | Adds coverage for alias resolution and introduces an ambiguity-guard test. |
| packages/countries/src/types.ts | Introduces the optional alias?: string[] field on ICountry. |
| packages/countries/src/getCountryCode.ts | Extends matching logic to include alias values in the lookup. |
| packages/countries/src/data/countries.ts | Adds curated alias arrays and normalizes Myanmar’s displayed name. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
The name was shortened to 'Myanmar', which dropped the previously-resolvable (and tested) 'Myanmar (Burma)' string. Add it as an alias so the rename stays backward-compatible for name->code lookups.
Targets 3.4.1 — ships as a patch to fix the 3.4 name→code regression. The historical (ISO 3166-3) + regions sub-modules follow in 3.5.0.
Problem
ISO alpha-2 codes are stable across renames, but
getCountryCode(name)breaks when a country adopts a new ISO short name.getCountryCode('Czech Republic'),'Turkey','Cape Verde'all returnedfalse. This also matters for name-based sources like Discogs ("UK", "USSR"…) and MusicBrainz.Approach — inline
alias, fixes the default lookupAn optional
alias?: string[]onICountry, matched by the coregetCountryCode. So the fix applies to every consumer of the default function — no opt-in, no separate import.(Earlier drafts used a separate
countries-list/aliasessub-module. Dropped after measuring: the data is ~0.6 KB, and a sub-module wouldn't fix the defaultgetCountryCode— only opt-in importers. Inline is simpler and fixes it for everyone.)Scope — curated, 23 countries
Czech Republic, Cabo Verde←Cape Verde, Türkiye←Turkey, Eswatini←Swaziland, North Macedonia←Macedonia, Myanmar←Burma.UK/Britain, US←USA/America, AE←UAE, NL←Holland, RU←Russian Federation.Persia, LK←Ceylon, TH←Siam, KH←Kampuchea, CD←Zaire.Also cleans the ad-hoc name hack:
'Myanmar (Burma)'→ name'Myanmar'+ alias'Burma'.Safety — false-positives guarded
Matching stays exact, case-insensitive, trimmed (no fuzzy/substring), so a returned code is trusted. A guard test fails the build if any name, native, or alias ever resolves to more than one country — inherently ambiguous forms ("Congo", "Korea") are excluded by construction.
Size & impact
countries.min.json: +634 B (36.4 → 37.0 KB). No CSV/SQL schema change (both use explicit fields; the only CSV/SQL diff is the Myanmar name fix).alias?is optional; existing behaviour for current names is unchanged.Verified
bun run cigreen — lint, all 3 typecheck targets, build, 21 tests (incl. resolution + the ambiguity guard). Local CodeRabbit review clean. No generateddist/committed (rebuilt at release).Related: #35 (alternative names); #177 (4.0 size).