|
1 | 1 | import { distance, closest } from 'fastest-levenshtein'; |
2 | 2 | import { memo } from './server.caching'; |
3 | 3 |
|
| 4 | +/** |
| 5 | + * normalizeString function interface |
| 6 | + */ |
| 7 | +interface NormalizeString { |
| 8 | + (str: string): string; |
| 9 | + memo: (str: string) => string; |
| 10 | +} |
| 11 | + |
4 | 12 | /** |
5 | 13 | * Options for closest search |
6 | 14 | */ |
@@ -48,10 +56,11 @@ interface FuzzySearchOptions { |
48 | 56 | * |
49 | 57 | * - Functions `findClosest` and `fuzzySearch` use this internally. |
50 | 58 | * - Can be overridden in the `findClosest` and `fuzzySearch` related options for custom normalization. |
| 59 | + * - Function has a `memo` property to allow use as a memoized function. |
51 | 60 | * |
52 | 61 | * @param str |
53 | 62 | */ |
54 | | -const normalizeString = (str: string) => String(str || '') |
| 63 | +const normalizeString: NormalizeString = (str: string) => String(str || '') |
55 | 64 | .trim() |
56 | 65 | .toLowerCase() |
57 | 66 | .normalize('NFKD') |
@@ -108,9 +117,9 @@ const findClosest = ( |
108 | 117 | * - Exact/prefix/suffix/contains are evaluated first with constant distances (0/1/1/2). |
109 | 118 | * - Fuzzy distance is computed only when earlier classifications fail and only when the |
110 | 119 | * string length delta is within `maxDistance` (cheap lower-bound check). |
111 | | - * - Global filter `distance <= maxDistance` applies to all match types. |
112 | | - * - Empty-query fallback: if `query` normalizes to `''` and `isFuzzyMatch` is true, |
113 | | - * items with length `<= maxDistance` can match (since `distance('', s) = s.length`). |
| 120 | + * - Global filter: result included only if its type is enabled AND distance <= maxDistance. |
| 121 | + * - Negative `maxDistance` values intentionally filter out all results, including exact matches. |
| 122 | + * - Empty-query fallback is allowed when `isFuzzyMatch` is true (items with length <= maxDistance can match). |
114 | 123 | * |
115 | 124 | * @param query - Search query string |
116 | 125 | * @param items - Array of strings to search |
@@ -208,6 +217,7 @@ export { |
208 | 217 | normalizeString, |
209 | 218 | fuzzySearch, |
210 | 219 | findClosest, |
| 220 | + type NormalizeString, |
211 | 221 | type ClosestSearchOptions, |
212 | 222 | type FuzzySearchResult, |
213 | 223 | type FuzzySearchOptions |
|
0 commit comments