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 install country-code-emoji
yarn add country-code-emoji
usage
const { countryCodeEmoji, emojiCountryCode } = require('country-code-emoji');
countryCodeEmoji('US');
emojiCountryCode('🇺🇸');
import { countryCodeEmoji, emojiCountryCode } from 'country-code-emoji';
['AE', 'CN', 'GB'].map(countryCodeEmoji);
['🇦🇪', '🇨🇳', '🇬🇧'].map(emojiCountryCode);
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.
import flag from 'country-code-emoji';
flag();
['Us', 'uS', 'us'].every(e => flag(e) === flag('US'));
flag('UK');