🧹 [code health] Refactor overly long function searchAllSheets

🎯 What: Refactored the overly long `searchAllSheets` function in `src/ui/search.ts` by extracting the numeric and text search logic into separate helper functions (`searchNumeric` and `searchText`).
💡 Why: This improves readability and maintainability by splitting a complex, monolithic function into smaller, focused, and easier-to-understand parts.
✅ Verification: Ensured the full test suite (`npm run test`) passes with the refactored code.
✨ Result: The search logic is now more modular, with a simplified main entry point.

Co-authored-by: Horizontal42 <75383519+Horizontal42@users.noreply.github.com>
This commit is contained in:
google-labs-jules[bot] 2026-09-04 07:13:47 +00:00
parent 37cfd2144b
commit 0e9c43447f

View file

@ -44,12 +44,8 @@ export function parseResultQuery(
return { op: m[1], threshold: v }; return { op: m[1], threshold: v };
} }
export function searchAllSheets(deps: SearchDeps, query: string): SearchHit[] { function searchNumeric(deps: SearchDeps, docs: SearchDoc[], rq: { op: string; threshold: Value }): SearchHit[] {
const q = query.trim(); if (rq.threshold.kind !== "quantity") return [];
if (!q) return [];
const docs = deps.docs();
const rq = parseResultQuery(deps.engine, q);
if (rq && rq.threshold.kind === "quantity") {
const th = rq.threshold.value; const th = rq.threshold.value;
const hits: SearchHit[] = []; const hits: SearchHit[] = [];
for (const doc of docs) { for (const doc of docs) {
@ -95,6 +91,8 @@ export function searchAllSheets(deps: SearchDeps, query: string): SearchHit[] {
} }
return hits; return hits;
} }
function searchText(docs: SearchDoc[], q: string): SearchHit[] {
const ql = q.toLowerCase(); const ql = q.toLowerCase();
const hits: SearchHit[] = []; const hits: SearchHit[] = [];
for (const doc of docs) { for (const doc of docs) {
@ -114,6 +112,17 @@ export function searchAllSheets(deps: SearchDeps, query: string): SearchHit[] {
return hits; return hits;
} }
export function searchAllSheets(deps: SearchDeps, query: string): SearchHit[] {
const q = query.trim();
if (!q) return [];
const docs = deps.docs();
const rq = parseResultQuery(deps.engine, q);
if (rq && rq.threshold.kind === "quantity") {
return searchNumeric(deps, docs, rq);
}
return searchText(docs, q);
}
export function initSearch(deps: SearchDeps): SearchController { export function initSearch(deps: SearchDeps): SearchController {
const overlay = $("#search-overlay"); const overlay = $("#search-overlay");
const input = $<HTMLInputElement>("#search-input"); const input = $<HTMLInputElement>("#search-input");