Exact prefix for 2-char emoji search, search short_names

This commit is contained in:
Alvaro 2022-10-17 10:54:46 -06:00 committed by GitHub
parent 4882248041
commit 0aa9351d4f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 3 deletions

View file

@ -222,11 +222,24 @@ const fuse = new Fuse(data, {
shouldSort: true, shouldSort: true,
threshold: 0.2, threshold: 0.2,
minMatchCharLength: 1, minMatchCharLength: 1,
keys: ['short_name'], keys: ['short_name', 'short_names'],
});
const fuseExactPrefix = new Fuse(data, {
shouldSort: true,
threshold: 0, // effectively a prefix search
minMatchCharLength: 2,
keys: ['short_name', 'short_names'],
}); });
export function search(query: string, count = 0): Array<EmojiData> { export function search(query: string, count = 0): Array<EmojiData> {
const results = fuse.search(query.substr(0, 32)).map(result => result.item); // when we only have 2 characters, do an exact prefix match
// to avoid matching on emoticon, like :-P
const fuseIndex = query.length === 2 ? fuseExactPrefix : fuse;
const results = fuseIndex
.search(query.substr(0, 32))
.map(result => result.item);
if (count) { if (count) {
return take(results, count); return take(results, count);

View file

@ -183,7 +183,7 @@ export class EmojiCompletion {
} }
} }
if (leftTokenText.length < 3) { if (leftTokenText.length < 2) {
this.reset(); this.reset();
return PASS_THROUGH; return PASS_THROUGH;
} }