mirror of
https://github.com/logto-io/logto.git
synced 2025-01-06 20:40:08 -05:00
497d5b5262
* feat(console,phrases): add support to update sign-in identifiers in user details form * chore: add changeset
28 lines
778 B
TypeScript
28 lines
778 B
TypeScript
import { parsePhoneNumberWithError } from 'libphonenumber-js';
|
|
|
|
/**
|
|
* Parse phone number to number string.
|
|
* E.g. +1 (650) 253-0000 -> 16502530000
|
|
*/
|
|
export const parsePhoneNumber = (phone: string) => {
|
|
try {
|
|
return parsePhoneNumberWithError(phone).number.slice(1);
|
|
} catch {
|
|
console.error(`Invalid phone number: ${phone}`);
|
|
return phone;
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Parse phone number to readable international format.
|
|
* E.g. 16502530000 -> +1 650 253 0000
|
|
*/
|
|
export const formatToInternationalPhoneNumber = (phone: string) => {
|
|
try {
|
|
const phoneNumber = phone.startsWith('+') ? phone : `+${phone}`;
|
|
return parsePhoneNumberWithError(phoneNumber).formatInternational();
|
|
} catch {
|
|
console.error(`Invalid phone number: ${phone}`);
|
|
return phone;
|
|
}
|
|
};
|