ecma402 icon indicating copy to clipboard operation
ecma402 copied to clipboard

List of ISO country codes

Open jfbrennan opened this issue 3 months ago • 2 comments

I'm looking to avoid maintaining a list of country codes in my app. I would expect JavaScript runtimes to provide this data.

This is precisely what I wish Intl would add to the current supportedValuesOf method:

Intl.supportedValuesOf('region') // "region" key is used in other APIs to mean ISO 3166, i.e. the country codes

Many Intl APIs apparently have mappings for country codes, so perhaps this internal list of codes can be given a public interface (and my apologies if I didn't google hard enough...I spent a lot of time searching around and reading MDN and SO and the tc39 backlogs and didn't find answers).

Why this is needful:

  • It's standardized data used by many computer systems
  • Existing APIs don't seem to offer any way of getting at this information today
  • A list of country codes is a very common need for developers building support for an international user base
  • Developers have to find a reliable source for this data, bring it into their codebase, and maintain it (or add a 3rd-party dependency that does it for them)
  • Many UI features depend on this info, being able to loop the country codes directly from an Intl API into a corresponding Intl API would be quite convenient:
    • select or menu element with a list of countries (e.g. shipping address form)
    • custom phone number input with list of country prefix numbers (e.g. contacts card phone input)
    • table of countries and their currency (e.g. financial tools)

An example:

// List of options for a country select
const names = new Intl.DisplayNames(['en'], {type: 'region'});
const options = Intl.supportedValuesOf('region').map(code => `<option value="${code}">${names.of(code)}</option>`);

jfbrennan avatar Mar 07 '24 22:03 jfbrennan