File size: 2,344 Bytes
fe92a01 635c08a fe92a01 635c08a fe92a01 635c08a fe92a01 635c08a fe92a01 635c08a fe92a01 635c08a fe92a01 635c08a fe92a01 635c08a fe92a01 635c08a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | import { IANA_ISO3166 } from '@/lib/iso3166-iana';
/** ISO 3166-1 alpha-2 → regional indicator symbols (flag emoji). */
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;
/**
* Full country list: IANA iso3166.tab (every ISO alpha-2 region), display names
* upgraded via {@link Intl.DisplayNames} when available.
*
* @returns {{ code: string, name: string }[]}
*/
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 {
/* keep IANA label */
}
return { code, name };
}).sort((a, b) => a.name.localeCompare(b.name));
return cached;
}
/**
* Match stored deal.country (full name or code) to a row.
* @param {string} stored
* @param {{ code: string, name: string }[]} countries
*/
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;
}
|