1
0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo.git synced 2025-09-30 19:22:08 +00:00
forgejo/web_src/js/utils/match.js

39 lines
1.1 KiB
JavaScript
Raw Normal View History

import {emojiKeys} from '../features/emoji.js';
const maxMatches = 6;
function sortAndReduce(map) {
const sortedMap = new Map(Array.from(map.entries()).sort((a, b) => a[1] - b[1]));
return Array.from(sortedMap.keys()).slice(0, maxMatches);
}
export function matchEmoji(queryText) {
const query = queryText.toLowerCase().replaceAll('_', ' ');
if (!query) return emojiKeys.slice(0, maxMatches);
// results is a map of weights, lower is better
const results = new Map();
for (const emojiKey of emojiKeys) {
const index = emojiKey.replaceAll('_', ' ').indexOf(query);
if (index === -1) continue;
results.set(emojiKey, index);
}
return sortAndReduce(results);
}
export function matchMention(queryText) {
const query = queryText.toLowerCase();
// results is a map of weights, lower is better
const results = new Map();
for (const obj of window.config.mentionValues ?? []) {
const index = obj.key.toLowerCase().indexOf(query);
if (index === -1) continue;
const existing = results.get(obj);
results.set(obj, existing ? existing - index : index);
}
return sortAndReduce(results);
}