Edit from phone contacts
This commit is contained in:
@@ -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;
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user