import { StackAssertionError } from "./errors"; export function getFlagEmoji(twoLetterCountryCode: string) { if (!/^[a-zA-Z][a-zA-Z]$/.test(twoLetterCountryCode)) throw new StackAssertionError("Country code must be two alphabetical letters"); const codePoints = twoLetterCountryCode .toUpperCase() .split('') .map(char => 127397 + char.charCodeAt(0)); return String.fromCodePoint(...codePoints); } import.meta.vitest?.test("getFlagEmoji", ({ expect }) => { // Test with valid country codes expect(getFlagEmoji("US")).toBe("πŸ‡ΊπŸ‡Έ"); expect(getFlagEmoji("us")).toBe("πŸ‡ΊπŸ‡Έ"); expect(getFlagEmoji("GB")).toBe("πŸ‡¬πŸ‡§"); expect(getFlagEmoji("JP")).toBe("πŸ‡―πŸ‡΅"); // Test with invalid country codes expect(() => getFlagEmoji("")).toThrow("Country code must be two alphabetical letters"); expect(() => getFlagEmoji("A")).toThrow("Country code must be two alphabetical letters"); expect(() => getFlagEmoji("ABC")).toThrow("Country code must be two alphabetical letters"); expect(() => getFlagEmoji("12")).toThrow("Country code must be two alphabetical letters"); expect(() => getFlagEmoji("A1")).toThrow("Country code must be two alphabetical letters"); });