Files
scoreko-dev/src/shared/countries.ts
T
Pandipipas 547f9ab95f Add country dropdown and flags (#28)
* Add country dropdown and flags

* Fix build for country flags

* Improve country select filtering and scoreboard teams

* Fix country select display value

* Fix country select input display
2026-02-09 22:42:00 +01:00

50 lines
1.2 KiB
TypeScript

import { getData, type CountryRecord } from 'country-list';
export interface CountryOption {
value: string;
label: string;
}
const baseCountries = getData();
export const countryOptions: CountryOption[] = baseCountries
.map((country: CountryRecord) => ({
value: country.code,
label: country.name,
}))
.sort((a: CountryOption, b: CountryOption) => a.label.localeCompare(b.label));
const countryByCode = new Map(
countryOptions.map((country) => [country.value.toUpperCase(), country.label]),
);
const countryByName = new Map(
countryOptions.map((country) => [country.label.toLowerCase(), country.value]),
);
export const resolveCountryCode = (value?: string) => {
if (!value) {
return '';
}
const trimmed = value.trim();
if (!trimmed) {
return '';
}
const upper = trimmed.toUpperCase();
if (countryByCode.has(upper)) {
return upper;
}
const byName = countryByName.get(trimmed.toLowerCase());
return byName ?? '';
};
export const getCountryLabel = (value?: string) => {
if (!value) {
return '';
}
const resolved = resolveCountryCode(value);
if (!resolved) {
return value;
}
return countryByCode.get(resolved) ?? value;
};