Skip to content

Commit

Permalink
doc: add basic documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
enescakir committed Feb 16, 2020
1 parent 6e3e6ae commit 37cba89
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 33 deletions.
61 changes: 30 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,44 +10,43 @@ go get github.com/enescakir/emoji

## 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,
)
fmt.Printf("different skin tones. default: %v light: %v dark: %v\n",
emoji.ThumbsUp,
emoji.OkHand.Tone(emoji.Light),
emoji.CallMeHand.Tone(emoji.Dark),
)

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),
)
}

/* OUTPUT
hello 👋 from 🇹🇷
different skin tones. default: 👍 light: 👌🏻 dark: 🤙🏿
emoji with multiple skins: 🧑🏻‍🤝‍🧑🏿
*/

fmt.Printf("emoji with multiple skins: %v\n",
emoji.PeopleHoldingHands.Tone(emoji.Light, emoji.Dark),
)
}

/* OUTPUT
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:
Expand Down
4 changes: 4 additions & 0 deletions doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/*
Package emoji makes working with emojis easier.
*/
package emoji
15 changes: 13 additions & 2 deletions emoji.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,36 @@ import (
"strings"
)

// Base attributes
const (
unicodeFlagBaseIndex = 127397
)

// Skin tone colors
const (
Default Tone = ""
Light Tone = "\U0001F3FB"
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 {
Expand All @@ -43,12 +50,15 @@ func (e EmojiWithTone) Tone(tones ...Tone) EmojiWithTone {
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)
Expand All @@ -60,6 +70,7 @@ func CountryFlag(code string) (Emoji, error) {
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)))
}

0 comments on commit 37cba89

Please sign in to comment.