Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ReverseCodeMap, map unicode character to lists of short codes #35

Merged
merged 2 commits into from
Apr 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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...)))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason for this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed from the output of https://github.com/golangci/golangci-lint

$ golangci-lint run .
emoji_test.go:189:22: S1019: should use make([]byte, buff.Len()) instead (gosimple)
                bc := make([]byte, buff.Len(), buff.Len())
                                   ^
emoji.go:140:20: SA1006: printf-style function with dynamic format string and no further arguments should use print-style function instead (staticcheck)
        return fmt.Printf(compile(fmt.Sprintf(format, a...)))
                          ^

Happy to move to a separate PR or remove if you want!

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see . Thank you, I understand 👍

}

// 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