Skip to content

Commit

Permalink
emoji: Refactor get_emoji_details_by_name function.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Lalit3716 authored and timabbott committed Apr 5, 2023
1 parent 30204ed commit 6f46c48
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 20 deletions.
38 changes: 18 additions & 20 deletions web/src/emoji.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,29 +217,27 @@ export function get_emoji_details_by_name(emoji_name) {
throw new Error("Emoji name must be passed.");
}

const emoji_info = {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);
emoji_info.emoji_code = emoji_code_info.id;
emoji_info.url = emoji_code_info.emoji_url;
if (emoji_code_info.still_url) {
emoji_info.still_url = emoji_code_info.still_url;
}
} else {
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_name,
emoji_code: emoji_code_info.id,
url: emoji_code_info.emoji_url,
still_url: emoji_code_info.still_url,
reaction_type: emoji_name === "zulip" ? "zulip_extra_emoji" : "realm_emoji",
};
}
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) {
Expand Down
2 changes: 2 additions & 0 deletions web/tests/emoji.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ run_test("get_emoji_details_by_name", () => {
reaction_type: "zulip_extra_emoji",
emoji_code: "zulip",
url: "/static/generated/emoji/images/emoji/unicode/zulip.png",
still_url: null,
});

// Test adding realm emoji.
Expand All @@ -97,6 +98,7 @@ run_test("get_emoji_details_by_name", () => {
reaction_type: "realm_emoji",
emoji_code: "102",
url: "/some/path/to/emoji",
still_url: null,
});

// Test sending without emoji name.
Expand Down
1 change: 1 addition & 0 deletions web/tests/reactions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,7 @@ test("sending", ({override, override_rewire}) => {
reaction_type: "zulip_extra_emoji",
emoji_name: "zulip",
emoji_code: "zulip",
still_url: null,
url: "/static/generated/emoji/images/emoji/unicode/zulip.png",
});
}
Expand Down

0 comments on commit 6f46c48

Please sign in to comment.