Skip to content

Instantly share code, notes, and snippets.

@Niels-NTG
Last active November 1, 2020 13:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Niels-NTG/8ac6bebe90c147c52f4f3b1d5f0a16e7 to your computer and use it in GitHub Desktop.
Save Niels-NTG/8ac6bebe90c147c52f4f3b1d5f0a16e7 to your computer and use it in GitHub Desktop.
Construct flag emoji from ISO 3166-1 2 letter country code
/*
* Constructs an emoji flag using a supported ISO 3166-1 2 letter country code.
*
* Per character in the input string it shifts it 127365 code points from the ASCII lower case
* characters to Regional Indicator Symbol series, which is used to construct flag emoji.
*
* @param {String} regionCode String starting with a supported ISO 3166-1 2 letter region code.
* @returns {String} Emoji flag character constructed from 2 regional indicator symbols.
*/
function flag(regionCode = '') {
return regionCode.toLowerCase().split('', 2).reduce((m, char) => {
return m + String.fromCodePoint(char.codePointAt(0) + 127365)
}, '')
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment