|
| 1 | +// String utility functions |
| 2 | + |
| 3 | +export function removeVietnameseTones(str: string): string { |
| 4 | + return str.normalize('NFD') |
| 5 | + .replace(/[\u0300-\u036f]/g, '') |
| 6 | + .replace(/\u0111/g, 'd') |
| 7 | + .replace(/\u0110/g, 'D'); |
| 8 | +} |
| 9 | + |
| 10 | +export function maskEmail(email: string): string { |
| 11 | + const parts = email.split('@'); |
| 12 | + if (parts.length !== 2) return email; |
| 13 | + const name = parts[0]; |
| 14 | + const masked = name.charAt(0) + '***' + name.charAt(name.length - 1); |
| 15 | + return masked + '@' + parts[1]; |
| 16 | +} |
| 17 | + |
| 18 | +export function maskPhone(phone: string): string { |
| 19 | + if (phone.length < 4) return phone; |
| 20 | + return phone.slice(0, -4).replace(/./g, '*') + phone.slice(-4); |
| 21 | +} |
| 22 | + |
| 23 | +export function pluralize(count: number, singular: string, plural?: string): string { |
| 24 | + return count === 1 |
| 25 | + ? count + ' ' + singular |
| 26 | + : count + ' ' + (plural || singular + 's'); |
| 27 | +} |
| 28 | + |
| 29 | +export function highlightSearchTerm(text: string, term: string): string { |
| 30 | + if (!term.trim()) return text; |
| 31 | + const escaped = term.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); |
| 32 | + const regex = new RegExp('(' + escaped + ')', 'gi'); |
| 33 | + return text.replace(regex, '<mark>$1</mark>'); |
| 34 | +} |
0 commit comments