mirror of
https://github.com/andwati/zitadel.git
synced 2026-10-20 06:24:25 +00:00
20 lines
550 B
TypeScript
20 lines
550 B
TypeScript
import { CountryCode, parsePhoneNumber } from 'libphonenumber-js';
|
|
|
|
export function formatPhone(phone: string): { phone: string; country: CountryCode } {
|
|
const defaultCountry = 'CH';
|
|
|
|
if (phone) {
|
|
try {
|
|
const phoneNumber = parsePhoneNumber(phone, defaultCountry);
|
|
const country = phoneNumber.country ?? defaultCountry;
|
|
if (phoneNumber) {
|
|
return { phone: phoneNumber.formatInternational(), country };
|
|
}
|
|
} catch (error) {
|
|
console.error(error);
|
|
}
|
|
}
|
|
|
|
return { phone, country: defaultCountry };
|
|
}
|