Skip to content

Emoticon conversion feature #101

Open
LinusAltemark wants to merge 2 commits intomainfrom
Convert-UTF-&-ASCII-emoticons
Open

Emoticon conversion feature #101
LinusAltemark wants to merge 2 commits intomainfrom
Convert-UTF-&-ASCII-emoticons

Conversation

@LinusAltemark
Copy link
Copy Markdown

@LinusAltemark LinusAltemark commented Feb 20, 2025

Summary by CodeRabbit

  • New Features

    • Introduced a utility for converting between UTF-8 and ASCII emoticons, enhancing text representation capabilities.
  • Tests

    • Added unit tests for the emoticon conversion utility, verifying functionality and handling of edge cases such as null and empty inputs.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Feb 20, 2025

Walkthrough

This pull request introduces a new utility class, EmojiConverter, in the org/fungover/breeze/util package. The class implements static methods for converting between UTF-8 and ASCII emoticons using thread-safe, immutable unmodifiable maps. Additionally, a set of unit tests are provided in EmojiConverterTest to verify the utility's behavior with valid, null, and empty inputs. The changes ensure robust handling of emoticon conversion without altering existing public interfaces.

Changes

File Path Change Summary
src/main/java/org/fungover/breeze/util/EmojiConverter.java Added a new utility class EmojiConverter with static methods (isAscii, isUtf, toAscii, toUtf) that convert UTF-8 emoticons to ASCII and vice versa using unmodifiable maps.
src/test/java/org/fungover/breeze/util/EmojiConverterTest.java Added a test class EmojiConverterTest with unit tests covering normal, null, and empty string scenarios for all conversion methods in EmojiConverter.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant EmojiConverter
    participant MapStore
    
    Client->>EmojiConverter: toAscii(emoji)
    EmojiConverter->>MapStore: Lookup UTF_TO_ASCII[emoji]
    MapStore-->>EmojiConverter: ASCII equivalent or null
    EmojiConverter-->>Client: Return ASCII result

    Client->>EmojiConverter: toUtf(emoji)
    EmojiConverter->>MapStore: Lookup ASCII_TO_UTF[emoji]
    MapStore-->>EmojiConverter: UTF equivalent or null
    EmojiConverter-->>Client: Return UTF result
Loading

Poem

I'm a bunny with a keyboard so bright,
Hopping through code in a digital light.
Emojis transformed with a flip and a cheer,
ASCII or UTF, the answer is clear.
With maps and tests, my code's pure delight,
Celebrating changes from morning to night! 🐇✨


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between bac72a8 and c47d74e.

📒 Files selected for processing (1)
  • src/main/java/org/fungover/breeze/util/EmojiConverter.java (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/main/java/org/fungover/breeze/util/EmojiConverter.java

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@LinusAltemark LinusAltemark linked an issue Feb 20, 2025 that may be closed by this pull request
6 tasks
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 5

🧹 Nitpick comments (2)
src/test/java/org/fungover/breeze/util/EmojiConverterTest.java (1)

6-43: Consider adding more test cases for edge cases.

While the current test coverage is good for the main functionality, consider adding test cases for:

  1. Mixed ASCII and UTF characters
  2. Special characters
  3. Whitespace-only strings
  4. Very long strings

Here's an example of additional test cases:

@Test
void testEdgeCases() {
    // Mixed ASCII and UTF
    String mixed = "Hello ツ World";
    assertFalse(EmojiConverter.isAscii(mixed));
    assertTrue(EmojiConverter.isUtf(mixed));

    // Special characters
    String special = "!@#$%^&*()";
    assertTrue(EmojiConverter.isAscii(special));
    assertFalse(EmojiConverter.isUtf(special));

    // Whitespace-only
    String whitespace = "   \t\n";
    assertTrue(EmojiConverter.isAscii(whitespace));
    assertFalse(EmojiConverter.isUtf(whitespace));

    // Very long string
    String longString = "x".repeat(1000);
    assertTrue(EmojiConverter.isAscii(longString));
    assertFalse(EmojiConverter.isUtf(longString));
}
src/main/java/org/fungover/breeze/util/EmojiConverter.java (1)

28-38: Consider making the emoticon mappings more extensible.

The current implementation hardcodes a limited set of emoticons. Consider making it more extensible by:

  1. Using a builder pattern to allow runtime addition of mappings
  2. Loading mappings from a configuration file
  3. Providing a method to register new mappings

This would make it easier to maintain and extend the emoticon set without modifying the code.

Would you like me to provide an example implementation of any of these approaches?

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 83521e0 and bac72a8.

📒 Files selected for processing (2)
  • src/main/java/org/fungover/breeze/util/EmojiConverter.java (1 hunks)
  • src/test/java/org/fungover/breeze/util/EmojiConverterTest.java (1 hunks)
🔇 Additional comments (4)
src/test/java/org/fungover/breeze/util/EmojiConverterTest.java (2)

20-25: LGTM!

The test cases for toAscii are well-structured and cover the main scenarios:

  • Converting known UTF emoticons to ASCII
  • Handling unknown input by returning it unchanged

27-32: LGTM!

The test cases for toUtf are well-structured and cover the main scenarios:

  • Converting known ASCII emoticons to UTF
  • Handling unknown input by returning it unchanged
src/main/java/org/fungover/breeze/util/EmojiConverter.java (2)

23-27: Well-designed utility class structure!

The class is correctly designed as a utility class with:

  • final class declaration to prevent inheritance
  • private and final fields for encapsulation
  • Static fields for shared state

44-82: Excellent method implementations!

The public methods are well-designed with:

  • Consistent null handling using Objects.toString
  • Clear and accurate Javadoc
  • Proper use of Map methods
  • Well-documented return values

Comment on lines +34 to +42
@Test
void testNullAndEmptyHandling() {
assertFalse(EmojiConverter.isAscii(null));
assertFalse(EmojiConverter.isUtf(null));
assertEquals("", EmojiConverter.toAscii(""));
assertEquals("", EmojiConverter.toUtf(""));
assertNull(null, EmojiConverter.toAscii(null));
assertNull(null, EmojiConverter.toUtf(null));
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fix incorrect assertion syntax in testNullAndEmptyHandling.

The assertNull assertions have incorrect syntax. The method takes only one argument (the actual value), but it's being called with two arguments.

Apply this diff to fix the assertions:

         assertFalse(EmojiConverter.isAscii(null));
         assertFalse(EmojiConverter.isUtf(null));
         assertEquals("", EmojiConverter.toAscii(""));
         assertEquals("", EmojiConverter.toUtf(""));
-        assertNull(null, EmojiConverter.toAscii(null));
-        assertNull(null, EmojiConverter.toUtf(null));
+        assertNull(EmojiConverter.toAscii(null));
+        assertNull(EmojiConverter.toUtf(null));
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
@Test
void testNullAndEmptyHandling() {
assertFalse(EmojiConverter.isAscii(null));
assertFalse(EmojiConverter.isUtf(null));
assertEquals("", EmojiConverter.toAscii(""));
assertEquals("", EmojiConverter.toUtf(""));
assertNull(null, EmojiConverter.toAscii(null));
assertNull(null, EmojiConverter.toUtf(null));
}
@Test
void testNullAndEmptyHandling() {
assertFalse(EmojiConverter.isAscii(null));
assertFalse(EmojiConverter.isUtf(null));
assertEquals("", EmojiConverter.toAscii(""));
assertEquals("", EmojiConverter.toUtf(""));
assertNull(EmojiConverter.toAscii(null));
assertNull(EmojiConverter.toUtf(null));
}

Comment on lines +14 to +18
@Test
void testIsUtf() {
assertTrue(EmojiConverter.isUtf("(shrug)"));
assertFalse(EmojiConverter.isUtf("¯\\_(ツ)_/¯"));
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fix incorrect test assertions in testIsUtf.

The test assertions appear to be reversed:

  • "(shrug)" is an ASCII string, not a UTF emoticon, so isUtf should return false
  • "¯\\_(ツ)_/¯" contains UTF characters (ツ), so isUtf should return true

Apply this diff to fix the assertions:

-        assertTrue(EmojiConverter.isUtf("(shrug)"));
-        assertFalse(EmojiConverter.isUtf("¯\\_(ツ)_/¯"));
+        assertFalse(EmojiConverter.isUtf("(shrug)"));
+        assertTrue(EmojiConverter.isUtf("¯\\_(ツ)_/¯"));
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
@Test
void testIsUtf() {
assertTrue(EmojiConverter.isUtf("(shrug)"));
assertFalse(EmojiConverter.isUtf(\\_(ツ)_/¯"));
}
@Test
void testIsUtf() {
assertFalse(EmojiConverter.isUtf("(shrug)"));
assertTrue(EmojiConverter.isUtf(\\_(ツ)_/¯"));
}

Comment on lines +8 to +12
@Test
void testIsAscii() {
assertTrue(EmojiConverter.isAscii("\\( ゚ヮ゚)/"));
assertFalse(EmojiConverter.isAscii("(shrug)"));
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fix incorrect test assertions in testIsAscii.

The test assertions appear to be reversed:

  • "\\( ゚ヮ゚)/" contains non-ASCII characters (゚ and ヮ), so isAscii should return false
  • "(shrug)" contains only ASCII characters, so isAscii should return true

Apply this diff to fix the assertions:

-        assertTrue(EmojiConverter.isAscii("\\( ゚ヮ゚)/"));
-        assertFalse(EmojiConverter.isAscii("(shrug)"));
+        assertFalse(EmojiConverter.isAscii("\\( ゚ヮ゚)/"));
+        assertTrue(EmojiConverter.isAscii("(shrug)"));
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
@Test
void testIsAscii() {
assertTrue(EmojiConverter.isAscii("\\( ゚ヮ゚)/"));
assertFalse(EmojiConverter.isAscii("(shrug)"));
}
@Test
void testIsAscii() {
- assertTrue(EmojiConverter.isAscii("\\( ゚ヮ゚)/"));
- assertFalse(EmojiConverter.isAscii("(shrug)"));
+ assertFalse(EmojiConverter.isAscii("\\( ゚ヮ゚)/"));
+ assertTrue(EmojiConverter.isAscii("(shrug)"));
}

Comment on lines +28 to +38
static {
Map<String, String> utfToAscii = new HashMap<>();
utfToAscii.put("(shrug)", "¯\\_(ツ)_/¯");
utfToAscii.put("(yay)", "\\( ゚ヮ゚)/");

UTF_TO_ASCII = Collections.unmodifiableMap(utfToAscii);

Map<String, String> asciiToUtf = new HashMap<>();
utfToAscii.forEach((utf, ascii) -> asciiToUtf.put(ascii, utf));
ASCII_TO_UTF = Collections.unmodifiableMap(asciiToUtf);
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fix potential thread-safety issue in static initialization.

The reverse mapping is created using the mutable utfToAscii map instead of the unmodifiable UTF_TO_ASCII. This could lead to inconsistent mappings if the forward map is modified after creating the reverse mapping but before making it unmodifiable.

Apply this diff to fix the initialization order:

     static {
         Map<String, String> utfToAscii = new HashMap<>();
         utfToAscii.put("(shrug)", "¯\\_(ツ)_/¯");
         utfToAscii.put("(yay)", "\\( ゚ヮ゚)/");
 
         UTF_TO_ASCII = Collections.unmodifiableMap(utfToAscii);
 
         Map<String, String> asciiToUtf = new HashMap<>();
-        utfToAscii.forEach((utf, ascii) -> asciiToUtf.put(ascii, utf));
+        UTF_TO_ASCII.forEach((utf, ascii) -> asciiToUtf.put(ascii, utf));
         ASCII_TO_UTF = Collections.unmodifiableMap(asciiToUtf);
     }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
static {
Map<String, String> utfToAscii = new HashMap<>();
utfToAscii.put("(shrug)", \\_(ツ)_/¯");
utfToAscii.put("(yay)", "\\( ゚ヮ゚)/");
UTF_TO_ASCII = Collections.unmodifiableMap(utfToAscii);
Map<String, String> asciiToUtf = new HashMap<>();
utfToAscii.forEach((utf, ascii) -> asciiToUtf.put(ascii, utf));
ASCII_TO_UTF = Collections.unmodifiableMap(asciiToUtf);
}
static {
Map<String, String> utfToAscii = new HashMap<>();
utfToAscii.put("(shrug)", \\_(ツ)_/¯");
utfToAscii.put("(yay)", "\\( ゚ヮ゚)/");
UTF_TO_ASCII = Collections.unmodifiableMap(utfToAscii);
Map<String, String> asciiToUtf = new HashMap<>();
UTF_TO_ASCII.forEach((utf, ascii) -> asciiToUtf.put(ascii, utf));
ASCII_TO_UTF = Collections.unmodifiableMap(asciiToUtf);
}

Comment on lines +8 to +22
/**
* Utility class for converting between UTF-8 emoticons and ASCII emoticons.
* Supports transformations similar to ASCIImoji.
* <p>
* Thread-safe and immutable.
* </p>
*
* <pre>
* String utf = "(shrug)";
* String ascii = "\\( ゚ヮ゚)/";
*
* EmojiConverter.toAscii(utf); // "¯\\_(ツ)_/¯"
* EmojiConverter.toUtf(ascii); // "(shrug)"
* </pre>
*/
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fix inconsistency in documentation example.

The example code shows \\( ゚ヮ゚)/ converting to "(shrug)", but in the implementation, this ASCII emoticon is actually mapped to "(yay)".

Apply this diff to fix the example:

  String utf = "(shrug)";
- String ascii = "\\( ゚ヮ゚)/";
+ String ascii = "¯\\_(ツ)_/¯";
 
  EmojiConverter.toAscii(utf);  // "¯\\_(ツ)_/¯"
- EmojiConverter.toUtf(ascii);  // "(shrug)"
+ EmojiConverter.toUtf(ascii);  // "(shrug)"

Committable suggestion skipped: line range outside the PR's diff.

@kappsegla kappsegla changed the title @Coderabbitai Emoticon conversion feature Feb 20, 2025
Comment thread src/main/java/org/fungover/breeze/util/EmojiConverter.java
Comment thread src/main/java/org/fungover/breeze/util/EmojiConverter.java Outdated
@martinez0466 martinez0466 self-requested a review February 20, 2025 12:21
martinez0466
martinez0466 previously approved these changes Feb 20, 2025
Copy link
Copy Markdown
Contributor

@martinez0466 martinez0466 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider explicitly handling null inputs in the conversion methods instead of relying on Objects.toString(emoji, "") for improved readability. This would make the intent clearer and avoid potential silent errors.

malvamalmgren
malvamalmgren previously approved these changes Feb 20, 2025
Copy link
Copy Markdown
Contributor

@malvamalmgren malvamalmgren left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree about the handling of nulls but it might be a taste thing, other than that it's a clear and efficient implementation!

@sonarqubecloud
Copy link
Copy Markdown

* @return true if it is an ASCII emoticon, false otherwise
*/
public static boolean isAscii(String emoji) {
return ASCII_TO_UTF.containsKey(Objects.toString(emoji, ""));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be more useful for the user of the library to receive an IllegalArgumentException instead of false for a null input, as that is probably a user mistake.

* @param emoji the emoji to check
* @return true if it is an ASCII emoticon, false otherwise
*/
public static boolean isAscii(String emoji) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The method name isAscii might be a bit missleading as most "Ascii" emojis contain non-Ascii characters. Something like isAsciiArt or isAsciiEmoji might be easier to understand. This might be why coderabbitai thinks that the following asserts should be changed:

-        assertTrue(EmojiConverter.isAscii("\\( ゚ヮ゚)/"));
-        assertFalse(EmojiConverter.isAscii("(shrug)"));
+        assertFalse(EmojiConverter.isAscii("\\( ゚ヮ゚)/"));
+        assertTrue(EmojiConverter.isAscii("(shrug)"));

If isAscii is taken litteraly then coderabbitai is correct.

Copy link
Copy Markdown
Contributor

@Rita8109 Rita8109 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have some comments on error handling and method names.

Copy link
Copy Markdown
Contributor

@Stelle83 Stelle83 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The class is well-documented with clear comments explaining its purpose and usage.
The class is designed to be immutable and thread-safe, which is a good practice for utility classes.

If the number of emoticons grows significantly, consider using a more efficient data structure or a database for storage and retrieval.

@Stelle83 Stelle83 enabled auto-merge February 23, 2025 21:03
@Stelle83
Copy link
Copy Markdown
Contributor

@LinusAltemark your commits are not verified!
It is not possible to merge before!

Most probably in your case you can use those commandos:
git rebase
git push --force

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Convert UTF & ASCII emoticons

5 participants