Skip to content

Commit 7b1d3ab

Browse files
committed
fix: review update
1 parent 429bcfc commit 7b1d3ab

3 files changed

Lines changed: 49 additions & 9 deletions

File tree

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,16 @@ exports[`findClosest should attempt to find a closest match, undefined items 1`]
9191
}
9292
`;
9393

94+
exports[`findClosest should handle numbers in addition to strings, float against float query 1`] = `123.44`;
95+
96+
exports[`findClosest should handle numbers in addition to strings, float number query 1`] = `123`;
97+
98+
exports[`findClosest should handle numbers in addition to strings, number query 1`] = `123`;
99+
100+
exports[`findClosest should handle numbers in addition to strings, number query with float 1`] = `123.45`;
101+
102+
exports[`findClosest should handle numbers in addition to strings, string query 1`] = `"Button"`;
103+
94104
exports[`fuzzySearch should fuzzy match, contains match multiple 1`] = `
95105
{
96106
"results": [
@@ -549,7 +559,7 @@ exports[`fuzzySearch should fuzzy match, undefined items 1`] = `
549559
}
550560
`;
551561

552-
exports[`fuzzySearch should handle numbers in addition to strings, exact number query with strings 1`] = `
562+
exports[`fuzzySearch should handle numbers in addition to strings, exact number query 1`] = `
553563
{
554564
"results": [
555565
{
@@ -591,7 +601,7 @@ exports[`fuzzySearch should handle numbers in addition to strings, prefix number
591601
}
592602
`;
593603

594-
exports[`fuzzySearch should handle numbers in addition to strings, string query with numbers 1`] = `
604+
exports[`fuzzySearch should handle numbers in addition to strings, string query 1`] = `
595605
{
596606
"results": [
597607
{

src/__tests__/server.search.test.ts

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ describe('findClosest', () => {
8282
])('should attempt to find a closest match, $description', ({ query, items }) => {
8383
expect({
8484
query,
85-
match: findClosest(query, items as string[])
85+
match: findClosest(query, items as any)
8686
}).toMatchSnapshot();
8787
});
8888

@@ -95,6 +95,36 @@ describe('findClosest', () => {
9595
findClosest('button', ['Button', 'Badge'], { normalizeFn: throwingNormalizeFn });
9696
}).toThrow('Normalization failed');
9797
});
98+
99+
it.each([
100+
{
101+
description: 'string query',
102+
query: 'button',
103+
items: ['Button', 123, 'Badge']
104+
},
105+
{
106+
description: 'number query',
107+
query: 123,
108+
items: ['Button', 123, 'Badge']
109+
},
110+
{
111+
description: 'number query with float',
112+
query: 123,
113+
items: ['Button', 123.45, 'Badge']
114+
},
115+
{
116+
description: 'float number query',
117+
query: 123.45,
118+
items: ['Button', 123, 'Badge']
119+
},
120+
{
121+
description: 'float against float query',
122+
query: 123.45,
123+
items: ['Button', 123, undefined, 123.44, 'Badge', null]
124+
}
125+
])('should handle numbers in addition to strings, $description', ({ query, items }) => {
126+
expect(findClosest(query, items as any)).toMatchSnapshot();
127+
});
98128
});
99129

100130
describe('fuzzySearch', () => {
@@ -312,7 +342,7 @@ describe('fuzzySearch', () => {
312342
}
313343
}
314344
])('should fuzzy match, $description', ({ query, items, options }) => {
315-
expect(fuzzySearch(query, items as string[], options)).toMatchSnapshot();
345+
expect(fuzzySearch(query, items as any, options)).toMatchSnapshot();
316346
});
317347

318348
it('should handle normalizeFn errors in fuzzySearch', () => {
@@ -327,12 +357,12 @@ describe('fuzzySearch', () => {
327357

328358
it.each([
329359
{
330-
description: 'string query with numbers',
360+
description: 'string query',
331361
query: 'button',
332362
items: ['Button', 123, 'Badge']
333363
},
334364
{
335-
description: 'exact number query with strings',
365+
description: 'exact number query',
336366
query: 123,
337367
items: ['Button', 123, 'Badge']
338368
},

src/server.search.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ interface FuzzySearchOptions {
8888
* @param str
8989
* @returns Normalized or empty string
9090
*/
91-
const normalizeString: NormalizeString = (str: string | number) => String(str || '')
91+
const normalizeString: NormalizeString = (str: string | number) => String(str ?? '')
9292
.trim()
9393
.toLowerCase()
9494
.normalize('NFKD')
@@ -121,7 +121,7 @@ normalizeString.memo = memo(normalizeString, { cacheLimit: 50 });
121121
* ```
122122
*/
123123
const findClosest = (
124-
query: string,
124+
query: string | number,
125125
items: (string | number)[] = [],
126126
{
127127
normalizeFn = normalizeString.memo
@@ -133,7 +133,7 @@ const findClosest = (
133133
return null;
134134
}
135135

136-
const normalizedItems = items.map(item => normalizeFn(item)).filter(Boolean);
136+
const normalizedItems = items.map(item => normalizeFn(item));
137137
const closestMatch = closest(normalizedQuery, normalizedItems);
138138

139139
return items[normalizedItems.indexOf(closestMatch)] || null;

0 commit comments

Comments
 (0)