forked from tmdvs/Go-Emoji-Utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
emoji.go
88 lines (71 loc) · 1.86 KB
/
emoji.go
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 emojiutils
import (
"encoding/json"
"fmt"
"os"
"path"
"runtime"
"strings"
"github.com/teacat/emojiutils/utils"
)
// Emoji - Struct representing Emoji
type Emoji struct {
Key string `json:"key"`
Value string `json:"value"`
Descriptor string `json:"descriptor"`
}
var Emojis = make(map[string]Emoji)
// Unmarshal the emoji JSON into the Emojis map
func init() {
// Work out where we are in relation to the caller
_, filename, _, ok := runtime.Caller(0)
if !ok {
panic("No caller information")
}
// Open the Emoji definition JSON and Unmarshal into map
byteValue, e := os.ReadFile(path.Dir(filename) + "/data/emoji.json")
if e != nil {
panic(e)
}
err := json.Unmarshal(byteValue, &Emojis)
if err != nil {
panic(e)
}
}
// LookupEmoji - Lookup a single emoji definition
func LookupEmoji(emojiString string) (emoji Emoji, err error) {
hexKey := utils.StringToHexKey(emojiString)
// If we have a definition for this string we'll return it,
// else we'll return an error
if e, ok := Emojis[hexKey]; ok {
emoji = e
} else {
err = fmt.Errorf("no record for \"%s\" could be found", emojiString)
}
return emoji, err
}
// LookupEmojis - Lookup definitions for each emoji in the input
func LookupEmojis(emoji []string) (matches []interface{}) {
for _, emoji := range emoji {
if match, err := LookupEmoji(emoji); err == nil {
matches = append(matches, match)
} else {
matches = append(matches, err)
}
}
return
}
// RemoveAll - Remove all emoji
func RemoveAll(input string) string {
// Find all the emojis in this string
matches := FindAll(input)
for _, item := range matches {
emo := item.Match
for _, r := range emo.Value {
input = strings.ReplaceAll(input, string([]rune{r}), "")
}
}
// Remove and trim and left over whitespace
return strings.TrimSpace(strings.Join(strings.Fields(input), " "))
//return input
}