gomoji

package module
v1.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 24, 2024 License: MIT Imports: 5 Imported by: 48

README

GoMoji 🔥

Tests Go Report codecov

GoMoji is a Go package that provides a 🚀fast and ✨simple way to work with emojis in strings. It has features such as:

Getting Started

Installing

To start using GoMoji, install Go and run go get:

$ go get -u github.com/forPelevin/gomoji

This will retrieve the package.

Check string contains emoji

package main

import (
    "github.com/forPelevin/gomoji"
)

func main() {
    res := gomoji.ContainsEmoji("hello world")
    println(res) // false
    
    res = gomoji.ContainsEmoji("hello world 🤗")
    println(res) // true
}

Find all

The function searches for all emoji occurrences in a string. It returns a nil slice if there are no emojis.

package main

import (
    "github.com/forPelevin/gomoji"
)

func main() {
    res := gomoji.FindAll("🧖 hello 🦋 world")
    println(res)
}

Result:

[]gomoji.Emoji{
    {
        Slug:        "person-in-steamy-room",
        Character:   "🧖",
        UnicodeName: "E5.0 person in steamy room",
        CodePoint:   "1F9D6",
        Group:       "People & Body",
        SubGroup:    "person-activity",
    },
    {
        Slug:        "butterfly",
        Character:   "🦋",
        UnicodeName: "E3.0 butterfly",
        CodePoint:   "1F98B",
        Group:       "Animals & Nature",
        SubGroup:    "animal-bug",
    },
}

Get all

The function returns all existing emojis. You can do whatever you need with the list.

package main

import (
    "github.com/forPelevin/gomoji"
)

func main() {
    emojis := gomoji.AllEmojis()
    println(emojis)
}

Remove all emojis

The function removes all emojis from given string:

res := gomoji.RemoveEmojis("🧖 hello 🦋world")
println(res) // "hello world"

Replace all emojis with a specified rune

The function replaces all emojis in a string with a given rune:

res := gomoji.ReplaceEmojisWith("🧖 hello 🦋world", '_')
println(res) // "_ hello _world"

Replace all emojis with the result of custom replacer function

The function replaces all emojis in a string with the result of given replacer function:

res := gomoji.ReplaceEmojisWithFunc("🧖 hello 🦋 world", func(em Emoji) string {
    return em.Slug
})
println(res) // "person-in-steamy-room hello butterfly world"

Get emoji info

The function returns info about provided emoji:

info, err := gomoji.GetInfo("1") // error: the string is not emoji
info, err := gomoji.GetInfo("1️⃣")
println(info)

Result:

gomoji.Entity{
    Slug:        "keycap-1",
    Character:   "1️⃣",
    UnicodeName: "E0.6 keycap: 1",
    CodePoint:   "0031 FE0F 20E3",
    Group:       "Symbols",
    SubGroup:    "keycap",
}

Emoji entity

All searching methods return the Emoji entity which contains comprehensive info about emoji.

type Emoji struct {
    Slug        string `json:"slug"`
    Character   string `json:"character"`
    UnicodeName string `json:"unicode_name"`
    CodePoint   string `json:"code_point"`
    Group       string `json:"group"`
    SubGroup    string `json:"sub_group"`
}

Example:

[]gomoji.Emoji{
    {
        Slug:        "butterfly",
        Character:   "🦋",
        UnicodeName: "E3.0 butterfly",
        CodePoint:   "1F98B",
        Group:       "Animals & Nature",
        SubGroup:    "animal-bug",
    },
    {
        Slug:        "roll-of-paper",
        Character:   "🧻",
        UnicodeName: "E11.0 roll of paper",
        CodePoint:   "1F9FB",
        Group:       "Objects",
        SubGroup:    "household",
    },
}

Performance

GoMoji Benchmarks

go test -bench=. -benchmem -v -run Benchmark ./...
goos: darwin
goarch: arm64
pkg: github.com/forPelevin/gomoji
BenchmarkContainsEmojiParallel
BenchmarkContainsEmojiParallel-10       150872365                7.207 ns/op           0 B/op          0 allocs/op
BenchmarkContainsEmoji
BenchmarkContainsEmoji-10               20428110                58.86 ns/op            0 B/op          0 allocs/op
BenchmarkRemoveEmojisParallel
BenchmarkRemoveEmojisParallel-10         6463779               193.1 ns/op            68 B/op          2 allocs/op
BenchmarkRemoveEmojis
BenchmarkRemoveEmojis-10                  882298              1341 ns/op              68 B/op          2 allocs/op
BenchmarkGetInfoParallel
BenchmarkGetInfoParallel-10             447353414                2.428 ns/op           0 B/op          0 allocs/op
BenchmarkGetInfo
BenchmarkGetInfo-10                     57733017                20.32 ns/op            0 B/op          0 allocs/op
BenchmarkFindAllParallel
BenchmarkFindAllParallel-10              4001587               305.8 ns/op           296 B/op          4 allocs/op
BenchmarkFindAll
BenchmarkFindAll-10                       694861              1675 ns/op             296 B/op          4 allocs/op
PASS
ok      github.com/forPelevin/gomoji    11.192s

Contact

Vlad Gukasov @vgukasov

License

GoMoji source code is available under the MIT License.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrStrNotEmoji = errors.New("the string is not emoji")
)

errors

Functions

func ContainsEmoji

func ContainsEmoji(s string) bool

ContainsEmoji checks whether given string contains emoji or not. It uses local emoji list as provider.

func RemoveEmojis added in v1.1.1

func RemoveEmojis(s string) string

RemoveEmojis removes all emojis from the s string and returns a new string.

func ReplaceEmojisWith added in v1.1.7

func ReplaceEmojisWith(s string, c rune) string

ReplaceEmojisWith replaces all emojis from the s string with the specified rune and returns a new string.

func ReplaceEmojisWithFunc added in v1.1.8

func ReplaceEmojisWithFunc(s string, replacer replacerFn) string

ReplaceEmojisWithFunc replaces all emojis from the s string with the result of the replacerFn function and returns a new string.

func ReplaceEmojisWithSlug added in v1.1.8

func ReplaceEmojisWithSlug(s string) string

ReplaceEmojisWithSlug replaces all emojis from the s string with the emoji's slug and returns a new string.

Types

type Emoji

type Emoji struct {
	Slug        string `json:"slug"`
	Character   string `json:"character"`
	UnicodeName string `json:"unicode_name"`
	CodePoint   string `json:"code_point"`
	Group       string `json:"group"`
	SubGroup    string `json:"sub_group"`
}

Emoji is an entity that represents comprehensive emoji info.

func AllEmojis

func AllEmojis() []Emoji

AllEmojis gets all emojis from provider.

func CollectAll added in v1.1.4

func CollectAll(s string) []Emoji

CollectAll finds all emojis in given string. Unlike FindAll, this does not distinct repeating occurrences of emoji. If there are no emojis it returns a nil-slice.

func FindAll

func FindAll(s string) []Emoji

FindAll finds all emojis in given string. If there are no emojis it returns a nil-slice.

func GetInfo added in v1.1.1

func GetInfo(emoji string) (Emoji, error)

GetInfo returns a gomoji.Emoji model representation of provided emoji. If the emoji was not found, it returns the gomoji.ErrStrNotEmoji error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL