UNPKG

@cprecioso/country-flag-emoji

Version:

Lightweight emoji flag to and from country code

22 lines (20 loc) 1.21 kB
/* These magic numbers are charCode math, where we take advantage of the offset between any two uppercase leters (e.g. A - C = 2) being the same as the one between the emoji flag variant of those letters (e.g. (emoji A) - (emoji C) = 2). */ const FLAG_LETTER_EMOJI_FIRST_CODEPOINT = 55356; // "🇦".charCodeAt(0) const UPPERCASE_AND_FLAG_LETTER_EMOJI_SECOND_CODEPOINT_OFFSET = 56741; // "🇦".charCodeAt(1) - "A".charCodeAt(0) const getEmojiFlag = (countryCode) => { countryCode = countryCode.toUpperCase(); /* Emoji flags are made from combination of two emoji sequences of two codepoints each. */ return String.fromCharCode(FLAG_LETTER_EMOJI_FIRST_CODEPOINT, UPPERCASE_AND_FLAG_LETTER_EMOJI_SECOND_CODEPOINT_OFFSET + countryCode.charCodeAt(0), FLAG_LETTER_EMOJI_FIRST_CODEPOINT, UPPERCASE_AND_FLAG_LETTER_EMOJI_SECOND_CODEPOINT_OFFSET + countryCode.charCodeAt(1)); }; const getCountryCode = (flagEmoji) => { return String.fromCharCode(flagEmoji.charCodeAt(1) - UPPERCASE_AND_FLAG_LETTER_EMOJI_SECOND_CODEPOINT_OFFSET, flagEmoji.charCodeAt(3) - UPPERCASE_AND_FLAG_LETTER_EMOJI_SECOND_CODEPOINT_OFFSET); }; export { getCountryCode, getEmojiFlag };