Skip to content

Commit 64b3c74

Browse files
committed
DEV annotations, typing
1 parent 12691f3 commit 64b3c74

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

src/server.search.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
import { distance, closest } from 'fastest-levenshtein';
22
import { memo } from './server.caching';
33

4+
/**
5+
* normalizeString function interface
6+
*/
7+
interface NormalizeString {
8+
(str: string): string;
9+
memo: (str: string) => string;
10+
}
11+
412
/**
513
* Options for closest search
614
*/
@@ -48,10 +56,11 @@ interface FuzzySearchOptions {
4856
*
4957
* - Functions `findClosest` and `fuzzySearch` use this internally.
5058
* - 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.
5160
*
5261
* @param str
5362
*/
54-
const normalizeString = (str: string) => String(str || '')
63+
const normalizeString: NormalizeString = (str: string) => String(str || '')
5564
.trim()
5665
.toLowerCase()
5766
.normalize('NFKD')
@@ -108,9 +117,9 @@ const findClosest = (
108117
* - Exact/prefix/suffix/contains are evaluated first with constant distances (0/1/1/2).
109118
* - Fuzzy distance is computed only when earlier classifications fail and only when the
110119
* 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).
114123
*
115124
* @param query - Search query string
116125
* @param items - Array of strings to search
@@ -208,6 +217,7 @@ export {
208217
normalizeString,
209218
fuzzySearch,
210219
findClosest,
220+
type NormalizeString,
211221
type ClosestSearchOptions,
212222
type FuzzySearchResult,
213223
type FuzzySearchOptions

0 commit comments

Comments
 (0)