Skip to content

Commit

Permalink
Move filters to new file and implement public API
Browse files Browse the repository at this point in the history
  • Loading branch information
hackebrot committed Sep 9, 2017
1 parent d14503c commit f18fdba
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 33 deletions.
40 changes: 40 additions & 0 deletions filters.go
@@ -0,0 +1,40 @@
package turtle

import "strings"

// filter a given slice of Emoji by f
func filter(emojis []*Emoji, f func(e *Emoji) bool) []*Emoji {
var r []*Emoji
for _, e := range emojis {
if f(e) {
r = append(r, e)
}
}
return r
}

// category filters a slice of Emoji by Category
func category(emojis []*Emoji, c string) []*Emoji {
return filter(emojis, func(e *Emoji) bool {
return e.Category == c
})
}

// keyword filters a slice of Emoji by Keywords
func keyword(emojis []*Emoji, k string) []*Emoji {
return filter(emojis, func(e *Emoji) bool {
for _, keyword := range e.Keywords {
if keyword == k {
return true
}
}
return false
})
}

// search Emoji in a slice by Name
func search(emojis []*Emoji, s string) []*Emoji {
return filter(emojis, func(e *Emoji) bool {
return strings.Contains(e.Name, s)
})
}
42 changes: 9 additions & 33 deletions turtle.go
@@ -1,7 +1,5 @@
package turtle

import "strings"

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

Expand All @@ -11,39 +9,17 @@ func init() {
}
}

// filter a given slice of Emoji by f
func filter(emojis []*Emoji, f func(e *Emoji) bool) []*Emoji {
var r []*Emoji
for _, e := range emojis {
if f(e) {
r = append(r, e)
}
}
return r
}

// category filters a slice of Emoji by Category
func category(emojis []*Emoji, c string) []*Emoji {
return filter(emojis, func(e *Emoji) bool {
return e.Category == c
})
// Search emojis by a name
func Search(s string) []*Emoji {
return search(emojis, s)
}

// keyword filters a slice of Emoji by Keywords
func keyword(emojis []*Emoji, k string) []*Emoji {
return filter(emojis, func(e *Emoji) bool {
for _, keyword := range e.Keywords {
if keyword == k {
return true
}
}
return false
})
// Keyword filters the emojis by a keyword
func Keyword(k string) []*Emoji {
return keyword(emojis, k)
}

// search Emoji in a slice by Name
func search(emojis []*Emoji, s string) []*Emoji {
return filter(emojis, func(e *Emoji) bool {
return strings.Contains(e.Name, s)
})
// Category filters the emojis by a category
func Category(c string) []*Emoji {
return category(emojis, c)
}

0 comments on commit f18fdba

Please sign in to comment.