Edit from phone contacts

This commit is contained in:
2026-09-21 20:26:19 +03:30
parent 5d9a572ff8
commit 9249302eac
3 changed files with 285 additions and 17 deletions
+33
View File
@@ -232,3 +232,36 @@ export async function parseVCardFile(file: File): Promise<PhoneContactDraft[]> {
const text = await file.text();
return parseVCard(text);
}
function toEnDigits(raw: string): string {
return raw.replace(/[۰-۹٠-٩]/g, (ch) => {
const fa = '۰۱۲۳۴۵۶۷۸۹'.indexOf(ch);
if (fa >= 0) return String(fa);
const ar = '٠١٢٣٤٥٦٧٨٩'.indexOf(ch);
return ar >= 0 ? String(ar) : ch;
});
}
export function sortPhoneContacts(rows: PhoneContactDraft[]): PhoneContactDraft[] {
return [...rows].sort((a, b) =>
a.fullName.localeCompare(b.fullName, 'fa', { sensitivity: 'base', numeric: true }),
);
}
export function filterPhoneContacts(
rows: PhoneContactDraft[],
query: string,
): PhoneContactDraft[] {
const needle = toEnDigits(query).trim().toLowerCase();
if (!needle) return rows;
const digitNeedle = needle.replace(/\D/g, '');
return rows.filter((row) => {
const name = toEnDigits(row.fullName).toLowerCase();
if (name.includes(needle)) return true;
const phones = [row.mobile, row.phone].filter(Boolean).join(' ');
const phonesNorm = toEnDigits(phones).toLowerCase();
if (phonesNorm.includes(needle)) return true;
if (digitNeedle && phonesNorm.replace(/\D/g, '').includes(digitNeedle)) return true;
return false;
});
}