Skip to content

Add lookup by emoji character 🕵️‍♂️ #15

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

Merged
merged 10 commits into from
May 17, 2020
2 changes: 2 additions & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
- Faiyaz Shaikh ([@yTakkar][yTakkar])
- Raphael Pierzina ([@hackebrot][hackebrot])
- Sumit ([@sum12][sum12])
- Kevin O'Neal ([@Scuilion][Scuilion])

[cassiobotaro]: https://github.com/cassiobotaro
[hackebrot]: https://github.com/hackebrot
[yTakkar]: https://github.com/yTakkar
[sum12]: https://github.com/sum12
[Scuilion]: https://github.com/Scuilion
42 changes: 42 additions & 0 deletions cmd/turtle/char.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package main

import (
"fmt"
"os"

"github.com/hackebrot/turtle"
"github.com/spf13/cobra"
)

var (
cmdChar = &cobra.Command{
Use: "char",
Short: "Print the emoji for the emoji character",
Long: "Print the emoji for the emoji character",
RunE: runChar,
Args: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return fmt.Errorf("require one emoji character")
}
return nil
},
}
)

func runChar(cmd *cobra.Command, args []string) error {
char := args[0]

e, ok := turtle.EmojisByChar[char]

if !ok {
return fmt.Errorf("cannot find emoji with emoji character %q", char)
}

j, err := NewJSONWriter(os.Stdout, WithIndent(prefix, indent))

if err != nil {
return fmt.Errorf("error creating JSONWriter: %v", err)
}

return j.WriteEmoji(e)
}
32 changes: 32 additions & 0 deletions cmd/turtle/char_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package main

import (
"testing"
)

func Test_runChar(t *testing.T) {
tests := []struct {
name string
args []string
wantErr bool
}{
{
name: "error",
args: []string{"nopeasdasdasdsd"},
wantErr: true,
},
{
name: "no error",
args: []string{"🤖"},
wantErr: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := runChar(nil, tt.args); (err != nil) != tt.wantErr {
t.Errorf("runChar() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
1 change: 1 addition & 0 deletions cmd/turtle/turtle.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ var (
)

func init() {
cmdTurtle.AddCommand(cmdChar)
cmdTurtle.AddCommand(cmdCategory)
cmdTurtle.AddCommand(cmdKeyword)
cmdTurtle.AddCommand(cmdSearch)
Expand Down
46 changes: 46 additions & 0 deletions examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,52 @@ import (
"os"
)

// Example for using the Emojis map to find an
// emoji for the specified name.
func ExampleEmojis() {
name := "turtle"
emoji, ok := Emojis[name]

if !ok {
fmt.Fprintf(os.Stderr, "no emoji found for name: %v\n", name)
os.Exit(1)
}

fmt.Printf("Name: %q\n", emoji.Name)
fmt.Printf("Char: %s\n", emoji.Char)
fmt.Printf("Category: %q\n", emoji.Category)
fmt.Printf("Keywords: %q\n", emoji.Keywords)

// Output:
// Name: "turtle"
// Char: 🐢
// Category: "animals_and_nature"
// Keywords: ["animal" "slow" "nature" "tortoise"]
}

// Example for using the EmojisByChar map to find an
// emoji for the specified emoji character.
func ExampleEmojisByChar() {
char := "🐢"
emoji, ok := EmojisByChar[char]

if !ok {
fmt.Fprintf(os.Stderr, "no emoji found for char: %v\n", char)
os.Exit(1)
}

fmt.Printf("Name: %q\n", emoji.Name)
fmt.Printf("Char: %s\n", emoji.Char)
fmt.Printf("Category: %q\n", emoji.Category)
fmt.Printf("Keywords: %q\n", emoji.Keywords)

// Output:
// Name: "turtle"
// Char: 🐢
// Category: "animals_and_nature"
// Keywords: ["animal" "slow" "nature" "tortoise"]
}

// Example for using the Category function to find all
// emojis of the specified category.
func ExampleCategory() {
Expand Down
4 changes: 4 additions & 0 deletions turtle.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@ const Version = "v0.1.0"
// Emojis maps a name to an Emoji
var Emojis = make(map[string]*Emoji)

// EmojisByChar maps a character to an Emoji
var EmojisByChar = make(map[string]*Emoji)

func init() {
for _, e := range emojis {
Emojis[e.Name] = e
EmojisByChar[e.Char] = e
}
}

Expand Down
38 changes: 38 additions & 0 deletions turtle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,41 @@ func Test_search(t *testing.T) {
})
}
}

func Test_Emojis(t *testing.T) {
want := &Emoji{
Name: "fox_face",
Category: "animals_and_nature",
Char: "🦊",
Keywords: []string{"animal", "nature", "face"},
}

got, ok := Emojis[want.Name]

if !ok {
t.Fatalf("Emojis does not contain name %v", repr.Repr(want.Name))
}

if !cmp.Equal(got, want) {
t.Errorf("Emojis[] = %v, want %v", repr.Repr(got), repr.Repr(want))
}
}

func Test_EmojisByChar(t *testing.T) {
want := &Emoji{
Name: "fox_face",
Category: "animals_and_nature",
Char: "🦊",
Keywords: []string{"animal", "nature", "face"},
}

got, ok := EmojisByChar[want.Char]

if !ok {
t.Fatalf("Emojis does not contain char %v", repr.Repr(want.Char))
}

if !cmp.Equal(got, want) {
t.Errorf("Emojis[] = %v, want %v", repr.Repr(got), repr.Repr(want))
}
}