Merge pull request #1364 from Choices-js/fix-kmp-not-returning-results-as-expected

Fix kmp not returning results as expected
This commit is contained in:
Xon 2026-01-01 01:48:21 +08:00 committed by GitHub
commit 907efec520
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 19 additions and 2 deletions

View file

@ -73,11 +73,12 @@ export class SearchByKMP<T extends object> implements Searcher<T> {
const field = this._fields[k];
if (field in obj && kmpSearch(needle, (obj[field] as string).toLowerCase()) !== -1) {
results.push({
item: obj[field],
item: obj,
score: count,
rank: count + 1,
});
count++;
break;
}
}
}

View file

@ -118,7 +118,15 @@ describe('search', () => {
});
it('label suffix', () => {
const results = searcher.search(`${haystack.length - 1}`);
expect(results.length).eq(2);
expect(results.length).eq(1);
});
it('search order', () => {
const results = searcher.search('label');
expect(results.length).eq(haystack.length);
haystack.forEach((value, index) => {
expect(results[index].item.value).eq(value.value);
});
});
});
@ -141,5 +149,13 @@ describe('search', () => {
const results = searcher.search(`${haystack.length - 1}`);
expect(results.length).eq(0);
});
it('search order', () => {
const results = searcher.search('label');
expect(results.length).eq(haystack.length);
haystack.forEach((value, index) => {
expect(results[index].item.value).eq(value.value);
});
});
});
});