Skip to content

Instantly share code, notes, and snippets.

@bjjb
Created April 13, 2018 13:33
Show Gist options
  • Save bjjb/7bfb0ddb731021e650a9309c46bd05cd to your computer and use it in GitHub Desktop.
Save bjjb/7bfb0ddb731021e650a9309c46bd05cd to your computer and use it in GitHub Desktop.
/*
* Converts the 2-letter code (such as "es" or "ie" to an emoji representing that country's flag.
* It works because a flag emoji, such as the German flag, is a combination of regional letter
* glyphs (🇩 🇪 without a space is rendered as 🇩🇪, the German (de) flag). Those glyphs start at
* 0x1f1e6 and end at 0x1f1ff. 'a' is 97.
*/
const flagEmojiForCode = (code) => {
const h = code.toLowerCase().charCodeAt(0) + (0x1f1e6 - 97)
const l = code.toLowerCase().charCodeAt(1) + (0x1f1e6 - 97)
return String.fromCodePoint(h, l)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment