Skip to content

Commit b32bab3

Browse files
authored
Merge pull request #15 from hackebrot/add-lookup-by-char
Add lookup by emoji character πŸ•΅οΈβ€β™‚οΈ
2 parents 5f2aaa7 + fe92492 commit b32bab3

File tree

7 files changed

+165
-0
lines changed

7 files changed

+165
-0
lines changed

β€ŽCONTRIBUTORS.md

+2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
- Faiyaz Shaikh ([@yTakkar][yTakkar])
55
- Raphael Pierzina ([@hackebrot][hackebrot])
66
- Sumit ([@sum12][sum12])
7+
- Kevin O'Neal ([@Scuilion][Scuilion])
78

89
[cassiobotaro]: https://github.com/cassiobotaro
910
[hackebrot]: https://github.com/hackebrot
1011
[yTakkar]: https://github.com/yTakkar
1112
[sum12]: https://github.com/sum12
13+
[Scuilion]: https://github.com/Scuilion

β€Žcmd/turtle/char.go

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
"github.com/hackebrot/turtle"
8+
"github.com/spf13/cobra"
9+
)
10+
11+
var (
12+
cmdChar = &cobra.Command{
13+
Use: "char",
14+
Short: "Print the emoji for the emoji character",
15+
Long: "Print the emoji for the emoji character",
16+
RunE: runChar,
17+
Args: func(cmd *cobra.Command, args []string) error {
18+
if len(args) != 1 {
19+
return fmt.Errorf("require one emoji character")
20+
}
21+
return nil
22+
},
23+
}
24+
)
25+
26+
func runChar(cmd *cobra.Command, args []string) error {
27+
char := args[0]
28+
29+
e, ok := turtle.EmojisByChar[char]
30+
31+
if !ok {
32+
return fmt.Errorf("cannot find emoji with emoji character %q", char)
33+
}
34+
35+
j, err := NewJSONWriter(os.Stdout, WithIndent(prefix, indent))
36+
37+
if err != nil {
38+
return fmt.Errorf("error creating JSONWriter: %v", err)
39+
}
40+
41+
return j.WriteEmoji(e)
42+
}

β€Žcmd/turtle/char_test.go

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package main
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func Test_runChar(t *testing.T) {
8+
tests := []struct {
9+
name string
10+
args []string
11+
wantErr bool
12+
}{
13+
{
14+
name: "error",
15+
args: []string{"nopeasdasdasdsd"},
16+
wantErr: true,
17+
},
18+
{
19+
name: "no error",
20+
args: []string{"πŸ€–"},
21+
wantErr: false,
22+
},
23+
}
24+
25+
for _, tt := range tests {
26+
t.Run(tt.name, func(t *testing.T) {
27+
if err := runChar(nil, tt.args); (err != nil) != tt.wantErr {
28+
t.Errorf("runChar() error = %v, wantErr %v", err, tt.wantErr)
29+
}
30+
})
31+
}
32+
}

β€Žcmd/turtle/turtle.go

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ var (
2727
)
2828

2929
func init() {
30+
cmdTurtle.AddCommand(cmdChar)
3031
cmdTurtle.AddCommand(cmdCategory)
3132
cmdTurtle.AddCommand(cmdKeyword)
3233
cmdTurtle.AddCommand(cmdSearch)

β€Žexamples_test.go

+46
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,52 @@ import (
55
"os"
66
)
77

8+
// Example for using the Emojis map to find an
9+
// emoji for the specified name.
10+
func ExampleEmojis() {
11+
name := "turtle"
12+
emoji, ok := Emojis[name]
13+
14+
if !ok {
15+
fmt.Fprintf(os.Stderr, "no emoji found for name: %v\n", name)
16+
os.Exit(1)
17+
}
18+
19+
fmt.Printf("Name: %q\n", emoji.Name)
20+
fmt.Printf("Char: %s\n", emoji.Char)
21+
fmt.Printf("Category: %q\n", emoji.Category)
22+
fmt.Printf("Keywords: %q\n", emoji.Keywords)
23+
24+
// Output:
25+
// Name: "turtle"
26+
// Char: 🐒
27+
// Category: "animals_and_nature"
28+
// Keywords: ["animal" "slow" "nature" "tortoise"]
29+
}
30+
31+
// Example for using the EmojisByChar map to find an
32+
// emoji for the specified emoji character.
33+
func ExampleEmojisByChar() {
34+
char := "🐒"
35+
emoji, ok := EmojisByChar[char]
36+
37+
if !ok {
38+
fmt.Fprintf(os.Stderr, "no emoji found for char: %v\n", char)
39+
os.Exit(1)
40+
}
41+
42+
fmt.Printf("Name: %q\n", emoji.Name)
43+
fmt.Printf("Char: %s\n", emoji.Char)
44+
fmt.Printf("Category: %q\n", emoji.Category)
45+
fmt.Printf("Keywords: %q\n", emoji.Keywords)
46+
47+
// Output:
48+
// Name: "turtle"
49+
// Char: 🐒
50+
// Category: "animals_and_nature"
51+
// Keywords: ["animal" "slow" "nature" "tortoise"]
52+
}
53+
854
// Example for using the Category function to find all
955
// emojis of the specified category.
1056
func ExampleCategory() {

β€Žturtle.go

+4
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@ const Version = "v0.1.0"
66
// Emojis maps a name to an Emoji
77
var Emojis = make(map[string]*Emoji)
88

9+
// EmojisByChar maps a character to an Emoji
10+
var EmojisByChar = make(map[string]*Emoji)
11+
912
func init() {
1013
for _, e := range emojis {
1114
Emojis[e.Name] = e
15+
EmojisByChar[e.Char] = e
1216
}
1317
}
1418

β€Žturtle_test.go

+38
Original file line numberDiff line numberDiff line change
@@ -151,3 +151,41 @@ func Test_search(t *testing.T) {
151151
})
152152
}
153153
}
154+
155+
func Test_Emojis(t *testing.T) {
156+
want := &Emoji{
157+
Name: "fox_face",
158+
Category: "animals_and_nature",
159+
Char: "🦊",
160+
Keywords: []string{"animal", "nature", "face"},
161+
}
162+
163+
got, ok := Emojis[want.Name]
164+
165+
if !ok {
166+
t.Fatalf("Emojis does not contain name %v", repr.Repr(want.Name))
167+
}
168+
169+
if !cmp.Equal(got, want) {
170+
t.Errorf("Emojis[] = %v, want %v", repr.Repr(got), repr.Repr(want))
171+
}
172+
}
173+
174+
func Test_EmojisByChar(t *testing.T) {
175+
want := &Emoji{
176+
Name: "fox_face",
177+
Category: "animals_and_nature",
178+
Char: "🦊",
179+
Keywords: []string{"animal", "nature", "face"},
180+
}
181+
182+
got, ok := EmojisByChar[want.Char]
183+
184+
if !ok {
185+
t.Fatalf("Emojis does not contain char %v", repr.Repr(want.Char))
186+
}
187+
188+
if !cmp.Equal(got, want) {
189+
t.Errorf("Emojis[] = %v, want %v", repr.Repr(got), repr.Repr(want))
190+
}
191+
}

0 commit comments

Comments
Β (0)