emoji: Refactor `get_emoji_details_by_name` function.

This is a prep commit for typescript migration of `emoji.js`. This
commit refactors the code for generating emoji rendering details such
that we avoid writing an ugly code which will involve writing an
incomplete type object when we migrate to TypeScript.
This commit is contained in:
Lalit 2023-04-05 16:21:26 +05:30 committed by Tim Abbott
parent 30204ed694
commit 6f46c4830f
3 changed files with 21 additions and 20 deletions

View File

@ -217,29 +217,27 @@ export function get_emoji_details_by_name(emoji_name) {
throw new Error("Emoji name must be passed."); throw new Error("Emoji name must be passed.");
} }
const emoji_info = {emoji_name};
if (active_realm_emojis.has(emoji_name)) { if (active_realm_emojis.has(emoji_name)) {
if (emoji_name === "zulip") {
emoji_info.reaction_type = "zulip_extra_emoji";
} else {
emoji_info.reaction_type = "realm_emoji";
}
const emoji_code_info = active_realm_emojis.get(emoji_name); const emoji_code_info = active_realm_emojis.get(emoji_name);
emoji_info.emoji_code = emoji_code_info.id; return {
emoji_info.url = emoji_code_info.emoji_url; emoji_name,
if (emoji_code_info.still_url) { emoji_code: emoji_code_info.id,
emoji_info.still_url = emoji_code_info.still_url; url: emoji_code_info.emoji_url,
} still_url: emoji_code_info.still_url,
} else { reaction_type: emoji_name === "zulip" ? "zulip_extra_emoji" : "realm_emoji",
const codepoint = get_emoji_codepoint(emoji_name); };
if (codepoint === undefined) {
throw new Error("Bad emoji name: " + emoji_name);
}
emoji_info.reaction_type = "unicode_emoji";
emoji_info.emoji_code = codepoint;
} }
return emoji_info;
const codepoint = get_emoji_codepoint(emoji_name);
if (codepoint === undefined) {
throw new Error("Bad emoji name: " + emoji_name);
}
return {
emoji_name,
reaction_type: "unicode_emoji",
emoji_code: codepoint,
};
} }
export function get_emoji_details_for_rendering(opts) { export function get_emoji_details_for_rendering(opts) {

View File

@ -76,6 +76,7 @@ run_test("get_emoji_details_by_name", () => {
reaction_type: "zulip_extra_emoji", reaction_type: "zulip_extra_emoji",
emoji_code: "zulip", emoji_code: "zulip",
url: "/static/generated/emoji/images/emoji/unicode/zulip.png", url: "/static/generated/emoji/images/emoji/unicode/zulip.png",
still_url: null,
}); });
// Test adding realm emoji. // Test adding realm emoji.
@ -97,6 +98,7 @@ run_test("get_emoji_details_by_name", () => {
reaction_type: "realm_emoji", reaction_type: "realm_emoji",
emoji_code: "102", emoji_code: "102",
url: "/some/path/to/emoji", url: "/some/path/to/emoji",
still_url: null,
}); });
// Test sending without emoji name. // Test sending without emoji name.

View File

@ -358,6 +358,7 @@ test("sending", ({override, override_rewire}) => {
reaction_type: "zulip_extra_emoji", reaction_type: "zulip_extra_emoji",
emoji_name: "zulip", emoji_name: "zulip",
emoji_code: "zulip", emoji_code: "zulip",
still_url: null,
url: "/static/generated/emoji/images/emoji/unicode/zulip.png", url: "/static/generated/emoji/images/emoji/unicode/zulip.png",
}); });
} }