22 lines
612 B
TypeScript
22 lines
612 B
TypeScript
import { instance, PhoneNumberFormat } from './libphonenumberInstance';
|
|
|
|
export function formatPhoneNumber(
|
|
phoneNumber: string,
|
|
options: {
|
|
ourRegionCode: string;
|
|
}
|
|
) {
|
|
try {
|
|
const { ourRegionCode } = options;
|
|
const parsedNumber = instance.parse(phoneNumber);
|
|
const regionCode = instance.getRegionCodeForNumber(parsedNumber);
|
|
|
|
if (ourRegionCode && regionCode === ourRegionCode) {
|
|
return instance.format(parsedNumber, PhoneNumberFormat.NATIONAL);
|
|
}
|
|
|
|
return instance.format(parsedNumber, PhoneNumberFormat.INTERNATIONAL);
|
|
} catch (error) {
|
|
return phoneNumber;
|
|
}
|
|
}
|