all repos — emoji @ 37cba89abc97b27c86f58544f888c752c5db0228

A minimalistic emoji package for Go (golang)

doc: add basic documentation
Enes Çakır enes@cakir.web.tr
Mon, 17 Feb 2020 00:59:17 +0300
commit

37cba89abc97b27c86f58544f888c752c5db0228

parent

6e3e6aef7ad024165bb9dddb49e79d49b1689a5e

3 files changed, 44 insertions(+), 30 deletions(-)

jump to
M README.mdREADME.md

@@ -10,44 +10,43 @@ ```

## Usage :surfer: ```go - package main - - import ( - "fmt" - - "github.com/enescakir/emoji" +package main + +import ( + "fmt" + + "github.com/enescakir/emoji" +) + +func main() { + fmt.Printf("hello %v from %v\n", + emoji.WavingHand, + emoji.FlagsForFlagTurkey, ) - - func main() { - fmt.Printf("hello %v from %v\n", - emoji.WavingHand, - emoji.FlagsForFlagTurkey, - ) - fmt.Printf("different skin tones. default: %v light: %v dark: %v\n", - emoji.ThumbsUp, - emoji.OkHand.Tone(emoji.Light), - emoji.CallMeHand.Tone(emoji.Dark), - ) - fmt.Printf("emoji with multiple skins: %v\n", - emoji.PeopleHoldingHands.Tone(emoji.Light, emoji.Dark), - ) - } + fmt.Printf("different skin tones. default: %v light: %v dark: %v\n", + emoji.ThumbsUp, + emoji.OkHand.Tone(emoji.Light), + emoji.CallMeHand.Tone(emoji.Dark), + ) + fmt.Printf("emoji with multiple skins: %v\n", + emoji.PeopleHoldingHands.Tone(emoji.Light, emoji.Dark), + ) +} - /* OUTPUT +/* OUTPUT - hello 👋 from 🇹🇷 - different skin tones. default: 👍 light: 👌🏻 dark: 🤙🏿 - emoji with multiple skins: 🧑🏻‍🤝‍🧑🏿 + hello 👋 from 🇹🇷 + different skin tones. default: 👍 light: 👌🏻 dark: 🤙🏿 + emoji with multiple skins: 🧑🏻‍🤝‍🧑🏿 - */ - +*/ ``` This package contains Full Emoji List v12.0 based on [https://unicode.org/Public/emoji/12.0/emoji-test.txt](https://unicode.org/Public/emoji/12.0/emoji-test.txt). Also, you can generate country flag emoji with [ISO 3166 Alpha2](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2) codes: ```go - emoji.CountryFlag("tr") // 🇹🇷 +emoji.CountryFlag("tr") // 🇹🇷 ``` ## Testing :hammer:
A doc.go

@@ -0,0 +1,4 @@

+/* +Package emoji makes working with emojis easier. + */ +package emoji
M emoji.goemoji.go

@@ -6,6 +6,12 @@ "html"

"strings" ) +// Base attributes +const ( + unicodeFlagBaseIndex = 127397 +) + +// Skin tone colors const ( Default Tone = "" Light Tone = "\U0001F3FB"

@@ -13,22 +19,23 @@ MediumLight Tone = "\U0001F3FC"

Medium Tone = "\U0001F3FD" MediumDark Tone = "\U0001F3FE" Dark Tone = "\U0001F3FF" - - unicodeFlagBaseIndex = 127397 ) +// Emoji defines an emoji object. type Emoji string func (e Emoji) String() string { return string(e) } +// EmojiWithTone defines an emoji object that has skin tone options. type EmojiWithTone Emoji func (e EmojiWithTone) String() string { return strings.ReplaceAll(string(e), "@", string(Default)) } +// Tone returns an emoji object with given skin tone. func (e EmojiWithTone) Tone(tones ...Tone) EmojiWithTone { str := string(e) for _, tone := range tones {

@@ -43,12 +50,15 @@

return EmojiWithTone(str) } +// Tone defines skin tone options for emojis. type Tone string func (t Tone) String() string { return string(t) } +// CountryFlag returns a country flag emoji from given country code. +// Full list of country codes: https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2 func CountryFlag(code string) (Emoji, error) { if len(code) != 2 { return "", fmt.Errorf("not valid country code: %q", code)

@@ -60,6 +70,7 @@

return Emoji(flag), nil } +// countryCodeLetter shifts given letter byte as unicodeFlagBaseIndex and changes encoding func countryCodeLetter(l byte) string { return html.UnescapeString(fmt.Sprintf("&#%v;", unicodeFlagBaseIndex+int(l))) }