Files
SHiNE-server/old-frame-v1/shine-UI/js/services/people-search.js
T

21 lines
1.2 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.
// Поиск людей в списках на устройстве: совпадение с началом любого слова имени или логина.
// «анна» находит «Анну Гусеву» и anna_guseva, но не «Жанну»; «гус ан» – тоже Анну Гусеву.
// Регистр и «ё» не важны, «@» в начале запроса не мешает.
export function normalizeSearchText(value) {
return String(value || '').toLowerCase().replaceAll('ё', 'е').trim();
}
/**
* @param {string} text – всё, по чему ищется человек: имя, фамилия, логин
* @param {string} query – как ввели (нормализуется здесь же)
*/
export function matchesPeopleQuery(text, query) {
const parts = normalizeSearchText(query).replace(/^@/, '').split(/\s+/).filter(Boolean);
if (!parts.length) return true;
const hay = normalizeSearchText(text);
// Слова целиком («q_tester1») и их части по «_», «.», «-», «@».
const words = new Set([...hay.split(/\s+/), ...hay.split(/[\s@_.-]+/)].filter(Boolean));
return parts.every((part) => [...words].some((word) => word.startsWith(part)));
}