Port changes from enterprise codebase

This commit is contained in:
Tom Moor
2023-09-16 08:36:22 -04:00
parent 3582a6a0a2
commit 4fa3270f4e
3 changed files with 42 additions and 12 deletions

View File

@@ -0,0 +1,13 @@
import { getNameFromEmoji, getEmojiFromName } from "./emoji";
describe("getNameFromEmoji", () => {
it("returns the correct shortcode", () => {
expect(getNameFromEmoji("🤔")).toBe("thinking_face");
});
});
describe("getEmojiFromName", () => {
it("returns the correct native character", () => {
expect(getEmojiFromName("thinking_face")).toBe("🤔");
});
});

View File

@@ -17,11 +17,28 @@ export const snakeCase = (str: string) => str.replace(/(\w)(-)(\w)/g, "$1_$2");
* A map of emoji shortcode to emoji character. The shortcode is snake cased
* for backwards compatibility with those already encoded into documents.
*/
export const nameToEmoji = Object.values((data as EmojiMartData).emojis).reduce(
(acc, emoji) => {
const convertedId = snakeCase(emoji.id);
acc[emojiMartToGemoji[convertedId] ?? convertedId] = emoji.skins[0].native;
return acc;
},
{}
);
export const nameToEmoji: Record<string, string> = Object.values(
(data as EmojiMartData).emojis
).reduce((acc, emoji) => {
const convertedId = snakeCase(emoji.id);
acc[emojiMartToGemoji[convertedId] ?? convertedId] = emoji.skins[0].native;
return acc;
}, {});
/**
* Get the emoji character for a given emoji shortcode.
*
* @param name The emoji shortcode
* @returns The emoji character
*/
export const getEmojiFromName = (name: string) =>
nameToEmoji[name.replace(/:/g, "")];
/**
* Get the emoji shortcode for a given emoji character.
*
* @param emoji The emoji character
* @returns The emoji shortcode
*/
export const getNameFromEmoji = (emoji: string) =>
Object.entries(nameToEmoji).find(([, value]) => value === emoji)?.[0];