Skip to content

feat: resolve alternative and former country names in getCountryCode - #178

Merged
dmythro merged 2 commits into
mainfrom
feat/country-aliases
Jul 14, 2026
Merged

dmythro merged 2 commits into
mainfrom
feat/country-aliases

Conversation

@dmythro

@dmythro dmythro commented Jul 14, 2026 •

Copy link
Copy Markdown
Member

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 returned false. This also matters for name-based sources like Discogs ("UK", "USSR"…) and MusicBrainz.

Approach — inline alias, fixes the default lookup

An optional alias?: string[] on ICountry, matched by the core getCountryCode. So the fix applies to every consumer of the default function — no opt-in, no separate import.

getCountryCode('Czech Republic') // 'CZ'
getCountryCode('Turkey')         // 'TR'  (current name is 'Türkiye')
getCountryCode('Burma')          // 'MM'
getCountryCode('UK')             // 'GB'
getCountryData('MM').alias       // ['Burma']

(Earlier drafts used a separate countries-list/aliases sub-module. Dropped after measuring: the data is ~0.6 KB, and a sub-module wouldn't fix the default getCountryCode — only opt-in importers. Inline is simpler and fixes it for everyone.)

Scope — curated, 23 countries

  • Recent ISO renames (the actual backward-compat fix): Czechia←Czech Republic, Cabo Verde←Cape Verde, Türkiye←Turkey, Eswatini←Swaziland, North Macedonia←Macedonia, Myanmar←Burma.
  • Common short forms: GB←UK/Britain, US←USA/America, AE←UAE, NL←Holland, RU←Russian Federation.
  • Former single-country names: IR←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).
  • Non-breaking: alias? is optional; existing behaviour for current names is unchanged.

Verified

bun run ci green — lint, all 3 typecheck targets, build, 21 tests (incl. resolution + the ambiguity guard). Local CodeRabbit review clean. No generated dist/ committed (rebuilt at release).

Related: #35 (alternative names); #177 (4.0 size).

@coderabbitai

coderabbitai Bot commented Jul 14, 2026 •

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 0daa290b-587b-4e9b-a444-05ea8c839344

📥 Commits

Reviewing files that changed from the base of the PR and between c8a55da and 583581a.

📒 Files selected for processing (2)
  • packages/countries/src/data/countries.ts
  • packages/test-js/getCountryCode.aliases.test.ts
🚧 Files skipped from review as they are similar to previous changes (2)
  • packages/countries/src/data/countries.ts
  • packages/test-js/getCountryCode.aliases.test.ts

📝 Walkthrough

Walkthrough

The country data model now supports optional aliases for selected entries. Country records include common and former names, while Myanmar uses Myanmar as its primary name and Burma as an alias. getCountryCode matches aliases alongside names and native names. Tests cover normalization, unknown inputs, alias coverage, uniqueness, and updated long-form country expectations.

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
Loading

Possibly related PRs

  • annexare/Countries#170: Updates overlapping country names and getCountryCode expectations for Czechia and Cabo Verde.

Suggested reviewers: copilot

Poem

A bunny hops through names anew,
From Burma’s trail to Türkiye blue.
Aliases bloom, codes answer bright,
Whitespace and case now sit just right.
Each country claims its naming tune.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description is detailed, but it omits the template's Issue # and verified data source URLs. Add the Issue # field and include URL(s) for the data sources used to verify the country alias changes.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title is concise and accurately summarizes the main change to getCountryCode.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/country-aliases

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

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
@dmythro
dmythro force-pushed the feat/country-aliases branch from 27ac454 to c8a55da Compare July 14, 2026 13:36
@dmythro dmythro changed the title feat(aliases): opt-in countries-list/aliases sub-module feat: resolve alternative and former country names in getCountryCode Jul 14, 2026
@dmythro
dmythro marked this pull request as ready for review July 14, 2026 13:45
@dmythro
dmythro requested a review from Copilot July 14, 2026 13:45

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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[] to ICountry and populate it for selected countries (including renames and common short forms).
  • Update getCountryCode() to match against name, native, or any alias entry (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.
@dmythro
dmythro merged commit 1c0f419 into main Jul 14, 2026
4 checks passed
@dmythro
dmythro deleted the feat/country-aliases branch July 14, 2026 14:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants