teqo-backup/src/lib/homeSearchMunicipalityMatch.ts
Francisco Ignácio Fontoura Solla 0e73d47a62 B50: home search Assessores group for unrestricted staff.
Word-start on name, municipality count on the row, shared HomeSearchHitRow; POST /campanha/home-search aggregates advisors alongside municipalities.
2026-07-29 22:09:42 -03:00

32 lines
1.1 KiB
TypeScript

import { normalizeSearchPhrase } from '@/lib/wordStartFilter'
/** Lower tier = stronger name match (word-start at label start beats mid-label). */
export const homeSearchNameRelevanceTier = (
normalizedLabel: string,
normalizedQuery: string,
): number => {
if (!normalizedQuery) return 0
if (normalizedLabel.startsWith(normalizedQuery)) return 0
if (normalizedLabel.includes(` ${normalizedQuery}`)) return 1
return 2
}
export type HomeSearchNameSortable = {
normalizedName: string
/** Secondary sort key (higher sorts first after name relevance tier). */
tieBreakDesc: number
}
export const compareHomeSearchNameRelevance = (
left: HomeSearchNameSortable,
right: HomeSearchNameSortable,
normalizedQuery: string,
): number => {
const tierDelta =
homeSearchNameRelevanceTier(left.normalizedName, normalizedQuery) -
homeSearchNameRelevanceTier(right.normalizedName, normalizedQuery)
if (tierDelta !== 0) return tierDelta
return right.tieBreakDesc - left.tieBreakDesc
}
export const normalizeHomeSearchName = (name: string): string => normalizeSearchPhrase(name)