fix(web): exact emoji match ranked below partial matches in autocomplete (#40493)

`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 <me@silverwind.io>
This commit is contained in:
Thejesh
2026-11-27 03:59:50 +00:00
committed by GitHub
parent 073a966227
commit b6b6ef38ed
2 changed files with 16 additions and 6 deletions

View File

@@ -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 () => {

View File

@@ -19,14 +19,13 @@ export function matchEmoji(queryText: string): string[] {
// results is a map of weights, lower is better
const results = new Map<string, number>();
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);