summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/peterhellberg/emojilib/emojilib.go
blob: 29da98b5157e6dfec2195cb2108f20e708d406a0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*

Package emojilib is a port of the Emoji keyword library to Go

Installation

Just go get the package:

    go get -u github.com/peterhellberg/emojilib

Usage

A small usage example

		package main

		import (
			"fmt"

			"github.com/peterhellberg/emojilib"
		)

		func main() {
			fmt.Println(emojilib.ReplaceWithPadding("I :green_heart: You!"))
		}

*/
package emojilib

import "errors"

//go:generate go run _generator/main.go

// Emojis contain emojis keyed on their name
type Emojis map[string]Emoji

// Emoji contains the keywords, char and category for an emoji
type Emoji struct {
	Keywords []string `json:"keywords"`
	Char     string   `json:"char"`
	Category string   `json:"category"`
}

// ErrUnknownEmoji is returned from Find if provided with a unknown emoji name
var ErrUnknownEmoji = errors.New("unknown emoji")

// ErrUnknownKeyword is returned from Keyword if provided with a unknown keyword
var ErrUnknownKeyword = errors.New("unknown keyword")

// Find returns an Emoji if provided with a known name
func Find(n string) (Emoji, error) {
	if e, ok := emojis[n]; ok {
		return e, nil
	}

	return Emoji{}, ErrUnknownEmoji
}

// Keyword returns Emojis for the given keyword
func Keyword(k string) ([]Emoji, error) {
	if names, ok := keywordLookup[k]; ok {
		es := []Emoji{}

		for _, n := range names {
			es = append(es, emojis[n])
		}

		return es, nil
	}

	return []Emoji{}, ErrUnknownKeyword
}

// All returns all the emojis
func All() Emojis {
	return emojis
}

// Replace takes a string and replaces all emoji names with their emoji character
func Replace(s string) string {
	return emojiReplacer.Replace(s)
}

// ReplaceWithPadding takes a string and replaces all emoji names with their
// emoji character and a space in order to display better in terminals
func ReplaceWithPadding(s string) string {
	return emojiPaddedReplacer.Replace(s)
}