getFlagEmoji.js
Get a country’s flag emoji from its two-letter country code.
Example
let usa = getFlagEmoji('us');
let japan = getFlagEmoji('jp');
let mexico = getFlagEmoji('mx');
The helper function
/**
* Get the flag emoji for the country
* @link https://dev.to/jorik/country-code-to-flag-emoji-a21
* @param {String} countryCode The country code
* @return {String} The flag emoji
*/
function getFlagEmoji (countryCode) {
let codePoints = countryCode.toUpperCase().split('').map(char => 127397 + char.charCodeAt());
return String.fromCodePoint(...codePoints);
}
How it works
The flag emoji is a combination of the two unicode region characters, located at unicode position 127462 for the letter A.
The getFlagEmoji()
function maps the two letters in the country code to corresponding unicode characters, and returns them as a string.