From b6b6ef38ed67dc2d7d4e5627089aebf0b3bf355c Mon Sep 17 00:00:00 2001 From: Thejesh Date: Fri, 27 Nov 2026 03:59:50 +0000 Subject: [PATCH] fix(web): exact emoji match ranked below partial matches in autocomplete (#40493) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `matchEmoji` combined alias scores wrongly. An exact match on the main alias scored 0, which is falsy, so a later matching alias overwrote it, and other multi-alias matches had their scores subtracted. Typing `:car` did not suggest πŸš— at all and `:boat` listed `rowboat` before `boat`. Each emoji now keeps its best alias score, and an exact match on any alias ranks first, like on GitHub, so `:uk` suggests πŸ‡¬πŸ‡§ before πŸ‡ΊπŸ‡¦. Assisted-by: Claude Code:claude-opus-5-5 --------- Co-authored-by: silverwind --- web_src/js/utils/match.test.ts | 11 +++++++++++ web_src/js/utils/match.ts | 11 +++++------ 2 files changed, 16 insertions(+), 6 deletions(-) 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);