Skip to content

Commit bad0d1e

Browse files
committed
fix: review update
1 parent 60d8233 commit bad0d1e

2 files changed

Lines changed: 35 additions & 42 deletions

File tree

src/patternFly.search.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -145,12 +145,7 @@ const filterPatternFly = async (
145145
// Filter matching for resources and entries
146146
const byResource = new Map<string, PatternFlyMcpResourceFilteredMetadata>();
147147
const byEntry: (PatternFlyMcpDocsCatalogDoc & PatternFlyMcpDocsMeta)[] = [];
148-
const filterMatch = (propertyValue: string | number | undefined, filterValue: string) => {
149-
if (typeof propertyValue !== 'string' && typeof propertyValue !== 'number') {
150-
return false;
151-
}
152-
153-
// Coerce potential numbers to strings
148+
const filterMatch = (propertyValue: unknown, filterValue: string) => {
154149
const normalizePropertyValue = String(propertyValue).trim().toLowerCase();
155150

156151
return normalizePropertyValue === filterValue ||
@@ -160,7 +155,7 @@ const filterPatternFly = async (
160155

161156
for (const [name, resource] of resources) {
162157
const matchedEntries = resource.entries.filter(entry => {
163-
const matchesVersion = !updatedFilters.version || entry.version.toLowerCase() === updatedFilters.version;
158+
const matchesVersion = !updatedFilters.version || String(entry.version).toLowerCase() === updatedFilters.version;
164159
const matchesCategory = !updatedFilters.category || filterMatch(entry.category, updatedFilters.category);
165160
const matchesSection = !updatedFilters.section || filterMatch(entry.section, updatedFilters.section);
166161
const matchesName = !updatedFilters.name || filterMatch(entry.name, updatedFilters.name);
@@ -293,9 +288,13 @@ const searchPatternFly = async (searchQuery: unknown, filters?: FilterPatternFly
293288
// Apply filtering
294289
const { byResource } = await filterPatternFly(updatedFilters, searchResultsFilterMap);
295290

296-
// Loop filtered results, update search results.
297-
for (const [name, filteredData] of byResource) {
298-
const fuzzyMatch = fuzzyResultsMap.get(name);
291+
// Loop fuzzy results, apply and update search results with resources.
292+
for (const [name, fuzzyMatch] of fuzzyResultsMap) {
293+
const filteredData = byResource.get(name);
294+
295+
if (!filteredData) {
296+
continue;
297+
}
299298

300299
searchResultsMap.set(name, {
301300
...fuzzyMatch,

src/server.search.ts

Lines changed: 26 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
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: unknown): string;
9-
memo: (str: unknown) => string;
10-
}
11-
124
/**
135
* Options for closest search
6+
*
7+
* @interface ClosestSearchOptions
8+
*
9+
* @property missingReturnValue - The value to return when no match is found.
10+
* @property normalizeFn - Function to normalize strings for comparison.
1411
*/
1512
interface ClosestSearchOptions {
1613
missingReturnValue?: unknown;
@@ -53,17 +50,25 @@ interface FuzzySearch {
5350
/**
5451
* Options for fuzzy search
5552
*
56-
* - `maxDistance` - Maximum edit distance for a match. Distance is defined as
53+
* @interface FuzzySearchOptions
54+
*
55+
* @param allowEmptyQuery - Allow empty queries to match items with length <= maxDistance (default: `false`)
56+
* @param maxDistance - Maximum edit distance for a match. Distance is defined as
5757
* - exact = 0
5858
* - prefix = 1
5959
* - suffix = 1
6060
* - contains = 2
6161
* - partial = 2
6262
* - fuzzy = Levenshtein edit distance
63-
* - `maxResults` - Maximum number of results to return
64-
* - `normalizeFn` - Function to normalize values (default: `normalizeString`)
65-
* - `isExactMatch` | `isPrefixMatch` | `isSuffixMatch` | `isContainsMatch` | `isFuzzyMatch` - Enable specific match modes
66-
* - `deduplicateByNormalized` - If true, deduplicate results by normalized value instead of original string (default: false)
63+
* @param maxResults - Maximum number of results to return
64+
* @param normalizeFn - Function to normalize strings. Should always return a string (default: `normalizeString`)
65+
* @param isExactMatch - Include exact matches in results (default: `true`)
66+
* @param isPrefixMatch - Include prefix matches in results (default: `true`)
67+
* @param isSuffixMatch - Include suffix matches in results (default: `true`)
68+
* @param isContainsMatch - Include contains matches in results (default: `true`)
69+
* @param isPartialMatch - Include partial matches in results (default: `true`)
70+
* @param isFuzzyMatch - Allow fuzzy matches even when `maxDistance` is negative or zero.
71+
* @param deduplicateByNormalized - If `true`, deduplicate results by normalized value instead of original value.
6772
*/
6873
interface FuzzySearchOptions {
6974
allowEmptyQuery?: boolean;
@@ -80,16 +85,17 @@ interface FuzzySearchOptions {
8085
}
8186

8287
/**
83-
* Internal lightweight normalization: trim, lowercase, remove diacritics (a sign/accent character), squash separators
88+
* Internal lightweight normalization: coerce any value to string, trim, lowercase,
89+
* remove diacritics (a sign/accent character), squash separators.
8490
*
8591
* - Functions `findClosest` and `fuzzySearch` use this internally.
8692
* - Can be overridden in the `findClosest` and `fuzzySearch` related options for custom normalization.
8793
* - Function has a `memo` property to allow use as a memoized function.
8894
*
89-
* @param str - String or number to normalize.
90-
* @returns Normalized or empty string
95+
* @param str - Any value to normalize.
96+
* @returns Normalized string
9197
*/
92-
const normalizeString: NormalizeString = (str: unknown) => String(str)
98+
const normalizeString = (str: unknown) => String(str)
9399
.trim()
94100
.toLowerCase()
95101
.normalize('NFKD')
@@ -108,9 +114,9 @@ normalizeString.memo = memo(normalizeString, { cacheLimit: 50 });
108114
* - Returns the **first** original item whose normalized value equals the best normalized candidate.
109115
* - If multiple items normalize to the same value, only the first occurrence in the array is returned.
110116
* - For multiple matches, use `fuzzySearch` instead.
111-
* - Null/undefined items are normalized to empty strings to prevent runtime errors.
117+
* - Null/undefined items are coerced to strings to make them searchable.
112118
*
113-
* @param query - Search query string or number
119+
* @param query - Search query value
114120
* @param items - Array of strings and/or numbers to search
115121
* @param {ClosestSearchOptions} options - Search configuration options
116122
* @returns Closest matching item from items.
@@ -151,20 +157,9 @@ const findClosest = (
151157
* - Negative `maxDistance` values intentionally filter out all results, including exact matches.
152158
* - Empty-query fallback is allowed when `isFuzzyMatch` is true (items with length <= maxDistance can match).
153159
*
154-
* @param query - Search query string or number
160+
* @param query - Search query value
155161
* @param items - Array of strings and/or numbers to search
156162
* @param {FuzzySearchOptions} options - Search configuration options
157-
* @param options.allowEmptyQuery - Allow empty queries to match items with length <= maxDistance (default: `false`)
158-
* @param options.maxDistance - Maximum edit distance for a match. Distance is defined as
159-
* @param options.maxResults - Maximum number of results to return
160-
* @param {NormalizeString} options.normalizeFn - Function to normalize strings. Should always return a string or empty string (default: `normalizeString`)
161-
* @param options.isExactMatch - Include exact matches in results (default: `true`)
162-
* @param options.isPrefixMatch - Include prefix matches in results (default: `true`)
163-
* @param options.isSuffixMatch - Include suffix matches in results (default: `true`)
164-
* @param options.isContainsMatch - Include contains matches in results (default: `true`)
165-
* @param options.isPartialMatch - Include partial matches in results (default: `true`)
166-
* @param options.isFuzzyMatch - Allow fuzzy matches even when `maxDistance` is negative or zero.
167-
* @param options.deduplicateByNormalized - If `true`, deduplicate results by normalized value instead of original string.
168163
* @returns {FuzzySearch} An object containing search results with distance and match type
169164
* - `results`: Array of matching strings with distance and match type.
170165
* - `totalResults`: Total number of results found.
@@ -275,7 +270,6 @@ export {
275270
normalizeString,
276271
fuzzySearch,
277272
findClosest,
278-
type NormalizeString,
279273
type ClosestSearchOptions,
280274
type FuzzySearch,
281275
type FuzzySearchResult,

0 commit comments

Comments
 (0)