add comments and fix index

This commit is contained in:
ewwwwwwww 2022-07-09 16:32:34 -07:00
parent 63ee482982
commit b8a335a166
2 changed files with 17 additions and 6 deletions

View file

@ -100,10 +100,12 @@ export const emojifyText = (str: string, customEmojis = {}) => {
};
for (let c of split(str)) {
// convert FE0E selector to FE0F so it can be found in unimap
if (c.codePointAt(c.length - 1) === 65038) {
c = c.slice(0, -1) + String.fromCodePoint(65039);
}
// unqualified emojis aren't in emoji-mart's mappings so we just add FEOF
const unqualified = c + String.fromCodePoint(65039);
if (c in unicodeMapping) {

View file

@ -12,7 +12,7 @@ const index = new Index({
});
for (const [key, emoji] of Object.entries(data.emojis)) {
index.add(key, emoji.name);
index.add('n' + key, emoji.name);
}
export interface searchOptions {
@ -21,18 +21,27 @@ export interface searchOptions {
}
export const addCustomToPool = (customEmojis: any[]) => {
// @ts-ignore
for (const key in index.register) {
if (key[0] === 'c') {
index.remove(key); // remove old custom emojis
}
}
let i = 0;
for (const emoji of customEmojis) {
index.add(i++, emoji.id);
index.add('c' + i++, emoji.id);
}
};
// we can share an index by prefixing custom emojis with 'c' and native with 'n'
const search = (str: string, { maxResults = 5, custom }: searchOptions = {}, custom_emojis?: any): Emoji[] => {
return index.search(str, maxResults)
.flatMap(id => {
if (Number.isInteger(id)) {
const { shortcode, static_url } = custom_emojis.get(id).toJS();
// @ts-ignore
if (id[0] === 'c') {
const { shortcode, static_url } = custom_emojis.get((id as string).substr(1)).toJS();
return {
id: shortcode,
@ -42,10 +51,10 @@ const search = (str: string, { maxResults = 5, custom }: searchOptions = {}, cus
};
}
const { skins } = data.emojis[id];
const { skins } = data.emojis[(id as string).substr(1)];
return {
id: id as string,
id: (id as string).substr(1),
colons: ':' + id + ':',
unified: skins[0].unified,
native: skins[0].native,