refactoring
This commit is contained in:
33
src/components/PhoneInput/utils/countryOptionsDataMap.ts
Normal file
33
src/components/PhoneInput/utils/countryOptionsDataMap.ts
Normal file
@ -0,0 +1,33 @@
|
||||
import countries from "i18n-iso-countries";
|
||||
import ru from "i18n-iso-countries/langs/ru.json";
|
||||
import { type CountryCode, getCountries } from "libphonenumber-js";
|
||||
import getFlagEmoji from "@/components/PhoneInput/utils/getFlagEmoji";
|
||||
import { getCountryCallingCode } from "libphonenumber-js/max";
|
||||
import { Country } from "@/components/PhoneInput/types";
|
||||
|
||||
countries.registerLocale(ru);
|
||||
|
||||
const libIsoCountries = countries.getNames("ru", { select: "official" });
|
||||
const libPhoneNumberCountries = getCountries();
|
||||
|
||||
const countryOptionsDataMap = Object.fromEntries(
|
||||
libPhoneNumberCountries
|
||||
.map(code => {
|
||||
const name = libIsoCountries[code];
|
||||
const emoji = getFlagEmoji(code);
|
||||
if (!name || !emoji) return null;
|
||||
const callingCode = getCountryCallingCode(code);
|
||||
return [
|
||||
code,
|
||||
{
|
||||
code,
|
||||
name,
|
||||
emoji,
|
||||
callingCode,
|
||||
},
|
||||
] as [CountryCode, Country];
|
||||
})
|
||||
.filter(o => !!o)
|
||||
);
|
||||
|
||||
export default countryOptionsDataMap;
|
||||
Reference in New Issue
Block a user