(nodes: NodeDataSchema[], searchValue: string)
| 86 | * Score and sort nodes by fuzzy search relevance |
| 87 | */ |
| 88 | export function searchNodes(nodes: NodeDataSchema[], searchValue: string): NodeDataSchema[] { |
| 89 | // Return all nodes unsorted if search is empty |
| 90 | if (!searchValue || searchValue.trim() === '') { |
| 91 | return nodes |
| 92 | } |
| 93 | |
| 94 | // Calculate fuzzy scores for each node |
| 95 | const nodesWithScores = nodes.map((nd) => { |
| 96 | const nameScore = fuzzyScore(searchValue, nd.name) |
| 97 | const labelScore = fuzzyScore(searchValue, nd.label) |
| 98 | const categoryScore = fuzzyScore(searchValue, nd.category || '') * 0.5 // Lower weight for category |
| 99 | const descriptionScore = fuzzyScore(searchValue, nd.description || '') * 0.3 // Even lower for description |
| 100 | const maxScore = Math.max(nameScore, labelScore, categoryScore, descriptionScore) |
| 101 | |
| 102 | return { node: nd, score: maxScore } |
| 103 | }) |
| 104 | |
| 105 | // Filter nodes with score > 0 and sort by score (highest first) |
| 106 | return nodesWithScores |
| 107 | .filter((item) => item.score > 0) |
| 108 | .sort((a, b) => b.score - a.score) |
| 109 | .map((item) => item.node) |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Debounce function for search input |
no test coverage detected