Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

country-code-emoji

thekelvinliu141.9kMIT2.3.0TypeScript support: included

convert country codes (ISO 3166-1 alpha-2) to corresponding emoji flags (unicode regional indicator symbols)

country, code, emoji, flag, ISO, 3166-1, alpha-2, regional, indicator, symbols

readme

country-code-emoji

convert country codes (ISO 3166-1 alpha-2) to corresponding emoji flags (unicode regional indicator symbols)

about

this is a completely dependency-free module to convert ascii letters to regional indicator symbols; for valid country codes, this results in that country's emoji flag. commonjs and es module builds are available via package.json's main and module. it relies on String.fromCodePoint internally, but does not provide any polyfills. if your environment does not support String.fromCodePoint, grab a polyfill like this one from mdn.

install

# npm
npm install country-code-emoji

# yarn
yarn add country-code-emoji

usage

// commonjs module
const { countryCodeEmoji, emojiCountryCode } = require('country-code-emoji');
countryCodeEmoji('US'); // returns '🇺🇸'
emojiCountryCode('🇺🇸'); // returns 'US'

// es module
import { countryCodeEmoji, emojiCountryCode } from 'country-code-emoji';
['AE', 'CN', 'GB'].map(countryCodeEmoji); // returns ['🇦🇪', '🇨🇳', '🇬🇧']
['🇦🇪', '🇨🇳', '🇬🇧'].map(emojiCountryCode); // returns ['AE', 'CN', 'GB']

this module exports a pair of functions to convert country codes to/from flag emojis. the first is countryCodeEmoji(cc). it accepts a two-character (case-insensitive) country code and throws a TypeError if anything else is passed. more specifically, cc is expected to be a ISO 3166-1 alpha-2 country code. to keep things simple, if cc a two letter string, but not an actual ISO 3166-1 alpha-2 code, the regional indicator symbols corresponding to the letters in cc are returned. the second is emojiCountryCode(flag), which accepts a flag emoji and similarly throws a TypeError if anything else is passed.

// default export is countryCodeEmoji
import flag from 'country-code-emoji';

flag(); // throws TypeError
['Us', 'uS', 'us'].every(e => flag(e) === flag('US')); // returns true
flag('UK'); // returns '🇺🇰'

changelog

changelog

all notable changes to this project will be documented in this file.

the format is based on keep a changelog, and this project adheres to semantic versioning.

[2.3.0] - 2021-05-23

added

  • esm build with .mjs extension for official nodejs esm import support

[2.2.0] - 2020-12-30

added

  • emojiCountryCode named export to convert from flag emoji to country code
  • more usage info to readme

[2.1.0] - 2020-12-16

added

  • typescript types

[2.0.0] - 2020-12-13

added

  • export map
  • countryCodeEmoji named export

changed

  • build to use esbuild instead of rollup

removed

  • browser umd build
  • travis ci

[1.0.0] - 2019-03-03

added

  • commonjs, es module, and umd builds

changed

  • single, default export
  • throw TypeError for any input that isn't a country code string
  • rollup for cjs, es, and umd builds
  • jest for unit testing

removed

  • gulp as build tool
  • named export

[0.0.1] - 2016-06-02

added

  • initial release