Created
January 10, 2022 08:39
-
-
Save kcjpop/b38e80b6c0e325f8e43ce5d9bbe3dca9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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