mirror of
https://github.com/Paillat-dev/dismoji.git
synced 2026-01-01 16:46:20 +00:00
🐛 Fix some complex emojis not being converted and enhance test coverage (#19)
This commit is contained in:
@@ -12,10 +12,13 @@ EMOJIS_PATH = Path(__file__).parent / "raw" / "build" / "emojis.json"
|
||||
with EMOJIS_PATH.open("r", encoding="utf-8") as f:
|
||||
EMOJIS = json.load(f)
|
||||
|
||||
EMOJI_MAPPING: dict[str, str] = {k: EMOJIS["emojis"][v]["surrogates"] for k, v in EMOJIS["nameToEmoji"].items()}
|
||||
_VARIATION_SELECTOR = "\ufe0f" # We remove this as it is not needed by discord and causes issues with tests
|
||||
EMOJI_MAPPING: dict[str, str] = {
|
||||
k: EMOJIS["emojis"][v]["surrogates"].replace(_VARIATION_SELECTOR, "") for k, v in EMOJIS["nameToEmoji"].items()
|
||||
}
|
||||
|
||||
# Create a reverse mapping for demojizing (emoji to name)
|
||||
REVERSE_EMOJI_MAPPING: dict[str, str] = {}
|
||||
|
||||
for emoji_index_str, emoji_index in sorted(EMOJIS["surrogateToEmoji"].items(), key=lambda x: len(x[0]), reverse=True):
|
||||
# Get the first name in the list as the preferred name
|
||||
e = EMOJIS["emojis"][emoji_index]
|
||||
@@ -23,9 +26,9 @@ for emoji_index_str, emoji_index in sorted(EMOJIS["surrogateToEmoji"].items(), k
|
||||
# e.g. :handshake_light_skin_tone_dark_skin_tone: vs :handshake_tone1_tone5:
|
||||
REVERSE_EMOJI_MAPPING[emoji_index_str] = e["names"][-1 if e.get("hasMultiDiversityParent") else 0]
|
||||
|
||||
del EMOJIS # Clean up to save memory
|
||||
del EMOJIS, _VARIATION_SELECTOR # Clean up to save memory
|
||||
|
||||
EMOJI_PATTERN = re.compile(r":([a-zA-Z0-9_-]+):")
|
||||
EMOJI_PATTERN = re.compile(r":([\w+-]+):")
|
||||
|
||||
EMOJI_CHARS_PATTERN = re.compile("|".join(map(re.escape, REVERSE_EMOJI_MAPPING.keys())))
|
||||
|
||||
|
||||
@@ -1,7 +1,26 @@
|
||||
# Copyright (c) Paillat-dev
|
||||
# SPDX-License-Identifier: MIT
|
||||
|
||||
from dismoji import REVERSE_EMOJI_MAPPING, demojize, emojize
|
||||
from dismoji import EMOJI_MAPPING, REVERSE_EMOJI_MAPPING, demojize, emojize
|
||||
|
||||
|
||||
def are_equal(a: str, b: str) -> bool:
|
||||
"""Check if two emojis are equal.
|
||||
|
||||
Allows for comparing emojis with modifiers even when they are in different orders.
|
||||
|
||||
Args:
|
||||
a (str): First emoji string.
|
||||
b (str): Second emoji string.
|
||||
|
||||
Returns:
|
||||
bool: True if the emojis are equal, False otherwise.
|
||||
"""
|
||||
if len(a) != len(b):
|
||||
return False
|
||||
if len(a) == 1:
|
||||
return a == b
|
||||
return a[0] == b[0] and set(a[1:]) == set(b[1:])
|
||||
|
||||
|
||||
def test_basic() -> None:
|
||||
@@ -70,6 +89,11 @@ def test_emoji_with_special_characters() -> None:
|
||||
assert emojize(input_str) == expected_output
|
||||
|
||||
|
||||
def test_emojize_all() -> None:
|
||||
for name, emoji in EMOJI_MAPPING.items():
|
||||
assert are_equal(emojize(f":{name}:"), emoji)
|
||||
|
||||
|
||||
def test_demojize_basic() -> None:
|
||||
"""Test basic functionality of demojize function."""
|
||||
assert demojize("Hello 😄") == "Hello :smile:"
|
||||
|
||||
Reference in New Issue
Block a user