xonecas.io
6615
Yc

yoctoselect / commit 1d11db1

feat: prefix filter logic with tests

xonecas2026-03-21 1d11db14833875a7650cb5c6707e3c1ab496d15c parent cd319e4

2 files changed

src/filter.test.ts+62−0
@@ -0,0 +1,62 @@
1+import { describe, expect, test } from "bun:test";
2+import { normalizeItems, prefixFilter } from "./filter.ts";
3+
4+describe("normalizeItems", () => {
5+ test("normalizes string items", () => {
6+ const result = normalizeItems(["foo", "bar"]);
7+ expect(result).toEqual([
8+ { label: "foo", value: "foo", filterText: "foo" },
9+ { label: "bar", value: "bar", filterText: "bar" },
10+ ]);
11+ });
12+
13+ test("normalizes {label, value} items, filterText defaults to label", () => {
14+ const result = normalizeItems([{ label: "Foo Label", value: "foo" }]);
15+ expect(result).toEqual([
16+ { label: "Foo Label", value: "foo", filterText: "Foo Label" },
17+ ]);
18+ });
19+
20+ test("preserves explicit filterText", () => {
21+ const result = normalizeItems([
22+ { label: "colored", value: "v", filterText: "plain" },
23+ ]);
24+ expect(result[0]!.filterText).toBe("plain");
25+ });
26+});
27+
28+describe("prefixFilter", () => {
29+ const items = normalizeItems(["apple", "apricot", "banana", "Avocado"]);
30+
31+ test("empty query returns all items", () => {
32+ expect(prefixFilter(items, "")).toEqual(items);
33+ });
34+
35+ test("prefix match", () => {
36+ const result = prefixFilter(items, "ap");
37+ expect(result.map((i) => i.value)).toEqual(["apple", "apricot"]);
38+ });
39+
40+ test("case insensitive", () => {
41+ const result = prefixFilter(items, "AV");
42+ expect(result.map((i) => i.value)).toEqual(["Avocado"]);
43+ });
44+
45+ test("no match returns empty", () => {
46+ expect(prefixFilter(items, "xyz")).toEqual([]);
47+ });
48+
49+ test("matches against filterText, not label", () => {
50+ const rich = normalizeItems([
51+ {
52+ label: "\x1b[31mClaude\x1b[0m",
53+ value: "claude",
54+ filterText: "claude-sonnet-4",
55+ },
56+ ]);
57+ expect(prefixFilter(rich, "claude-s").map((i) => i.value)).toEqual([
58+ "claude",
59+ ]);
60+ expect(prefixFilter(rich, "\x1b[31m")).toEqual([]);
61+ });
62+});
src/filter.ts+29−0
@@ -0,0 +1,29 @@
1+import type { SelectItem } from "./types.ts";
2+
3+export interface NormalizedItem {
4+ label: string;
5+ value: string;
6+ filterText: string;
7+}
8+
9+export function normalizeItems(items: SelectItem[]): NormalizedItem[] {
10+ return items.map((item) => {
11+ if (typeof item === "string") {
12+ return { label: item, value: item, filterText: item };
13+ }
14+ return {
15+ label: item.label,
16+ value: item.value,
17+ filterText: item.filterText ?? item.label,
18+ };
19+ });
20+}
21+
22+export function prefixFilter(
23+ items: NormalizedItem[],
24+ query: string,
25+): NormalizedItem[] {
26+ if (!query) return items;
27+ const q = query.toLowerCase();
28+ return items.filter((item) => item.filterText.toLowerCase().startsWith(q));
29+}