countries-list Documentation

repository·main·Indexed 23 days ago

https://github.com/annexare/countries

A comprehensive dataset of countries, languages, and continents providing strongly typed data for ISO 3166-1 (countries), ISO 639-1 (languages), and ISO 4217 (currencies). It includes utilities for looking up country codes by name, retrieving emoji flags, and mapping between ISO alpha-2 and alpha-3 codes. Data is available in JSON, CSV, and SQL formats, with support for ESM, CJS, and IIFE.

Tokens
4.6K
Snippets
8
Records
30
Agent score
79%

What's inside countries-list

  1. Understand the data structures

    main

    The library organizes data into four main objects: continents, countries, languages, and currencies.

    • Continents: Keyed by TContinentCode (e.g., AF, EU), mapping to the continent name.
    • Countries: Keyed by TCountryCode (e.g., UA), containing name, native name, phone prefix, continent code, capital, currency array, and languages array.
    • Languages: Keyed by TLanguageCode (e.g., uk), containing name, native name, and rtl (right-to-left) indicator.
    • Currencies: Keyed by TCurrencyCode (e.g., UAH), containing name, native name, symbol, symbolNative, numeric code, and decimals count.
    const continents = {
      AF: 'Africa',
      AN: 'Antarctica',
      AS: 'Asia',
      EU: 'Europe',
      NA: 'North America',
      OC: 'Oceania',
      SA: 'South America',
    }
    
    const countries = {
      UA: {
        name: 'Ukraine',
        native: 'Україна',
        phone: [380],
        continent: 'EU',
        capital: 'Kyiv',
        currency: ['UAH'],
        languages: ['uk'],
      },
    }
    
    const languages = {
      uk: {
        name: 'Ukrainian',
        native: 'Українська',
      },
      ur: {
        name: 'Urdu',
        native: 'اردو',
        rtl: 1,
      },
    }
    
    const currencies = {
      UAH: {
        name: 'Ukrainian Hryvnia',
        native: 'українська гривня',
        symbol: '₴',
        symbolNative: '₴',
        numeric: '980',
        decimals: 2,
      },
    }
  2. Install countries-list

    main

    You can install the countries-list package using Bun, NPM, or Composer depending on your environment.

    # Bun
    bun add countries-list
    
    # NPM
    npm install countries-list
    
    # Composer / Packagist
    composer require annexare/countries-list
  3. Understand the structure of the `countries` data object

    main

    The countries object is a mapping where keys are ISO 3166-1 alpha-2 country codes and values are objects containing detailed country information.

    Each country object typically includes:

    • name: The English name of the country.
    • native: The name of the country in its native language.
    • phone: An array of phone country codes.
    • continent: The primary continent code (e.g., 'AF', 'EU', 'AS').
    • continents: (Optional) An array of continent codes if the country spans multiple continents.
    • capital: The name of the capital city.
    • currency: An array of ISO 4217 currency codes.
    • languages: An array of ISO 639-1 language codes.
    • alias: (Optional) An array of alternative names for the country.
    • partOf: (Optional) The ISO code of the country it belongs to (e.g., for territories).

    Note: Some entries like Antarctica (AQ) may have empty arrays for currency or languages and an empty string for capital.

  4. Access ISO 4217 currency data

    main

    Full currency data is an opt-in import via the countries-list/currencies subpath to keep the main bundle small. This includes English and native names, UI symbols, native symbols, 3-digit numeric codes, and decimal counts.

    Utilities provided:

    • currencies: An object containing all currency data keyed by code.
    • getCurrency(code): Returns ICurrencyData for a specific code (e.g., 'JPY').
    • getCurrencyByNumeric(numericCode): Performs a lookup using the 3-digit numeric code (e.g., '840' for USD).
    import type { ICurrency, ICurrencyData, TCurrencyCode } from 'countries-list'
    import { currencies, getCurrency, getCurrencyByNumeric } from 'countries-list/currencies'
    
    // Minimal maps (code -> value)
    import currencySymbols from 'countries-list/minimal/currencies.symbol.min.json'
    import currencyNumbers from 'countries-list/minimal/currencies.numeric.min.json'
    
    // Example usage
    currencies.UAH // { name: 'Ukrainian Hryvnia', native: 'українська гривня', symbol: '₴', symbolNative: '₴', numeric: '980', decimals: 2 }
    getCurrency('JPY') // ICurrencyData: { code: 'JPY', name: 'Japanese Yen', symbol: '¥', decimals: 0, ... }
    getCurrencyByNumeric('840') // ICurrencyData for USD
  5. Use the core countries-list module

    main

    The main module provides access to continents, countries, and languages data, along with utility functions for lookups. It supports ESM, CJS, and IIFE formats.

    Key utilities include:

    • getCountryCode(name): Returns the ISO 3166-1 alpha-2 code for a given country name (supports English and native names).
    • getCountryData(code): Returns an ICountryData object for a specific country code.
    • getCountryDataList(): Returns the full list of country data.
    • getEmojiFlag(code): Returns the emoji flag for a country code.
    import type {
      ICountry,
      ICountryData,
      ILanguage,
      TContinentCode,
      TCountryCode,
      TLanguageCode,
    } from 'countries-list'
    
    import { continents, countries, languages } from 'countries-list'
    import { getCountryCode, getCountryData, getCountryDataList, getEmojiFlag } from 'countries-list'
    
    // Example usage
    getCountryCode('Ukraine') // 'UA'
    getCountryCode('Україна') // 'UA'
    getCountryData('UA') // ICountryData
  6. Import minimal data sets

    main

    To reduce bundle size, you can import specific minimal JSON files for country code conversions or native language names.

    Available minimal imports:

    • countries-list/minimal/countries.2to3.min.json: ISO 3166-1 alpha-2 to alpha-3 mapping.
    • countries-list/minimal/countries.3to2.min.json: ISO 3166-1 alpha-3 to alpha-2 mapping.
    • countries-list/minimal/languages.native.min.json: Native language names.
    import countries2to3 from 'countries-list/minimal/countries.2to3.min.json'
    import countries3to2 from 'countries-list/minimal/countries.3to2.min.json'
    import languageNames from 'countries-list/minimal/languages.native.min'
  7. Resolve a country code from a country name with getCountryCode()

    main

    The getCountryCode function takes a country name string and attempts to find a matching ISO 3166-1 alpha-2 code (TCountryCode). It performs a case-insensitive match against the country's English name, its native name, or any of its defined aliases.

    If a match is found, it returns the iso2 code. If no match is found, it returns false.

  8. Access language data via the `languages` object

    main

    The languages object provides a mapping of ISO 639-1 language codes to their respective language details. Each entry contains the English name, the native name, and an optional rtl flag indicating if the language is written from right-to-left.

    Data Structure

    Each language entry follows the ILanguage interface:

    • name: The English name of the language.
    • native: The name of the language in its own script.
    • rtl (optional): A numeric flag (1) indicating Right-to-Left writing direction.

    Example Usage

    import { languages } from './path/to/languages';
    
    const english = languages.en;
    console.log(english.name); // 'English'
    console.log(english.native); // 'English'
    
    const arabic = languages.ar;
    console.log(arabic.name); // 'Arabic'
    console.log(arabic.rtl); // 1
  9. Access currency data via the `currencies` object

    main

    The currencies object provides a mapping of ISO 4217 currency codes to their respective metadata. Each entry contains the currency's name, native name, symbol, native symbol, numeric ISO code, and decimal precision. Some entries may include a withdrawn: true flag if the currency is no longer in active use.

    Each currency object follows the ICurrency interface structure.