Files
orca/src/shared/search-match-count.ts
T
JinjingandOrca 26aa473bca File search match counts (#5085)
* Add matchCount to SearchFileResult for accurate per-file hit counts

Co-authored-by: Orca <help@stably.ai>

* Add file search match count design

* rm design doc

---------

Co-authored-by: Orca <help@stably.ai>
2026-06-10 17:42:49 -07:00

31 lines
930 B
TypeScript

import type { SearchFileResult, SearchResult } from './types'
function isValidMatchCount(value: unknown): value is number {
return (
typeof value === 'number' && Number.isFinite(value) && Number.isInteger(value) && value >= 0
)
}
export function normalizeSearchFileMatchCount(
fileResult: Pick<SearchFileResult, 'matches' | 'matchCount'>
): number {
const matchCount = isValidMatchCount(fileResult.matchCount) ? fileResult.matchCount : 0
return Math.max(matchCount, fileResult.matches.length)
}
export function normalizeSearchFileResult(fileResult: SearchFileResult): SearchFileResult {
return {
...fileResult,
matchCount: normalizeSearchFileMatchCount(fileResult)
}
}
export function normalizeSearchResult(result: SearchResult): SearchResult {
return {
...result,
files: result.files
.filter((fileResult) => fileResult.matches.length > 0)
.map(normalizeSearchFileResult)
}
}