21 lines
614 B
TypeScript
21 lines
614 B
TypeScript
export function normalizePhone(raw: string): string {
|
||||
|
|
let digits = raw.replace(/\D/g, '');
|
|||
|
|
if (digits.startsWith('98') && digits.length === 12) {
|
|||
|
|
digits = `0${digits.slice(2)}`;
|
|||
|
|
}
|
|||
|
|
if (digits.length === 10 && digits.startsWith('9')) {
|
|||
|
|
digits = `0${digits}`;
|
|||
|
|
}
|
|||
|
|
return digits;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
export function isValidPhone(phone: string): boolean {
|
|||
|
|
return /^09\d{9}$/.test(phone);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
export function toEnglishDigits(value: string): string {
|
|||
|
|
return value
|
|||
|
|
.replace(/[۰-۹]/g, (d) => String('۰۱۲۳۴۵۶۷۸۹'.indexOf(d)))
|
|||
|
|
.replace(/[٠-٩]/g, (d) => String('٠١٢٣٤٥٦٧٨٩'.indexOf(d)));
|
|||
|
|
}
|