@@ -5,15 +5,16 @@ import { memo } from './server.caching';
55 * normalizeString function interface
66 */
77interface NormalizeString {
8- ( str : string | number ) : string ;
9- memo : ( str : string | number ) => string ;
8+ ( str : unknown ) : string ;
9+ memo : ( str : unknown ) => string ;
1010}
1111
1212/**
1313 * Options for closest search
1414 */
1515interface ClosestSearchOptions {
16- normalizeFn ?: ( str : string | number ) => string ;
16+ missingReturnValue ?: unknown ;
17+ normalizeFn ?: ( str : unknown ) => string ;
1718}
1819
1920/**
@@ -60,15 +61,15 @@ interface FuzzySearch {
6061 * - partial = 2
6162 * - fuzzy = Levenshtein edit distance
6263 * - `maxResults` - Maximum number of results to return
63- * - `normalizeFn` - Function to normalize strings (default: `normalizeString`)
64+ * - `normalizeFn` - Function to normalize values (default: `normalizeString`)
6465 * - `isExactMatch` | `isPrefixMatch` | `isSuffixMatch` | `isContainsMatch` | `isFuzzyMatch` - Enable specific match modes
6566 * - `deduplicateByNormalized` - If true, deduplicate results by normalized value instead of original string (default: false)
6667 */
6768interface FuzzySearchOptions {
6869 allowEmptyQuery ?: boolean ;
6970 maxDistance ?: number ;
7071 maxResults ?: number ;
71- normalizeFn ?: ( str : string | number ) => string ;
72+ normalizeFn ?: ( str : unknown ) => string ;
7273 isExactMatch ?: boolean ;
7374 isPrefixMatch ?: boolean ;
7475 isSuffixMatch ?: boolean ;
@@ -85,10 +86,10 @@ interface FuzzySearchOptions {
8586 * - Can be overridden in the `findClosest` and `fuzzySearch` related options for custom normalization.
8687 * - Function has a `memo` property to allow use as a memoized function.
8788 *
88- * @param str
89+ * @param str - String or number to normalize.
8990 * @returns Normalized or empty string
9091 */
91- const normalizeString : NormalizeString = ( str : string | number ) => String ( str ?? '' )
92+ const normalizeString : NormalizeString = ( str : unknown ) => String ( str )
9293 . trim ( )
9394 . toLowerCase ( )
9495 . normalize ( 'NFKD' )
@@ -112,7 +113,7 @@ normalizeString.memo = memo(normalizeString, { cacheLimit: 50 });
112113 * @param query - Search query string or number
113114 * @param items - Array of strings and/or numbers to search
114115 * @param {ClosestSearchOptions } options - Search configuration options
115- * @returns Closest matching string or number or null
116+ * @returns Closest matching item from items.
116117 *
117118 * @example
118119 * ```typescript
@@ -121,22 +122,23 @@ normalizeString.memo = memo(normalizeString, { cacheLimit: 50 });
121122 * ```
122123 */
123124const findClosest = (
124- query : string | number ,
125- items : ( string | number ) [ ] = [ ] ,
125+ query : unknown ,
126+ items : unknown [ ] = [ ] ,
126127 {
128+ missingReturnValue = null ,
127129 normalizeFn = normalizeString . memo
128130 } : ClosestSearchOptions = { }
129131) => {
130132 const normalizedQuery = normalizeFn ( query ) ;
131133
132- if ( ! normalizedQuery || ! Array . isArray ( items ) || items . length === 0 ) {
133- return null ;
134+ if ( normalizedQuery === '' || ! Array . isArray ( items ) || items . length === 0 ) {
135+ return missingReturnValue ;
134136 }
135137
136138 const normalizedItems = items . map ( item => normalizeFn ( item ) ) ;
137139 const closestMatch = closest ( normalizedQuery , normalizedItems ) ;
138140
139- return items [ normalizedItems . indexOf ( closestMatch ) ] || null ;
141+ return items [ normalizedItems . indexOf ( closestMatch ) ] ;
140142} ;
141143
142144/**
@@ -178,8 +180,8 @@ const findClosest = (
178180 * ```
179181 */
180182const fuzzySearch = (
181- query : string | number ,
182- items : ( string | number ) [ ] = [ ] ,
183+ query : unknown ,
184+ items : unknown [ ] = [ ] ,
183185 {
184186 allowEmptyQuery = false ,
185187 maxDistance = 3 ,
0 commit comments