bigbuffet-rw/app/soapbox/features/emoji/mapping.ts

44 lines
1.1 KiB
TypeScript
Raw Normal View History

2022-07-04 16:07:04 -07:00
import data, { EmojiData } from './data';
2022-07-04 13:30:35 -07:00
2022-07-07 15:33:55 -07:00
const stripLeadingZeros = /^0+/;
function replaceAll(str: string, find: string, replace: string) {
return str.replace(new RegExp(find, 'g'), replace);
}
2022-07-04 16:07:04 -07:00
interface UnicodeMap {
2022-07-04 13:30:35 -07:00
[s: string]: {
unified: string,
shortcode: string,
}
}
2022-07-04 16:07:04 -07:00
export const generateMappings = (data: EmojiData): UnicodeMap => {
2022-07-04 13:30:35 -07:00
const result = {};
const emojis = Object.values(data.emojis ?? {});
for (const value of emojis) {
// @ts-ignore
for (const item of value.skins) {
const { unified, native } = item;
2022-07-07 15:33:55 -07:00
const stripped = unified.replace(stripLeadingZeros, '');
if (unified.includes('200d') && unified !== '1f441-fe0f-200d-1f5e8-fe0f') {
// @ts-ignore
result[native] = { unified: stripped, shortcode: value.id };
} else {
const twemojiCode = replaceAll(stripped, '-fe0f', '');
2022-07-04 13:30:35 -07:00
2022-07-07 15:33:55 -07:00
// @ts-ignore
result[native] = { unified: twemojiCode, shortcode: value.id };
}
2022-07-04 13:30:35 -07:00
}
}
return result;
};
2022-07-04 16:07:04 -07:00
const unicodeMapping = generateMappings(data);
2022-07-04 13:30:35 -07:00
export default unicodeMapping;