code.gitea.io/gitea@v1.21.7/web_src/js/utils/match.js (about) 1 import emojis from '../../../assets/emoji.json'; 2 3 const maxMatches = 6; 4 5 function sortAndReduce(map) { 6 const sortedMap = new Map(Array.from(map.entries()).sort((a, b) => a[1] - b[1])); 7 return Array.from(sortedMap.keys()).slice(0, maxMatches); 8 } 9 10 export function matchEmoji(queryText) { 11 const query = queryText.toLowerCase().replaceAll('_', ' '); 12 if (!query) return emojis.slice(0, maxMatches).map((e) => e.aliases[0]); 13 14 // results is a map of weights, lower is better 15 const results = new Map(); 16 for (const {aliases} of emojis) { 17 const mainAlias = aliases[0]; 18 for (const [aliasIndex, alias] of aliases.entries()) { 19 const index = alias.replaceAll('_', ' ').indexOf(query); 20 if (index === -1) continue; 21 const existing = results.get(mainAlias); 22 const rankedIndex = index + aliasIndex; 23 results.set(mainAlias, existing ? existing - rankedIndex : rankedIndex); 24 } 25 } 26 27 return sortAndReduce(results); 28 } 29 30 export function matchMention(queryText) { 31 const query = queryText.toLowerCase(); 32 33 // results is a map of weights, lower is better 34 const results = new Map(); 35 for (const obj of window.config.mentionValues ?? []) { 36 const index = obj.key.toLowerCase().indexOf(query); 37 if (index === -1) continue; 38 const existing = results.get(obj); 39 results.set(obj, existing ? existing - index : index); 40 } 41 42 return sortAndReduce(results); 43 }