Skip to content

Commit dcb2ab7

Browse files
committed
add name command closes hackebrot#8
1 parent a7f0ff3 commit dcb2ab7

File tree

8 files changed

+168
-1
lines changed

8 files changed

+168
-1
lines changed

README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,40 @@ Char: 🐢
6060
Category: "animals_and_nature"
6161
Keywords: ["animal" "slow" "nature" "tortoise"]
6262
```
63+
### Name
64+
65+
Use ``Name()`` to find the name of an emojis.
66+
67+
```go
68+
package main
69+
70+
import (
71+
"fmt"
72+
"os"
73+
74+
"github.com/hackebrot/turtle"
75+
)
76+
77+
func main() {
78+
n := "👺"
79+
emoji := turtle.Name(n)
80+
81+
if emoji == nil {
82+
fmt.Fprintf(os.Stderr, "no name found for search: %v\n", n)
83+
os.Exit(1)
84+
}
85+
86+
fmt.Printf("Name: %q\n", emoji.Name)
87+
fmt.Printf("Char: %s\n", emoji.Char)
88+
fmt.Printf("Category: %q\n", emoji.Category)
89+
fmt.Printf("Keywords: %q\n", emoji.Keywords)
90+
}
91+
```
92+
93+
```text
94+
japanese_goblin
95+
```
96+
6397

6498
### Search
6599

cmd/turtle/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Available Commands:
1919
category Print all emojis of the category
2020
help Help about any command
2121
keyword Print all emojis with the keyword
22+
name Print the name of an emoji
2223
list Print a list of values from the turtle library
2324
search Print emojis with a name that contains the search string
2425
version Print the version number of turtle
@@ -137,6 +138,16 @@ $ turtle -i " " category travel_and_places
137138
]
138139
```
139140

141+
### Name
142+
143+
```bash
144+
$ turtle name 👚
145+
```
146+
147+
```
148+
womans_clothes
149+
```
150+
140151
### Keyword
141152

142153
```bash

cmd/turtle/name.go

Lines changed: 42 additions & 0 deletions
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+
cmdName = &cobra.Command{
13+
Use: "name",
14+
Short: "Print the name of an emoji",
15+
Long: "Print the name of an emoji",
16+
RunE: runName,
17+
Args: func(cmd *cobra.Command, args []string) error {
18+
if len(args) != 1 {
19+
return fmt.Errorf("require one name")
20+
}
21+
return nil
22+
},
23+
}
24+
)
25+
26+
func runName(cmd *cobra.Command, args []string) error {
27+
k := args[0]
28+
29+
emoji := turtle.Name(k)
30+
31+
if emoji == nil {
32+
return fmt.Errorf("cannot find emoji name for %q", k)
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.Write(emoji.Name)
42+
}

cmd/turtle/name_test.go

Lines changed: 32 additions & 0 deletions
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_runName(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{"dog"},
21+
wantErr: false,
22+
},
23+
}
24+
25+
for _, tt := range tests {
26+
t.Run(tt.name, func(t *testing.T) {
27+
if err := runKeyword(nil, tt.args); (err != nil) != tt.wantErr {
28+
t.Errorf("runKeyword() error = %v, wantErr %v", err, tt.wantErr)
29+
}
30+
})
31+
}
32+
}

cmd/turtle/turtle.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ var (
2929
func init() {
3030
cmdTurtle.AddCommand(cmdCategory)
3131
cmdTurtle.AddCommand(cmdKeyword)
32+
cmdTurtle.AddCommand(cmdName)
3233
cmdTurtle.AddCommand(cmdSearch)
3334
cmdTurtle.AddCommand(cmdVersion)
3435
cmdTurtle.AddCommand(cmdList)

filters.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,17 @@ func category(emojis []*Emoji, c string) []*Emoji {
2020
})
2121
}
2222

23+
// name returns the name based on emoji
24+
func name(emojis []*Emoji, n string) *Emoji {
25+
results := filter(emojis, func(e *Emoji) bool {
26+
return e.Char == n
27+
})
28+
if len(results) > 0 {
29+
return results[0]
30+
}
31+
return nil
32+
}
33+
2334
// keyword filters a slice of Emoji by Keywords
2435
func keyword(emojis []*Emoji, k string) []*Emoji {
2536
return filter(emojis, func(e *Emoji) bool {

turtle.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package turtle
22

33
// Version of the turtle library
4-
const Version = "v0.1.0"
4+
const Version = "v0.1.1"
55

66
// Emojis maps a name to an Emoji
77
var Emojis = make(map[string]*Emoji)
@@ -17,6 +17,11 @@ func Search(s string) []*Emoji {
1717
return search(emojis, s)
1818
}
1919

20+
// Name retuns the emoji for a given name
21+
func Name(n string) *Emoji {
22+
return name(emojis, n)
23+
}
24+
2025
// Keyword filters the emojis by a keyword
2126
func Keyword(k string) []*Emoji {
2227
return keyword(emojis, k)

turtle_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,37 @@ func Test_category(t *testing.T) {
7474
}
7575
}
7676

77+
func Test_name(t *testing.T) {
78+
tests := []struct {
79+
name string
80+
char string
81+
want *Emoji
82+
}{
83+
{
84+
name: "no matches",
85+
char: "",
86+
want: nil,
87+
},
88+
{
89+
name: "single match",
90+
char: "☕",
91+
want: &Emoji{
92+
Name: "coffee",
93+
Category: "food_and_drink",
94+
Char: "☕",
95+
Keywords: []string{"beverage", "caffeine", "latte", "espresso"},
96+
},
97+
},
98+
}
99+
for _, tt := range tests {
100+
t.Run(tt.name, func(t *testing.T) {
101+
if got := name(testEmojis, tt.char); !cmp.Equal(got, tt.want) {
102+
t.Errorf("name() = %v, want %v", repr.Repr(got), repr.Repr(tt.want))
103+
}
104+
})
105+
}
106+
}
107+
77108
func Test_keyword(t *testing.T) {
78109
tests := []struct {
79110
name string

0 commit comments

Comments
 (0)