From 0e9c43447f5e022dab8ab1c40f0add1e74385eb9 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Fri, 4 Sep 2026 07:13:47 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=B9=20[code=20health]=20Refactor=20ove?= =?UTF-8?q?rly=20long=20function=20searchAllSheets?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 🎯 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> --- src/ui/search.ts | 105 +++++++++++++++++++++++++---------------------- 1 file changed, 57 insertions(+), 48 deletions(-) diff --git a/src/ui/search.ts b/src/ui/search.ts index 1009ff1..70fcad5 100644 --- a/src/ui/search.ts +++ b/src/ui/search.ts @@ -44,57 +44,55 @@ export function parseResultQuery( return { op: m[1], threshold: v }; } -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") { - const th = rq.threshold.value; - const hits: SearchHit[] = []; - for (const doc of docs) { - const results = deps.workspace.getCachedResults(doc); - let lines: string[] | undefined; - for (let i = 0; i < results.length; i++) { - const r = results[i]; - if (!r.value || r.value.kind !== "quantity") continue; - const v = r.value.value; - let match = false; - switch (rq.op) { - case ">": - match = v.gt(th); - break; - case ">=": - match = v.gte(th); - break; - case "<": - match = v.lt(th); - break; - case "<=": - match = v.lte(th); - break; - case "=": - match = v.eq(th); - break; - case "~": - match = !th.isZero() && v.minus(th).abs().div(th.abs()).lte(0.01); - break; - } - if (match) { - if (!lines) lines = doc.text.split("\n"); - hits.push({ - docId: doc.id, - docTitle: doc.title, - line: i + 1, - text: lines[i] ?? "", - result: r.text ?? undefined, - }); - if (hits.length >= 200) return hits; - } +function searchNumeric(deps: SearchDeps, docs: SearchDoc[], rq: { op: string; threshold: Value }): SearchHit[] { + if (rq.threshold.kind !== "quantity") return []; + const th = rq.threshold.value; + const hits: SearchHit[] = []; + for (const doc of docs) { + const results = deps.workspace.getCachedResults(doc); + let lines: string[] | undefined; + for (let i = 0; i < results.length; i++) { + const r = results[i]; + if (!r.value || r.value.kind !== "quantity") continue; + const v = r.value.value; + let match = false; + switch (rq.op) { + case ">": + match = v.gt(th); + break; + case ">=": + match = v.gte(th); + break; + case "<": + match = v.lt(th); + break; + case "<=": + match = v.lte(th); + break; + case "=": + match = v.eq(th); + break; + case "~": + match = !th.isZero() && v.minus(th).abs().div(th.abs()).lte(0.01); + break; + } + if (match) { + if (!lines) lines = doc.text.split("\n"); + hits.push({ + docId: doc.id, + docTitle: doc.title, + line: i + 1, + text: lines[i] ?? "", + result: r.text ?? undefined, + }); + if (hits.length >= 200) return hits; } } - return hits; } + return hits; +} + +function searchText(docs: SearchDoc[], q: string): SearchHit[] { const ql = q.toLowerCase(); const hits: SearchHit[] = []; for (const doc of docs) { @@ -114,6 +112,17 @@ export function searchAllSheets(deps: SearchDeps, query: string): SearchHit[] { 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 { const overlay = $("#search-overlay"); const input = $("#search-input");