// packages/core/src/unified-search.ts // 데스크톱·모바일 통합 검색의 공통 부분 문자열 판정·발췌(excerpt)·시간순 병합 정본 (W5-06). import type { UnifiedSearchResult } from './types' export const DEFAULT_UNIFIED_SEARCH_EXCERPT_LENGTH = 180 export function includesSearchQuery(value: string | null | undefined, query: string): boolean { return value?.toLocaleLowerCase().includes(query.toLocaleLowerCase()) === true } export function createUnifiedSearchExcerpt( value: string | null | undefined, query: string, maxLength = DEFAULT_UNIFIED_SEARCH_EXCERPT_LENGTH, ): string { const safeMaxLength = Number.isFinite(maxLength) ? Math.floor(maxLength) : DEFAULT_UNIFIED_SEARCH_EXCERPT_LENGTH if (safeMaxLength <= 0) return '' const text = (value ?? '').replace(/\s+/g, ' ').trim() if (text.length <= safeMaxLength) return text const normalizedQuery = query.trim() const matchAt = normalizedQuery ? text.toLocaleLowerCase().indexOf(normalizedQuery.toLocaleLowerCase()) : -1 if (matchAt < 0) { if (safeMaxLength === 1) return '…' return `${text.slice(0, safeMaxLength - 1).trimEnd()}…` } const context = Math.max(20, Math.floor((safeMaxLength - normalizedQuery.length) / 2)) const start = Math.max(0, matchAt - context) const end = Math.min(text.length, start + safeMaxLength) const body = text.slice(start, end).trim() return `${start > 0 ? '…' : ''}${body}${end < text.length ? '…' : ''}` } export function mergeUnifiedSearchResults( records: readonly UnifiedSearchResult[], meetings: readonly UnifiedSearchResult[], limit: number, ): UnifiedSearchResult[] { if (limit === Number.POSITIVE_INFINITY) { return [...records, ...meetings].sort( (left, right) => right.occurredAt - left.occurredAt || left.id.localeCompare(right.id), ) } if (!Number.isFinite(limit) || limit <= 0) return [] const safeLimit = Math.floor(limit) return [...records, ...meetings] .sort((left, right) => right.occurredAt - left.occurredAt || left.id.localeCompare(right.id)) .slice(0, safeLimit) }