Merge pull request #5822 from matrix-org/gsouquet-user-autocomplete-nonprefix

This commit is contained in:
Germain 2021-04-01 14:01:50 +01:00 committed by GitHub
commit 151749b675
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 1 additions and 23 deletions

View file

@ -23,7 +23,6 @@ interface IOptions<T extends {}> {
keys: Array<string | keyof T>; keys: Array<string | keyof T>;
funcs?: Array<(T) => string>; funcs?: Array<(T) => string>;
shouldMatchWordsOnly?: boolean; shouldMatchWordsOnly?: boolean;
shouldMatchPrefix?: boolean;
// whether to apply unhomoglyph and strip diacritics to fuzz up the search. Defaults to true // whether to apply unhomoglyph and strip diacritics to fuzz up the search. Defaults to true
fuzzy?: boolean; fuzzy?: boolean;
} }
@ -56,12 +55,6 @@ export default class QueryMatcher<T extends Object> {
if (this._options.shouldMatchWordsOnly === undefined) { if (this._options.shouldMatchWordsOnly === undefined) {
this._options.shouldMatchWordsOnly = true; 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[]) { setObjects(objects: T[]) {
@ -112,7 +105,7 @@ export default class QueryMatcher<T extends Object> {
resultKey = resultKey.replace(/[^\w]/g, ''); resultKey = resultKey.replace(/[^\w]/g, '');
} }
const index = resultKey.indexOf(query); const index = resultKey.indexOf(query);
if (index !== -1 && (!this._options.shouldMatchPrefix || index === 0)) { if (index !== -1) {
matches.push( matches.push(
...candidates.map((candidate) => ({index, ...candidate})), ...candidates.map((candidate) => ({index, ...candidate})),
); );

View file

@ -56,7 +56,6 @@ export default class UserProvider extends AutocompleteProvider {
this.matcher = new QueryMatcher([], { this.matcher = new QueryMatcher([], {
keys: ['name'], keys: ['name'],
funcs: [obj => obj.userId.slice(1)], // index by user id minus the leading '@' funcs: [obj => obj.userId.slice(1)], // index by user id minus the leading '@'
shouldMatchPrefix: true,
shouldMatchWordsOnly: false, shouldMatchWordsOnly: false,
}); });

View file

@ -183,18 +183,4 @@ describe('QueryMatcher', function() {
expect(results.length).toBe(1); expect(results.length).toBe(1);
expect(results[0].name).toBe('bob'); 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');
});
}); });