Giter VIP home page Giter VIP logo

go-emoji-utils's Introduction

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.

Note: This is a fork of github.com/tmdvs/Go-Emoji-Utils. The purpose of this fork is to provide a reduced subset of functionality than its parent project, specifically:

  1. We do not ship a pre-compiled list of emojidata in the package itself. The expectation if that the developers uses our load function prior to any further calls in the package.
  2. emoji.json loading is not performed during init, as the expectation is this will be used in a broader set of environments where the data file configuration may be desirable, not linked to runtime locations, or require installation side-by-side to the binary.

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.