return string instead of EmojiWithTone for Tone method
Enes Çakır enes@cakir.web.tr
Wed, 19 Feb 2020 00:32:43 +0300
3 files changed,
23 insertions(+),
14 deletions(-)
M
emoji.go
→
emoji.go
@@ -24,6 +24,7 @@
// Emoji defines an emoji object. type Emoji string +// String returns string representation of the emoji. func (e Emoji) String() string { return string(e) }@@ -31,32 +32,38 @@
// EmojiWithTone defines an emoji object that has skin tone options. type EmojiWithTone Emoji +// String returns string representation of the emoji with default skin tone. func (e EmojiWithTone) String() string { return strings.ReplaceAll(string(e), "@", Default.String()) } -// Tone returns an emoji object with given skin tone. -func (e EmojiWithTone) Tone(tones ...Tone) EmojiWithTone { +// Tone returns string representation of the emoji with given skin tones. +func (e EmojiWithTone) Tone(tones ...Tone) string { str := string(e) + + // if no given tones, return with default skin tone if len(tones) == 0 { - tones = []Tone{Default} + return e.String() } - for _, tone := range tones { - str = strings.Replace(str, "@", tone.String(), 1) + // replace tone one by one + for _, t := range tones { + str = strings.Replace(str, "@", t.String(), 1) } + // if skin tone count is not enough, fill with last tone. if strings.Count(str, "@") > 0 { - lastTone := tones[len(tones)-1] - str = strings.ReplaceAll(str, "@", lastTone.String()) + last := tones[len(tones)-1] + str = strings.ReplaceAll(str, "@", last.String()) } - return EmojiWithTone(str) + return str } // Tone defines skin tone options for emojis. type Tone string +// String returns string representation of the skin tone. func (t Tone) String() string { return string(t) }@@ -76,5 +83,7 @@ }
// countryCodeLetter shifts given letter byte as unicodeFlagBaseIndex and changes encoding func countryCodeLetter(l byte) string { - return html.UnescapeString(fmt.Sprintf("&#%v;", unicodeFlagBaseIndex+int(l))) + shifted := unicodeFlagBaseIndex + int(l) + + return html.UnescapeString(fmt.Sprintf("&#%v;", shifted)) }
M
emoji_test.go
→
emoji_test.go
@@ -40,7 +40,7 @@ {input: WavingHand, tone: Dark, expected: "\U0001F44B\U0001F3FF"},
} for i, tc := range tt { - got := tc.input.Tone(tc.tone).String() + got := tc.input.Tone(tc.tone) if got != tc.expected { t.Fatalf("test case %v fail: got: %v, expected: %v", i+1, got, tc.expected) }@@ -59,7 +59,7 @@ {input: WomanAndManHoldingHandsWithTwoTone, tones: []Tone{Medium, Dark}, expected: "\U0001F469\U0001F3FD\u200D\U0001F91D\u200D\U0001F468\U0001F3FF"},
} for i, tc := range tt { - got := tc.input.Tone(tc.tones...).String() + got := tc.input.Tone(tc.tones...) if got != tc.expected { t.Fatalf("test case %v fail: got: %v, expected: %v", i+1, got, tc.expected) }@@ -114,13 +114,13 @@ }
func BenchmarkEmojiWithTone(b *testing.B) { for n := 0; n < b.N; n++ { - _ = WavingHand.Tone(Medium).String() + _ = WavingHand.Tone(Medium) } } func BenchmarkEmojiWithToneTwo(b *testing.B) { for n := 0; n < b.N; n++ { - _ = WomanAndManHoldingHandsWithTwoTone.Tone(Medium, Dark).String() + _ = WomanAndManHoldingHandsWithTwoTone.Tone(Medium, Dark) } }