emoji.go (view raw)
1package emoji
2
3import (
4 "fmt"
5 "strings"
6)
7
8// Base attributes
9const (
10 TonePlaceholder = "@"
11 flagBaseIndex = '\U0001F1E6' - 'a'
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 with no skin variations.
25type Emoji string
26
27// String returns string representation of the simple 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 struct {
34 oneTonedCode string
35 twoTonedCode string
36 defaultTone Tone
37}
38
39// newEmojiWithTone constructs a new emoji object that has skin tone options.
40func newEmojiWithTone(codes ...string) EmojiWithTone {
41 if len(codes) == 0 {
42 return EmojiWithTone{}
43 }
44
45 one := codes[0]
46 two := codes[0]
47
48 if len(codes) > 1 {
49 two = codes[1]
50 }
51
52 return EmojiWithTone{
53 oneTonedCode: one,
54 twoTonedCode: two,
55 }
56}
57
58// withDefaultTone sets default tone for an emoji and returns it.
59func (e EmojiWithTone) withDefaultTone(tone string) EmojiWithTone {
60 e.defaultTone = Tone(tone)
61
62 return e
63}
64
65// String returns string representation of the emoji with default skin tone.
66func (e EmojiWithTone) String() string {
67 return strings.ReplaceAll(e.oneTonedCode, TonePlaceholder, e.defaultTone.String())
68}
69
70// Tone returns string representation of the emoji with given skin tone.
71func (e EmojiWithTone) Tone(tones ...Tone) string {
72 // if no tone given, return with default skin tone
73 if len(tones) == 0 {
74 return e.String()
75 }
76
77 str := e.twoTonedCode
78 replaceCount := 1
79
80 // if one tone given or emoji doesn't have twoTonedCode, use oneTonedCode
81 // Also, replace all with one tone
82 if len(tones) == 1 {
83 str = e.oneTonedCode
84 replaceCount = -1
85 }
86
87 // replace tone one by one
88 for _, t := range tones {
89 // use emoji's default tone
90 if t == Default {
91 t = e.defaultTone
92 }
93
94 str = strings.Replace(str, TonePlaceholder, t.String(), replaceCount)
95 }
96
97 return str
98}
99
100// Tone defines skin tone options for emojis.
101type Tone string
102
103// String returns string representation of the skin tone.
104func (t Tone) String() string {
105 return string(t)
106}
107
108// CountryFlag returns a country flag emoji from given country code.
109// Full list of country codes: https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2
110func CountryFlag(code string) (Emoji, error) {
111 if len(code) != 2 {
112 return "", fmt.Errorf("not valid country code: %q", code)
113 }
114
115 code = strings.ToLower(code)
116 flag := countryCodeLetter(code[0]) + countryCodeLetter(code[1])
117
118 return Emoji(flag), nil
119}
120
121// countryCodeLetter shifts given letter byte as flagBaseIndex.
122func countryCodeLetter(l byte) string {
123 return string(rune(l) + flagBaseIndex)
124}