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
27func (e Emoji) String() string {
28 return string(e)
29}
30
31// EmojiWithTone defines an emoji object that has skin tone options.
32type EmojiWithTone Emoji
33
34func (e EmojiWithTone) String() string {
35 return strings.ReplaceAll(string(e), "@", Default.String())
36}
37
38// Tone returns an emoji object with given skin tone.
39func (e EmojiWithTone) Tone(tones ...Tone) EmojiWithTone {
40 str := string(e)
41 if len(tones) == 0 {
42 tones = []Tone{Default}
43 }
44
45 for _, tone := range tones {
46 str = strings.Replace(str, "@", tone.String(), 1)
47 }
48
49 if strings.Count(str, "@") > 0 {
50 lastTone := tones[len(tones)-1]
51 str = strings.ReplaceAll(str, "@", lastTone.String())
52 }
53
54 return EmojiWithTone(str)
55}
56
57// Tone defines skin tone options for emojis.
58type Tone string
59
60func (t Tone) String() string {
61 return string(t)
62}
63
64// CountryFlag returns a country flag emoji from given country code.
65// Full list of country codes: https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2
66func CountryFlag(code string) (Emoji, error) {
67 if len(code) != 2 {
68 return "", fmt.Errorf("not valid country code: %q", code)
69 }
70
71 code = strings.ToUpper(code)
72 flag := countryCodeLetter(code[0]) + countryCodeLetter(code[1])
73
74 return Emoji(flag), nil
75}
76
77// countryCodeLetter shifts given letter byte as unicodeFlagBaseIndex and changes encoding
78func countryCodeLetter(l byte) string {
79 return html.UnescapeString(fmt.Sprintf("&#%v;", unicodeFlagBaseIndex+int(l)))
80}