* Search items with fuzzy matching
(query: string, options: SearchOptions = {})
| 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 |
no test coverage detected