Skip to content

Instantly share code, notes, and snippets.

@kcjpop
Created January 10, 2022 08:39
Show Gist options
  • Save kcjpop/b38e80b6c0e325f8e43ce5d9bbe3dca9 to your computer and use it in GitHub Desktop.
Save kcjpop/b38e80b6c0e325f8e43ce5d9bbe3dca9 to your computer and use it in GitHub Desktop.
// Translates 'a' to '🇦', 'b' to '🇧' and so on.
function letterToLetterEmoji(letter) {
return String.fromCodePoint(letter.toLowerCase().charCodeAt() + 127365);
}
// Translates 'pl' to 'PL', 'en-US' to 'US' and so on.
function countryCodeToCountry(countryCode) {
return countryCode.split('-').pop().toUpperCase();
}
// Translates 'pl-PL' to 🇵🇱 and so on.
export default function countryCodeToFlagEmoji(str) {
if (!str) {
return str;
}
return Array.from(countryCodeToCountry(str)).map(letterToLetterEmoji).join('');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment