Skip to content

Commit

Permalink
Merge pull request #35 from joshblum/joshblum/revmap
Browse files Browse the repository at this point in the history
ReverseCodeMap, map unicode character to lists of short codes
  • Loading branch information
kyokomi committed Apr 2, 2020
2 parents f2b747d + 051f4c9 commit 8803a15
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 2 deletions.
45 changes: 44 additions & 1 deletion emoji.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"io"
"regexp"
"sort"
"unicode"
)

Expand All @@ -22,6 +23,48 @@ func CodeMap() map[string]string {
return emojiCodeMap
}

// emojiRevCodeMap maps unicode characters to lists of short codes.
var emojiRevCodeMap = make(map[string][]string, len(emojiCodeMap))

func init() {
for shortCode, unicode := range emojiCodeMap {
emojiRevCodeMap[unicode] = append(emojiRevCodeMap[unicode], shortCode)
}
// ensure deterministic ordering for aliases
for _, value := range emojiRevCodeMap {
sort.Slice(value, func(i, j int) bool {
if len(value[i]) == len(value[j]) {
return value[i] < value[j]
}
return len(value[i]) < len(value[j])
})
}
}

// RevCodeMap gets the underlying map of emoji.
func RevCodeMap() map[string][]string {
return emojiRevCodeMap
}

func AliasList(shortCode string) []string {
return emojiRevCodeMap[emojiCodeMap[shortCode]]
}

// HasAlias flags if the given `shortCode` has multiple aliases with other
// codes.
func HasAlias(shortCode string) bool {
return len(AliasList(shortCode)) > 1
}

// NormalizeShortCode normalizes a given `shortCode` to a deterministic alias.
func NormalizeShortCode(shortCode string) string {
shortLists := AliasList(shortCode)
if len(shortLists) == 0 {
return shortCode
}
return shortLists[0]
}

// regular expression that matches :flag-[countrycode]:
var flagRegexp = regexp.MustCompile(":flag-([a-z]{2}):")

Expand Down Expand Up @@ -99,7 +142,7 @@ func Println(a ...interface{}) (int, error) {

// Printf is fmt.Printf which supports emoji
func Printf(format string, a ...interface{}) (int, error) {
return fmt.Printf(compile(fmt.Sprintf(format, a...)))
return fmt.Print(compile(fmt.Sprintf(format, a...)))
}

// Fprint is fmt.Fprint which supports emoji
Expand Down
34 changes: 33 additions & 1 deletion emoji_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,38 @@ func TestCodeMap(t *testing.T) {
}
}

func TestRevCodeMap(t *testing.T) {
m := RevCodeMap()
if &emojiRevCodeMap == &m {
t.Error("emojiRevCodeMap != EmojiRevCodeMap")
}
}

func TestHasAlias(t *testing.T) {
hasAlias := HasAlias(":+1:")
if !hasAlias {
t.Error(":+1: doesn't have an alias")
}
hasAlias = HasAlias(":no-good:")
if hasAlias {
t.Error(":no-good: has an alias")
}
}

func TestNoramlizeShortCode(t *testing.T) {
test := ":thumbs_up:"
expected := ":+1:"
normalized := NormalizeShortCode(test)
if normalized != expected {
t.Errorf("Normalized %q != %q", test, expected)
}
test = ":no-good:"
normalized = NormalizeShortCode(test)
if normalized != test {
t.Errorf("Normalized %q != %q", test, normalized)
}
}

func TestPrint(t *testing.T) {
_, err := Print(beerKey, beerText)
if err != nil {
Expand Down Expand Up @@ -154,7 +186,7 @@ func BenchmarkFprint(b *testing.B) {
defer putBuffer(buff)
Fprint(buff, string(in))

bc := make([]byte, buff.Len(), buff.Len())
bc := make([]byte, buff.Len())
copy(bc, buff.Bytes())
return bc
}
Expand Down

0 comments on commit 8803a15

Please sign in to comment.