41 lines
814 B
TypeScript
41 lines
814 B
TypeScript
const PERSIAN_DIGITS = /[۰-۹]/g;
|
||
const PERSIAN_TO_ENGLISH: Record<string, string> = {
|
||
'۰': '0',
|
||
'۱': '1',
|
||
'۲': '2',
|
||
'۳': '3',
|
||
'۴': '4',
|
||
'۵': '5',
|
||
'۶': '6',
|
||
'۷': '7',
|
||
'۸': '8',
|
||
'۹': '9',
|
||
};
|
||
|
||
export const normalizePhoneNumber = (value: string): string => {
|
||
if (!value) {
|
||
return '';
|
||
}
|
||
|
||
const normalized = value.replace(PERSIAN_DIGITS, (digit) => PERSIAN_TO_ENGLISH[digit] ?? digit);
|
||
return normalized.trim();
|
||
};
|
||
|
||
export const toE164PhoneNumber = (value: string): string => {
|
||
const normalized = normalizePhoneNumber(value);
|
||
const digits = normalized.replace(/\D/g, '');
|
||
|
||
if (!digits) {
|
||
return '';
|
||
}
|
||
|
||
if (digits.startsWith('98')) {
|
||
return `+${digits}`;
|
||
}
|
||
|
||
if (digits.startsWith('0')) {
|
||
return `+98${digits.slice(1)}`;
|
||
}
|
||
|
||
return `+${digits}`;
|
||
};
|