all repos — emoji @ 0235a1371768f43c7c89286e0881464088241b54

A minimalistic emoji package for Go (golang)

add flag-[CODE] emojis to parser
Enes Çakır enes@cakir.web.tr
Sun, 08 Mar 2020 21:16:50 +0300
commit

0235a1371768f43c7c89286e0881464088241b54

parent

2540f5f5afd4359e8d91211d80b850bab60d96aa

4 files changed, 48 insertions(+), 18 deletions(-)

jump to
M README.mdREADME.md

@@ -71,7 +71,7 @@ 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("US") // 🇺🇸 -emoji.CountryFlag("gb") // 🇬🇧 +emoji.Parse("country flag alias :flag-gb:") // country flag alias 🇬🇧 ``` All constants are generated by `internal/generator`.

@@ -82,7 +82,6 @@ go test

``` ## Todo :pushpin: -* Add `flag-[CODE]` support to emoji string parser * Add examples to `godoc` ## Contributing :man_technologist:
M emoji.goemoji.go

@@ -2,14 +2,13 @@ package emoji

import ( "fmt" - "html" "strings" ) // Base attributes const ( - unicodeFlagBaseIndex = 127397 - TonePlaceholder = "@" + TonePlaceholder = "@" + flagBaseIndex = '\U0001F1E6' - 'a' ) // Skin tone colors

@@ -113,15 +112,13 @@ if len(code) != 2 {

return "", fmt.Errorf("not valid country code: %q", code) } - code = strings.ToUpper(code) + code = strings.ToLower(code) flag := countryCodeLetter(code[0]) + countryCodeLetter(code[1]) return Emoji(flag), nil } -// countryCodeLetter shifts given letter byte as unicodeFlagBaseIndex and changes encoding. +// countryCodeLetter shifts given letter byte as flagBaseIndex. func countryCodeLetter(l byte) string { - shifted := unicodeFlagBaseIndex + int(l) - - return html.UnescapeString(fmt.Sprintf("&#%v;", shifted)) + return string(rune(l) + flagBaseIndex) }
M parser.goparser.go

@@ -2,8 +2,13 @@ package emoji

import ( "fmt" + "regexp" "strings" "unicode" +) + +var ( + flagRegex = regexp.MustCompile(`^:flag-([a-zA-Z]{2}):$`) ) // Parse replaces emoji aliases (:pizza:) with unicode representation.

@@ -39,19 +44,29 @@ continue

} // it's the end of the emoji alias - alias := matched.String() + ":" + match := matched.String() + alias := match + ":" - code, ok := emojiMap[alias] - if ok { + // check for emoji alias + if code, ok := emojiMap[alias]; ok { output.WriteString(code) matched.Reset() - } else { - // TODO: check for country codes: `flag-[a-z]{2}` - output.WriteString(matched.String()) - // it might be the beginning of the another emoji alias + continue + } + + // check for `flag-[CODE]` emoji + if flag := checkFlag(alias); len(flag) > 0 { + output.WriteString(flag) matched.Reset() - matched.WriteRune(r) + continue } + + // not found any emoji + output.WriteString(match) + // it might be the beginning of the another emoji alias + matched.Reset() + matched.WriteRune(r) + } // if matched not empty, add it to output

@@ -61,6 +76,17 @@ matched.Reset()

} return output.String() +} + +// checkFlag finds flag emoji for `flag-[CODE]` pattern +func checkFlag(alias string) string { + if matches := flagRegex.FindStringSubmatch(alias); len(matches) == 2 { + flag, _ := CountryFlag(matches[1]) + + return flag.String() + } + + return "" } // Map returns the emojis map.
M parser_test.goparser_test.go

@@ -55,6 +55,14 @@ input: "emoji with space :angry face_with_horns:anger_symbol:",

expected: fmt.Sprintf("emoji with space :angry face_with_horns%v", AngerSymbol), }, { + input: "flag testing :flag-tr: done", + expected: fmt.Sprintf("flag testing %v done", FlagForTurkey), + }, + { + input: "not valid flags :flag-tra: :flag-t: testing", + expected: fmt.Sprintf("not valid flags :flag-tra: :flag-t: testing"), + }, + { input: "dummytext", expected: "dummytext", },