all repos — emoji @ 7a25cdeb568bc3347ea800ba141d97ebb7d87637

A minimalistic emoji package for Go (golang)

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