]> git.lizzy.rs Git - go-anidb.git/blob - titles/resultsearch.go
Modernize
[go-anidb.git] / titles / resultsearch.go
1 package titles
2
3 // Returns true if the given *Anime should be included in the final ResultSet
4 type ResultFilter func(*Anime) bool
5
6 // Returns true if the Anime with the given title should be included in the final ResultSet
7 type TitleComparer func(string) bool
8
9 // Filters a ResultSet according to the given TitleComparer; returns the filtered ResultSet
10 func (rs ResultSet) FilterByTitles(cmp TitleComparer) ResultSet {
11         return rs.Filter(
12                 func(a *Anime) bool {
13                         if cmp(a.PrimaryTitle) {
14                                 return true
15                         }
16
17                         for _, m := range []map[string][]Name{
18                                 a.OfficialNames, a.ShortNames, a.Synonyms,
19                         } {
20                                 for _, names := range m {
21                                         for _, name := range names {
22                                                 if cmp(name.Title) {
23                                                         return true
24                                                 }
25                                         }
26                                 }
27                         }
28                         return false
29                 })
30 }
31
32 // Filters a ResultSet according to the given ResultFilter; returns the filtered ResultSet
33 func (rs ResultSet) Filter(filter ResultFilter) ResultSet {
34         ret := ResultSet{}
35         for _, a := range rs {
36                 if filter(&a) {
37                         ret[a.AID] = a
38                 }
39         }
40
41         return ret
42 }