Files
info-tager-mobile/constants/formatPhone.ts
Samandar Turgunboyev 124798419b fitst commit
2026-01-28 18:26:50 +05:00

50 lines
1.2 KiB
TypeScript

export function normalizeDigits(text: string): string {
return text.replace(/\D/g, '');
}
export function formatPhone(phone: string): string {
if (phone.length === 0) return '';
if (phone.length <= 2) return phone;
if (phone.length <= 5) return `${phone.slice(0, 2)} ${phone.slice(2)}`;
if (phone.length <= 7) return `${phone.slice(0, 2)} ${phone.slice(2, 5)} ${phone.slice(5)}`;
return `${phone.slice(0, 2)} ${phone.slice(2, 5)} ${phone.slice(5, 7)} ${phone.slice(7, 9)}`;
}
export function formatNumber(phone: string): string {
let digits = normalizeDigits(phone);
if (!digits) return '';
// Agar 998 bilan kelsa, olib tashlaymiz
if (digits.startsWith('998')) {
digits = digits.slice(3);
}
let formatted = '+998 ';
// operator kodi (XX)
if (digits.length >= 2) {
formatted += digits.slice(0, 2) + ' ';
} else {
return formatted + digits;
}
// 3 ta raqam
if (digits.length >= 5) {
formatted += digits.slice(2, 5) + '-';
} else {
return formatted + digits.slice(2);
}
// 2 ta raqam
if (digits.length >= 7) {
formatted += digits.slice(5, 7) + '-';
} else {
return formatted + digits.slice(5);
}
// oxirgi 2 ta raqam
formatted += digits.slice(7, 9);
return formatted;
}