23 lines
1.3 KiB
JavaScript
23 lines
1.3 KiB
JavaScript
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')) }));
|
||
}
|