lightbend-labs / lightbend-emoji   1.3.0

GitHub

Lightbend Emoji library — a wrapper around Java's Unicode character handling

Scala versions: 3.x 2.13 2.12

Lightbend Emoji

Build Status Current Version

Lightbend Emoji is a wrapper around Java's Unicode handling.

Installation

Supported Scala versions: 2.12, 2.13, 3

Add to build.sbt, inserting current version:

libraryDependencies += "com.lightbend" %% "emoji" % "<version>"

Usage

$ sbt console
scala> import com.lightbend.emoji._
scala> import com.lightbend.emoji.Emoji.Implicits._

You can map Emoji directly from a Unicode character:

scala> Emoji(0x1f603)
res0: com.lightbend.emoji.Emoji = 😃

Or you can map the implicit from Int or String:

scala> 0x1f603.emoji
res1: com.lightbend.emoji.Emoji = 😃
scala> "0x1f603".codePointEmoji
res2: com.lightbend.emoji.Emoji = 😃

Once you have an emoji, you can ask it for codePoint (Int) or hexadecimal value:

scala> res2.hex
res3: String = 0x1f603
scala> res2.codePoint
res4: Int = 128515

Unicode codepoints are not very convenient, so there's a ShortCodes class which is designed to be used as an implicit parameter for emoji mapping.

There is a default mapping available, which allows you to map from a string directly to an emoji:

scala> import com.lightbend.emoji.ShortCodes.Implicits._
scala> import com.lightbend.emoji.ShortCodes.Defaults._
scala> "smiley".emoji
res5: com.lightbend.emoji.Emoji = 😃

Short codes are not very convenient, so there's an emoji interpolator for constructing strings using familiar syntax:

scala> e"Have a nice day! :smiley:"
res6: String = Have a nice day! 😃

It supports the same escapes as the standard interpolator, and is forgiving of colons that don't delimit a valid short name. The colon can be escaped with a backslash:

scala> e"Dear Customer: Have a nice day! :) :smiley:"
res7: String = Dear Customer: Have a nice day! :) 😃

scala> e"\:smiley\: is interpolated as :smiley:"
res8: String = :smiley: is interpolated as 😃

You can query the current mapping for short codes:

scala> ShortCodes.current.shortCodes.filter(_.startsWith("heart"))
res9: scala.collection.Set[String] = Set(heart_decoration, heart_eyes_cat, hearts, heart_eyes, heartpulse, heart, heartbeat)

Finally, you can also use your own short codes mapping:

scala> implicit val mycodes = new ShortCodes()
mycodes: com.lightbend.emoji.ShortCodes = com.lightbend.emoji.ShortCodes@49fd69f5
scala> mycodes.entry(Emoji(0x1f603), "yay")
scala> "yay".emoji
res1: com.lightbend.emoji.Emoji = 😃

Scala 3

Imports for extension methods are slightly shorter in Scala 3.

The default ShortCodes is given in ShortCodes.

scala> import com.lightbend.emoji.Emoji.*
scala> import com.lightbend.emoji.ShortCodes.{given, *}

Similar Works

These libraries have not been evaluated, and they may work or not:

Bugs

Sadly, there is no direct mapping to emoji-cheat-sheet or emoji searcher, because some emoji are mapped directly to glyphs, without Unicode involvement, e.g. :neckbeard:.

Typesafe Emojr

For an example of Lightbend Emoji in an Enterprise Mission Critical Environment, please see Typesafe Emojr.

Note that Typesafe Emojr has not been Lightbend-rebranded or ported to Scala 2.12+. To register your interest in an updated version, please contact Lightbend's enterprise sales team.