| import { IANA_ISO3166 } from '@/lib/iso3166-iana'; |
|
|
| |
| export function flagEmojiFromCode(code) { |
| if (!code || typeof code !== 'string' || code.length !== 2) return '🏳️'; |
| const u = code.toUpperCase(); |
| if (u.length !== 2 || /[^A-Z]/.test(u)) return '🏳️'; |
| return String.fromCodePoint(...[...u].map((c) => 127397 + c.charCodeAt(0))); |
| } |
|
|
| let cached = null; |
|
|
| |
| |
| |
| |
| |
| |
| export function getAllCountries() { |
| if (cached) return cached; |
| const dn = |
| typeof Intl !== 'undefined' && typeof Intl.DisplayNames === 'function' |
| ? new Intl.DisplayNames(['en'], { type: 'region' }) |
| : null; |
|
|
| cached = IANA_ISO3166.map(({ code, name: ianaName }) => { |
| let name = ianaName; |
| try { |
| if (dn) { |
| const intl = dn.of(code); |
| if (intl && intl !== code) name = intl; |
| } |
| } catch { |
| |
| } |
| return { code, name }; |
| }).sort((a, b) => a.name.localeCompare(b.name)); |
|
|
| return cached; |
| } |
|
|
| |
| |
| |
| |
| |
| export function matchCountry(stored, countries) { |
| if (!stored || typeof stored !== 'string') return null; |
| const s = stored.trim(); |
| if (!s) return null; |
| const lower = s.toLowerCase(); |
|
|
| const byName = countries.find((c) => c.name.toLowerCase() === lower); |
| if (byName) return byName; |
|
|
| const byCode = countries.find((c) => c.code.toLowerCase() === lower); |
| if (byCode) return byCode; |
|
|
| const iana = IANA_ISO3166.find((c) => c.name.toLowerCase() === lower); |
| if (iana) { |
| return countries.find((c) => c.code === iana.code) || iana; |
| } |
|
|
| const fuzzy = countries.find((c) => c.name.toLowerCase().includes(lower)); |
| if (fuzzy) return fuzzy; |
|
|
| const ianaFuzzy = IANA_ISO3166.find((c) => c.name.toLowerCase().includes(lower)); |
| if (ianaFuzzy) { |
| return countries.find((c) => c.code === ianaFuzzy.code) || ianaFuzzy; |
| } |
|
|
| return null; |
| } |
|
|