MCPcopy Create free account
hub / github.com/github/awesome-copilot / FuzzySearch

Class FuzzySearch

website/src/scripts/search.ts:29–170  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

27}
28
29export class FuzzySearch<T extends SearchableItem = SearchItem> {
30 private items: T[] = [];
31
32 constructor(items: T[] = []) {
33 this.items = items;
34 }
35
36 /**
37 * Update the items to search
38 */
39 setItems(items: T[]): void {
40 this.items = items;
41 }
42
43 /**
44 * Search items with fuzzy matching
45 */
46 search(query: string, options: SearchOptions = {}): T[] {
47 const {
48 fields = ["title", "description", "searchText"],
49 limit = 50,
50 minScore = 0,
51 } = options;
52
53 if (!query || query.trim().length === 0) {
54 return this.items.slice(0, limit);
55 }
56
57 const normalizedQuery = query.toLowerCase().trim();
58 const queryWords = normalizedQuery.split(/\s+/);
59 const results: Array<{ item: T; score: number }> = [];
60
61 for (const item of this.items) {
62 const score = this.calculateScore(item, queryWords, fields);
63 if (score > minScore) {
64 results.push({ item, score });
65 }
66 }
67
68 // Sort by score descending
69 results.sort((a, b) => b.score - a.score);
70
71 return results.slice(0, limit).map((r) => r.item);
72 }
73
74 /**
75 * Calculate match score for an item
76 */
77 private calculateScore(
78 item: T,
79 queryWords: string[],
80 fields: string[]
81 ): number {
82 let totalScore = 0;
83
84 for (const word of queryWords) {
85 let wordScore = 0;
86

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected