Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/thekelvinliu/country-code-emoji
convert country codes (ISO 3166-1 alpha-2) to corresponding emoji flags (unicode regional indicator symbols)
https://github.com/thekelvinliu/country-code-emoji
country-codes country-flags emoji emoji-flags regional-indicator-symbols
Last synced: about 12 hours ago
JSON representation
convert country codes (ISO 3166-1 alpha-2) to corresponding emoji flags (unicode regional indicator symbols)
- Host: GitHub
- URL: https://github.com/thekelvinliu/country-code-emoji
- Owner: thekelvinliu
- License: mit
- Created: 2016-06-02T04:31:13.000Z (over 8 years ago)
- Default Branch: main
- Last Pushed: 2023-07-19T00:43:43.000Z (over 1 year ago)
- Last Synced: 2024-10-30T04:49:46.318Z (3 months ago)
- Topics: country-codes, country-flags, emoji, emoji-flags, regional-indicator-symbols
- Language: JavaScript
- Size: 328 KB
- Stars: 132
- Watchers: 3
- Forks: 10
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
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](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCodePoint#Polyfill).## install
```bash
# npm
npm install country-code-emoji# yarn
yarn add country-code-emoji
```## usage
```javascript
// 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](https://en.wikipedia.org/wiki/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.```javascript
// 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 'πΊπ°'
```