From b26a5a566ba3296fb2c75f9a46ba2f757602d2a8 Mon Sep 17 00:00:00 2001 From: Azalea <22280294+hykilpikonna@users.noreply.github.com> Date: Sun, 9 Feb 2025 00:22:00 -0500 Subject: [PATCH] [+] Country code to emoji --- AquaNet/src/libs/i18n.ts | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/AquaNet/src/libs/i18n.ts b/AquaNet/src/libs/i18n.ts index dee1e892..89dbfbc7 100644 --- a/AquaNet/src/libs/i18n.ts +++ b/AquaNet/src/libs/i18n.ts @@ -61,3 +61,33 @@ export function getCountryName(code: keyof typeof enCountires) { export const GAME_TITLE: { [key in GameName]: string } = {chu3: t("game.chu3"), mai2: t("game.mai2"), ongeki: t("game.ongeki"), wacca: t("game.wacca")} + +/** + * Converts a two-letter country code to its corresponding flag emoji. + * + * The Unicode flag emoji is represented by two Regional Indicator Symbols. + * Each letter in the country code is transformed into a Regional Indicator Symbol + * by adding its alphabetical position (A = 0, B = 1, etc.) to the base code point U+1F1E6. + * + * @param countryCode - A two-letter ISO country code (e.g., "US", "GB"). + * @returns The corresponding flag emoji if the country code is valid; otherwise, an empty string. + */ +export function countryCodeToEmoji(countryCode: string): string { + if (!countryCode) return "" + if (countryCode.length !== 2) return "" + + // Convert the country code to uppercase to standardize it + const code = countryCode.toUpperCase(); + + // The base code point for Regional Indicator Symbol Letter A is 0x1F1E6. + const OFFSET = 0x1F1E6; + const firstCharCode = code.charCodeAt(0); + const secondCharCode = code.charCodeAt(1); + + // 'A' has a char code of 65. + const firstIndicator = OFFSET + (firstCharCode - 65); + const secondIndicator = OFFSET + (secondCharCode - 65); + + // Create and return the flag emoji string + return String.fromCodePoint(firstIndicator, secondIndicator); +}