dana-medical-site/web/src/lib/doctorGroups.js
2026-10-22 10:59:52 +00:00

23 lines
1.3 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const normalize = (value) => String(value || '').toLocaleLowerCase('ru').replaceAll('ё', 'е').trim();
// Group by the catalog's real specialties, not by guesses from a doctor's name or role.
export function groupDoctors(doctors, query = '') {
const terms = normalize(query).split(/\s+/).filter(Boolean);
const groups = new Map();
for (const doctor of doctors) {
const specialties = doctor.specialties?.length ? doctor.specialties : [{ slug: 'other', name: 'Другие специалисты' }];
for (const specialty of specialties) {
const name = specialty.name_plural || specialty.name || 'Другие специалисты';
const key = String(specialty.slug || specialty.id || name);
const searchable = normalize(`${doctor.full_name} ${doctor.position || ''} ${name}`);
if (!terms.every((term) => searchable.includes(term))) continue;
if (!groups.has(key)) groups.set(key, { key, name, doctors: [] });
const group = groups.get(key);
if (!group.doctors.some((entry) => entry.id === doctor.id)) group.doctors.push(doctor);
}
}
return [...groups.values()]
.sort((a, b) => (a.key === 'other') - (b.key === 'other') || a.name.localeCompare(b.name, 'ru'))
.map((group) => ({ ...group, doctors: group.doctors.sort((a, b) => a.full_name.localeCompare(b.full_name, 'ru')) }));
}