24 lines
565 B
TypeScript
24 lines
565 B
TypeScript
const MAX_PHONE_DIGITS = 13;
|
|
|
|
export function parsePhoneNumber(value: string | number): string {
|
|
return String(value).replace(/\D/g, '').slice(0, MAX_PHONE_DIGITS);
|
|
}
|
|
|
|
export function formatPhoneNumber(value: string | number): string {
|
|
const digits = parsePhoneNumber(value);
|
|
|
|
if (!digits) {
|
|
return '';
|
|
}
|
|
|
|
if (digits.length <= 4) {
|
|
return digits;
|
|
}
|
|
|
|
if (digits.length <= 8) {
|
|
return `${digits.slice(0, 4)} ${digits.slice(4)}`;
|
|
}
|
|
|
|
return `${digits.slice(0, 4)} ${digits.slice(4, 8)} ${digits.slice(8)}`;
|
|
}
|