Files
zitadel/console/src/app/utils/formatPhone.ts
T
6f82285ad6 fix: country flag and phone now in sync (#6727)
* fix: country flag and phone now in sync

* change default country

---------

Co-authored-by: Elio Bischof <[email protected]>
Co-authored-by: Elio Bischof <[email protected]>
2023-10-24 13:47:44 +00:00

20 lines
514 B
TypeScript

import { CountryCode, parsePhoneNumber } from 'libphonenumber-js';
export function formatPhone(phone: string): { phone: string; country: CountryCode } | null {
const defaultCountry = 'US';
if (phone) {
try {
const phoneNumber = parsePhoneNumber(phone, defaultCountry);
const country = phoneNumber.country ?? defaultCountry;
if (phoneNumber) {
return { phone: phoneNumber.formatInternational(), country };
}
} catch (e) {
return null;
}
}
return null;
}