diff --git a/web_src/js/utils/match.test.ts b/web_src/js/utils/match.test.ts index b4d1e1a69bd..c3bab617a48 100644 --- a/web_src/js/utils/match.test.ts +++ b/web_src/js/utils/match.test.ts @@ -67,6 +67,17 @@ test('matchEmoji', () => { "jellyfish", ] `); + + expect(matchEmoji('boat')).toMatchInlineSnapshot(` + [ + "boat", + "rowboat", + "speedboat", + "motor_boat", + ] + `); + + expect(['uk', 'raised_hand'].map((query) => matchEmoji(query)[0])).toEqual(['gb', 'hand']); }); test('matchMention', async () => { diff --git a/web_src/js/utils/match.ts b/web_src/js/utils/match.ts index 63b2f6478cf..90d30db3587 100644 --- a/web_src/js/utils/match.ts +++ b/web_src/js/utils/match.ts @@ -19,14 +19,13 @@ export function matchEmoji(queryText: string): string[] { // results is a map of weights, lower is better const results = new Map(); for (const {aliases} of emojis) { - const mainAlias = aliases[0]; + let rank = Infinity; for (const [aliasIndex, alias] of aliases.entries()) { - const index = alias.replaceAll('_', ' ').indexOf(query); - if (index === -1) continue; - const existing = results.get(mainAlias); - const rankedIndex = index + aliasIndex; - results.set(mainAlias, existing ? existing - rankedIndex : rankedIndex); + const normalizedAlias = alias.replaceAll('_', ' '); + const index = normalizedAlias.indexOf(query); + if (index !== -1) rank = Math.min(rank, normalizedAlias === query ? -1 : index + aliasIndex); } + if (rank !== Infinity) results.set(aliases[0], rank); } return sortAndReduce(results);