( input: string, addOns: Array<AddOn>, )
| 66 | } |
| 67 | |
| 68 | function findClosestAddOn( |
| 69 | input: string, |
| 70 | addOns: Array<AddOn>, |
| 71 | ): string | undefined { |
| 72 | const inputLower = input.toLowerCase() |
| 73 | let bestMatch: string | undefined |
| 74 | let bestDistance = Infinity |
| 75 | |
| 76 | for (const addOn of addOns) { |
| 77 | const d = levenshtein(inputLower, addOn.id.toLowerCase()) |
| 78 | if (d < bestDistance) { |
| 79 | bestDistance = d |
| 80 | bestMatch = addOn.id |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | // Only suggest if the distance is reasonable (less than half the input length) |
| 85 | if (bestMatch && bestDistance <= Math.max(Math.floor(input.length / 2), 2)) { |
| 86 | return bestMatch |
| 87 | } |
| 88 | return undefined |
| 89 | } |
| 90 | |
| 91 | function levenshtein(a: string, b: string): number { |
| 92 | const m = a.length |
no test coverage detected