README

Go-Emoji-Utils

Go Emoji Utils

A collection of useful functions for working with emoji. For example: look up the definition of a specific emoji, or search for all occurrences of emojis in a string.

Features

  • Find all occurrences of emoji in a string
  • Search a string for the presence specific emoji
  • Look up the definition of a single emoji
  • Look up the definitions for a list of emojis
  • Remove all emoji from a string
  • Import tool to update Emoji data with Emojipedia specs
  • Find the location of and occurrences of a specific emoji in a string

Examples

Find all occurrences of emoji in a string

You can search a string for all occurrences of emoji. You will be returned an array of results specifying which emojis were found, and how many times each occurred. The Locations property comprises of an array containing the start and end locations of each occurance of the emoji within the string you're searching.

input := "This is a string πŸ˜„ 🐷 with some πŸ‘πŸ» πŸ™ˆ emoji! 🐷 πŸƒπŸΏβ€β™‚οΈ"
result := emoji.FindAll(input)

// result: SearchResults{ SearchResult{ Match: Emoji{…}, Occurrences: 1, Locations: […] }, …}
Search a string for the presence specific emoji

You can search a string for the presence of a specific emoji. You will be returned a SearchResult struct with the definition of the matching emoji, how many times it occurred in the string, and its location within the string.

input := "This is a string πŸ˜„ 🐷 with some πŸ‘πŸ» πŸ™ˆ emoji! 🐷 πŸƒπŸΏβ€β™‚οΈ"
result := emoji.Find("🐷", input)

// result: SearchResult{ Match: Emoji{ Key:"1F437", Value:"🐷", Descriptor: "pig" }, Occurrences: 2, Locations: [[19 19] [42 42]  } }
Checking search results for the occurrence of a specific emoji

The SearchResults struct has an IndexOf method for conveniently checking whether or not a specific emoji appears within the results set. If the emoji is not found a position of -1 is returned.

input := "This is a string πŸ˜„ 🐷"
results := emoji.FindAll(input)

pigEmoji := emoji.Emojis["1F437"]
pigIndex := results.IndexOf(pigEmoji)
// pigIndex: 1
Look up the definition of a single emoji

You can lookup the definition of a single emoji character using the LookupEmoji function. If a matching emoji can't be found an error will be returned.

result, err := emoji.LookupEmoji("🐷")

// result: Emoji{ Key:"1F437", Value:"🐷", Descriptor: "pig" }
Look up the definitions for a list of emojis

You can lookup the definition of a for each emoji character in an array of emojis using the LookupEmojis function. This function is a convenience function that loops through the input slice and passes each emoji to the LookupEmoji function.

Results are returned in the same order that the input strings were provided in. If a matching emoji can't be found an error will be appear in for that position in the result slice.

result := emoji.LookupEmojis([]string{"🐷", "πŸ™ˆ"})

// result: []interface{}{ Emoji{ Key:"1F437", Value:"🐷", Descriptor: "pig" }, …}
Remove all emoji from a string

You can remove all the emoji characters from a string. This function finds all occurences of emoji using the FindAll function, and uses the Location field to remove runes at those indexes.

input := "This is a string πŸ˜„ 🐷 with some πŸ‘πŸ» πŸ™ˆ emoji! 🐷 πŸƒπŸΏβ€β™‚οΈ πŸ₯°"
output := emoji.RemoveAll(input)

// output: "This is a string with some emoji!"
Updating the Emoji definitions data file

Definitions of each emoji can be found in the data/emoji.json file. This JSON file is unmarshalled into a map at runtime with the emoji's hex representation as it's key. You can update the definitions file using definitions from Emojipedia with the import program bundled with this package. You can find it at import/main.go.

Expand β–Ύ Collapse β–΄

Documentation

Index ΒΆ

Constants ΒΆ

This section is empty.

Variables ΒΆ

var Emojis = map[string]Emoji{ /* 3174 elements not displayed */

}

Emojis - Map of Emoji Runes as Hex keys to their description


Functions ΒΆ

func LookupEmojis ΒΆ

func LookupEmojis(emoji []string) (matches []interface{})

LookupEmojis - Lookup definitions for each emoji in the input

func RemoveAll ΒΆ

func RemoveAll(input string) string

RemoveAll - Remove all emoji

Types ΒΆ

type Emoji ΒΆ

type Emoji struct {
	Key        string `json:"key"`
	Value      string `json:"value"`
	Descriptor string `json:"descriptor"`
}

Emoji - Struct representing Emoji

func LookupEmoji ΒΆ

func LookupEmoji(emojiString string) (emoji Emoji, err error)

LookupEmoji - Lookup a single emoji definition

type SearchResult ΒΆ

type SearchResult struct {
	Match       interface{}
	Occurrences int
	Locations   [][]int
}

SearchResult - Occurence of an emoji in a string

func Find ΒΆ

func Find(emojiString string, input string) (result SearchResult, err error)

Find a specific emoji character within a srting

type SearchResults ΒΆ

type SearchResults []SearchResult

SearchResults - The result of a search

func FindAll ΒΆ

func FindAll(input string) (detectedEmojis SearchResults)

FindAll - Find all instances of emoji

func (SearchResults) IndexOf ΒΆ

func (results SearchResults) IndexOf(result interface{}) int

IndexOf - Check to see if search results contains a specific element

Directories

Path Synopsis
import
utils Package utils - Functions for converting between Hex, String, and Rune representations of emoji