Query Matcher apply js-sdk's removeHiddenChars

Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>
This commit is contained in:
Michael Telatynski 2020-07-15 04:58:13 +01:00
parent 87069a9856
commit 31e0d74adc

View file

@ -18,16 +18,15 @@ limitations under the License.
import _at from 'lodash/at';
import _uniq from 'lodash/uniq';
function stripDiacritics(str: string): string {
return str.normalize('NFD').replace(/[\u0300-\u036f]/g, '');
}
import {removeHiddenChars} from "../../../matrix-js-sdk/src/utils";
interface IOptions<T extends {}> {
keys: Array<string | keyof T>;
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;
}
/**
@ -86,7 +85,7 @@ export default class QueryMatcher<T extends Object> {
for (const [index, keyValue] of Object.entries(keyValues)) {
if (!keyValue) continue; // skip falsy keyValues
const key = stripDiacritics(keyValue).toLowerCase();
const key = this.processQuery(keyValue);
if (!this._items.has(key)) {
this._items.set(key, []);
}
@ -99,7 +98,7 @@ export default class QueryMatcher<T extends Object> {
}
match(query: string): T[] {
query = stripDiacritics(query).toLowerCase();
query = this.processQuery(query);
if (this._options.shouldMatchWordsOnly) {
query = query.replace(/[^\w]/g, '');
}
@ -142,4 +141,11 @@ export default class QueryMatcher<T extends Object> {
// Now map the keys to the result objects. Also remove any duplicates.
return _uniq(matches.map((match) => match.object));
}
private processQuery(query: string): string {
if (this._options.fuzzy !== false) {
return removeHiddenChars(query).toLowerCase();
}
return query.toLowerCase();
}
}