Skip to content

Commit dd11daf

Browse files
committed
fix: review update
1 parent 119ffdd commit dd11daf

6 files changed

Lines changed: 64 additions & 26 deletions

File tree

src/__tests__/__snapshots__/patternFly.getResources.test.ts.snap

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
22

3+
exports[`getPatternFlyComponentNames should return multiple organized facets: properties 1`] = `
4+
[
5+
"componentNamesIndex",
6+
"componentNamesIndexMap",
7+
"byVersion",
8+
]
9+
`;
10+
311
exports[`getPatternFlyMcpResources should return multiple organized facets: properties 1`] = `
412
[
513
"availableSemVer",
@@ -26,14 +34,6 @@ exports[`getPatternFlyMcpResources should return multiple organized facets: prop
2634
]
2735
`;
2836

29-
exports[`getPatternFlyReactComponentNames should return multiple organized facets: properties 1`] = `
30-
[
31-
"componentNamesIndex",
32-
"componentNamesIndexMap",
33-
"byVersion",
34-
]
35-
`;
36-
3737
exports[`setCategoryDisplayLabel should normalize categories and apply linking markdown, accessibility 1`] = `"Accessibility"`;
3838

3939
exports[`setCategoryDisplayLabel should normalize categories and apply linking markdown, design 1`] = `"Design Guidelines"`;

src/__tests__/__snapshots__/server.search.test.ts.snap

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
22

3+
exports[`findClosest should attempt to find a closest match, NaN match 1`] = `
4+
{
5+
"match": NaN,
6+
"query": NaN,
7+
}
8+
`;
9+
310
exports[`findClosest should attempt to find a closest match, all empty string items 1`] = `
411
{
5-
"match": null,
12+
"match": "",
613
"query": "test",
714
}
815
`;
@@ -70,6 +77,13 @@ exports[`findClosest should attempt to find a closest match, null items 1`] = `
7077
}
7178
`;
7279

80+
exports[`findClosest should attempt to find a closest match, null match 1`] = `
81+
{
82+
"match": null,
83+
"query": null,
84+
}
85+
`;
86+
7387
exports[`findClosest should attempt to find a closest match, partial query 1`] = `
7488
{
7589
"match": "Button",
@@ -91,6 +105,13 @@ exports[`findClosest should attempt to find a closest match, undefined items 1`]
91105
}
92106
`;
93107

108+
exports[`findClosest should attempt to find a closest match, undefined match 1`] = `
109+
{
110+
"match": undefined,
111+
"query": undefined,
112+
}
113+
`;
114+
94115
exports[`findClosest should handle numbers in addition to strings, float against float query 1`] = `123.44`;
95116

96117
exports[`findClosest should handle numbers in addition to strings, float number query 1`] = `123`;

src/__tests__/patternFly.getResources.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ describe('getPatternFlyComponentSchema', () => {
8484
});
8585
});
8686

87-
describe('getPatternFlyReactComponentNames', () => {
87+
describe('getPatternFlyComponentNames', () => {
8888
it('should return multiple organized facets', async () => {
8989
const result = await getPatternFlyComponentNames();
9090

src/__tests__/server.search.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,21 @@ describe('findClosest', () => {
7878
description: 'all empty string items',
7979
query: 'test',
8080
items: ['', '', '']
81+
},
82+
{
83+
description: 'undefined match',
84+
query: undefined,
85+
items: ['Button', 'Badge', undefined, null]
86+
},
87+
{
88+
description: 'null match',
89+
query: null,
90+
items: ['Button', 'Badge', undefined, null]
91+
},
92+
{
93+
description: 'NaN match',
94+
query: NaN,
95+
items: ['Button', 'Badge', NaN, null]
8196
}
8297
])('should attempt to find a closest match, $description', ({ query, items }) => {
8398
expect({

src/patternFly.search.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ interface SearchPatternFlyResults {
9494
* @property {Promise<PatternFlyMcpAvailableResources>} [mcpResources] - Object of multifaceted documentation entries to search.
9595
* @property [allowWildCardAll] - Allow a search query to match all components.
9696
* @property [maxDistance] - Maximum edit distance for fuzzy search.
97-
* @property [maxResults] - Maximum number of results to return.lts.
97+
* @property [maxResults] - Maximum number of results to return.
9898
*/
9999
interface SearchPatternFlyOptions {
100100
mcpResources?: Promise<PatternFlyMcpAvailableResources>;

src/server.search.ts

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,16 @@ import { memo } from './server.caching';
55
* normalizeString function interface
66
*/
77
interface 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
*/
1515
interface 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
*/
6768
interface 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
*/
123124
const 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
*/
180182
const 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

Comments
 (0)