-
Notifications
You must be signed in to change notification settings - Fork 37
/
emoji.go
99 lines (82 loc) · 2.09 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
89
90
91
92
93
94
95
96
97
98
99
package emoji
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path"
"runtime"
"strings"
"github.com/tmdvs/Go-Emoji-Utils/utils"
)
// Emoji - Struct representing Emoji
type Emoji struct {
Key string `json:"key"`
Value string `json:"value"`
Descriptor string `json:"descriptor"`
}
// 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
jsonFile, err := os.Open(path.Dir(filename) + "/data/emoji.json")
if jsonFile != nil {
defer jsonFile.Close()
}
if err != nil && len(Emojis) < 1 {
fmt.Println(err)
}
byteValue, e := ioutil.ReadAll(jsonFile)
if e != nil {
if len(Emojis) > 0 { // Use build-in emojis data (from emojidata.go)
return
}
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.(Emoji)
rs := []rune(emo.Value)
for _, r := range rs {
input = strings.ReplaceAll(input, string([]rune{r}), "")
}
}
// Remove and trim and left over whitespace
return strings.TrimSpace(strings.Join(strings.Fields(input), " "))
//return input
}