From 377b6c8a0537e0a92e09996d61670f1e398366a7 Mon Sep 17 00:00:00 2001 From: Germain Souquet Date: Wed, 31 Mar 2021 11:41:22 +0100 Subject: [PATCH 1/2] Make user autocomplete query search beyond prefix --- src/autocomplete/UserProvider.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/autocomplete/UserProvider.tsx b/src/autocomplete/UserProvider.tsx index 7fc01daef9..eeff1497c2 100644 --- a/src/autocomplete/UserProvider.tsx +++ b/src/autocomplete/UserProvider.tsx @@ -56,7 +56,7 @@ export default class UserProvider extends AutocompleteProvider { this.matcher = new QueryMatcher([], { keys: ['name'], funcs: [obj => obj.userId.slice(1)], // index by user id minus the leading '@' - shouldMatchPrefix: true, + shouldMatchPrefix: false, shouldMatchWordsOnly: false, }); From c81847689a2147a758d88626cbae32b520a32678 Mon Sep 17 00:00:00 2001 From: Germain Souquet Date: Wed, 31 Mar 2021 18:21:02 +0100 Subject: [PATCH 2/2] Remove query matcher shouldMatchPrefix support --- src/autocomplete/QueryMatcher.ts | 9 +-------- src/autocomplete/UserProvider.tsx | 1 - test/autocomplete/QueryMatcher-test.js | 14 -------------- 3 files changed, 1 insertion(+), 23 deletions(-) diff --git a/src/autocomplete/QueryMatcher.ts b/src/autocomplete/QueryMatcher.ts index a07ed29c7e..91fbea4d6a 100644 --- a/src/autocomplete/QueryMatcher.ts +++ b/src/autocomplete/QueryMatcher.ts @@ -23,7 +23,6 @@ interface IOptions { keys: Array; funcs?: Array<(T) => string>; shouldMatchWordsOnly?: boolean; - shouldMatchPrefix?: boolean; // whether to apply unhomoglyph and strip diacritics to fuzz up the search. Defaults to true fuzzy?: boolean; } @@ -56,12 +55,6 @@ export default class QueryMatcher { if (this._options.shouldMatchWordsOnly === undefined) { this._options.shouldMatchWordsOnly = true; } - - // By default, match anywhere in the string being searched. If enabled, only return - // matches that are prefixed with the query. - if (this._options.shouldMatchPrefix === undefined) { - this._options.shouldMatchPrefix = false; - } } setObjects(objects: T[]) { @@ -112,7 +105,7 @@ export default class QueryMatcher { resultKey = resultKey.replace(/[^\w]/g, ''); } const index = resultKey.indexOf(query); - if (index !== -1 && (!this._options.shouldMatchPrefix || index === 0)) { + if (index !== -1) { matches.push( ...candidates.map((candidate) => ({index, ...candidate})), ); diff --git a/src/autocomplete/UserProvider.tsx b/src/autocomplete/UserProvider.tsx index eeff1497c2..5f0cfc2df1 100644 --- a/src/autocomplete/UserProvider.tsx +++ b/src/autocomplete/UserProvider.tsx @@ -56,7 +56,6 @@ export default class UserProvider extends AutocompleteProvider { this.matcher = new QueryMatcher([], { keys: ['name'], funcs: [obj => obj.userId.slice(1)], // index by user id minus the leading '@' - shouldMatchPrefix: false, shouldMatchWordsOnly: false, }); diff --git a/test/autocomplete/QueryMatcher-test.js b/test/autocomplete/QueryMatcher-test.js index 2d0e10563b..3d383f08d7 100644 --- a/test/autocomplete/QueryMatcher-test.js +++ b/test/autocomplete/QueryMatcher-test.js @@ -183,18 +183,4 @@ describe('QueryMatcher', function() { expect(results.length).toBe(1); expect(results[0].name).toBe('bob'); }); - - it('Matches only by prefix with shouldMatchPrefix on', function() { - const qm = new QueryMatcher([ - {name: "Victoria"}, - {name: "Tori"}, - ], { - keys: ["name"], - shouldMatchPrefix: true, - }); - - const results = qm.match('tori'); - expect(results.length).toBe(1); - expect(results[0].name).toBe('Tori'); - }); });