Skip to content

Commit f18fdba

Browse files
committed
Move filters to new file and implement public API
1 parent d14503c commit f18fdba

File tree

2 files changed

+49
-33
lines changed

2 files changed

+49
-33
lines changed

filters.go

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package turtle
2+
3+
import "strings"
4+
5+
// filter a given slice of Emoji by f
6+
func filter(emojis []*Emoji, f func(e *Emoji) bool) []*Emoji {
7+
var r []*Emoji
8+
for _, e := range emojis {
9+
if f(e) {
10+
r = append(r, e)
11+
}
12+
}
13+
return r
14+
}
15+
16+
// category filters a slice of Emoji by Category
17+
func category(emojis []*Emoji, c string) []*Emoji {
18+
return filter(emojis, func(e *Emoji) bool {
19+
return e.Category == c
20+
})
21+
}
22+
23+
// keyword filters a slice of Emoji by Keywords
24+
func keyword(emojis []*Emoji, k string) []*Emoji {
25+
return filter(emojis, func(e *Emoji) bool {
26+
for _, keyword := range e.Keywords {
27+
if keyword == k {
28+
return true
29+
}
30+
}
31+
return false
32+
})
33+
}
34+
35+
// search Emoji in a slice by Name
36+
func search(emojis []*Emoji, s string) []*Emoji {
37+
return filter(emojis, func(e *Emoji) bool {
38+
return strings.Contains(e.Name, s)
39+
})
40+
}

turtle.go

+9-33
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package turtle
22

3-
import "strings"
4-
53
// Emojis maps a name to an Emoji
64
var Emojis = make(map[string]*Emoji)
75

@@ -11,39 +9,17 @@ func init() {
119
}
1210
}
1311

14-
// filter a given slice of Emoji by f
15-
func filter(emojis []*Emoji, f func(e *Emoji) bool) []*Emoji {
16-
var r []*Emoji
17-
for _, e := range emojis {
18-
if f(e) {
19-
r = append(r, e)
20-
}
21-
}
22-
return r
23-
}
24-
25-
// category filters a slice of Emoji by Category
26-
func category(emojis []*Emoji, c string) []*Emoji {
27-
return filter(emojis, func(e *Emoji) bool {
28-
return e.Category == c
29-
})
12+
// Search emojis by a name
13+
func Search(s string) []*Emoji {
14+
return search(emojis, s)
3015
}
3116

32-
// keyword filters a slice of Emoji by Keywords
33-
func keyword(emojis []*Emoji, k string) []*Emoji {
34-
return filter(emojis, func(e *Emoji) bool {
35-
for _, keyword := range e.Keywords {
36-
if keyword == k {
37-
return true
38-
}
39-
}
40-
return false
41-
})
17+
// Keyword filters the emojis by a keyword
18+
func Keyword(k string) []*Emoji {
19+
return keyword(emojis, k)
4220
}
4321

44-
// search Emoji in a slice by Name
45-
func search(emojis []*Emoji, s string) []*Emoji {
46-
return filter(emojis, func(e *Emoji) bool {
47-
return strings.Contains(e.Name, s)
48-
})
22+
// Category filters the emojis by a category
23+
func Category(c string) []*Emoji {
24+
return category(emojis, c)
4925
}

0 commit comments

Comments
 (0)