emoji.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package emoji
  2. import (
  3. "fmt"
  4. "strings"
  5. )
  6. // Base attributes
  7. const (
  8. TonePlaceholder = "@"
  9. flagBaseIndex = '\U0001F1E6' - 'a'
  10. )
  11. // Skin tone colors
  12. const (
  13. Default Tone = ""
  14. Light Tone = "\U0001F3FB"
  15. MediumLight Tone = "\U0001F3FC"
  16. Medium Tone = "\U0001F3FD"
  17. MediumDark Tone = "\U0001F3FE"
  18. Dark Tone = "\U0001F3FF"
  19. )
  20. // Emoji defines an emoji object with no skin variations.
  21. type Emoji string
  22. // String returns string representation of the simple emoji.
  23. func (e Emoji) String() string {
  24. return string(e)
  25. }
  26. // EmojiWithTone defines an emoji object that has skin tone options.
  27. type EmojiWithTone struct {
  28. oneTonedCode string
  29. twoTonedCode string
  30. defaultTone Tone
  31. }
  32. // newEmojiWithTone constructs a new emoji object that has skin tone options.
  33. func newEmojiWithTone(codes ...string) EmojiWithTone {
  34. if len(codes) == 0 {
  35. return EmojiWithTone{}
  36. }
  37. one := codes[0]
  38. two := codes[0]
  39. if len(codes) > 1 {
  40. two = codes[1]
  41. }
  42. return EmojiWithTone{
  43. oneTonedCode: one,
  44. twoTonedCode: two,
  45. }
  46. }
  47. // withDefaultTone sets default tone for an emoji and returns it.
  48. func (e EmojiWithTone) withDefaultTone(tone string) EmojiWithTone {
  49. e.defaultTone = Tone(tone)
  50. return e
  51. }
  52. // String returns string representation of the emoji with default skin tone.
  53. func (e EmojiWithTone) String() string {
  54. return strings.ReplaceAll(e.oneTonedCode, TonePlaceholder, e.defaultTone.String())
  55. }
  56. // Tone returns string representation of the emoji with given skin tone.
  57. func (e EmojiWithTone) Tone(tones ...Tone) string {
  58. // if no tone given, return with default skin tone
  59. if len(tones) == 0 {
  60. return e.String()
  61. }
  62. str := e.twoTonedCode
  63. replaceCount := 1
  64. // if one tone given or emoji doesn't have twoTonedCode, use oneTonedCode
  65. // Also, replace all with one tone
  66. if len(tones) == 1 {
  67. str = e.oneTonedCode
  68. replaceCount = -1
  69. }
  70. // replace tone one by one
  71. for _, t := range tones {
  72. // use emoji's default tone
  73. if t == Default {
  74. t = e.defaultTone
  75. }
  76. str = strings.Replace(str, TonePlaceholder, t.String(), replaceCount)
  77. }
  78. return str
  79. }
  80. // Tone defines skin tone options for emojis.
  81. type Tone string
  82. // String returns string representation of the skin tone.
  83. func (t Tone) String() string {
  84. return string(t)
  85. }
  86. // CountryFlag returns a country flag emoji from given country code.
  87. // Full list of country codes: https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2
  88. func CountryFlag(code string) (Emoji, error) {
  89. if len(code) != 2 {
  90. return "", fmt.Errorf("not valid country code: %q", code)
  91. }
  92. code = strings.ToLower(code)
  93. flag := countryCodeLetter(code[0]) + countryCodeLetter(code[1])
  94. return Emoji(flag), nil
  95. }
  96. // countryCodeLetter shifts given letter byte as flagBaseIndex.
  97. func countryCodeLetter(l byte) string {
  98. return string(rune(l) + flagBaseIndex)
  99. }