emoji.go (view raw)
1package emoji
2
3import (
4 "fmt"
5 "html"
6 "strings"
7)
8
9// Base attributes
10const (
11 unicodeFlagBaseIndex = 127397
12)
13
14// Skin tone colors
15const (
16 Default Tone = ""
17 Light Tone = "\U0001F3FB"
18 MediumLight Tone = "\U0001F3FC"
19 Medium Tone = "\U0001F3FD"
20 MediumDark Tone = "\U0001F3FE"
21 Dark Tone = "\U0001F3FF"
22)
23
24// Emoji defines an emoji object.
25type Emoji string
26
27// String returns string representation of the emoji.
28func (e Emoji) String() string {
29 return string(e)
30}
31
32// EmojiWithTone defines an emoji object that has skin tone options.
33type EmojiWithTone Emoji
34
35// String returns string representation of the emoji with default skin tone.
36func (e EmojiWithTone) String() string {
37 return strings.ReplaceAll(string(e), "@", Default.String())
38}
39
40// Tone returns string representation of the emoji with given skin tones.
41func (e EmojiWithTone) Tone(tones ...Tone) string {
42 str := string(e)
43
44 // if no given tones, return with default skin tone
45 if len(tones) == 0 {
46 return e.String()
47 }
48
49 // replace tone one by one
50 for _, t := range tones {
51 str = strings.Replace(str, "@", t.String(), 1)
52 }
53
54 // if skin tone count is not enough, fill with last tone.
55 if strings.Count(str, "@") > 0 {
56 last := tones[len(tones)-1]
57 str = strings.ReplaceAll(str, "@", last.String())
58 }
59
60 return str
61}
62
63// Tone defines skin tone options for emojis.
64type Tone string
65
66// String returns string representation of the skin tone.
67func (t Tone) String() string {
68 return string(t)
69}
70
71// CountryFlag returns a country flag emoji from given country code.
72// Full list of country codes: https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2
73func CountryFlag(code string) (Emoji, error) {
74 if len(code) != 2 {
75 return "", fmt.Errorf("not valid country code: %q", code)
76 }
77
78 code = strings.ToUpper(code)
79 flag := countryCodeLetter(code[0]) + countryCodeLetter(code[1])
80
81 return Emoji(flag), nil
82}
83
84// countryCodeLetter shifts given letter byte as unicodeFlagBaseIndex and changes encoding
85func countryCodeLetter(l byte) string {
86 shifted := unicodeFlagBaseIndex + int(l)
87
88 return html.UnescapeString(fmt.Sprintf("&#%v;", shifted))
89}