signal-desktop/ts/util/formatPhoneNumber.ts
2018-05-07 19:30:50 -07:00

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;
}
}