forked from uudashr/gocognit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter.go
47 lines (36 loc) · 807 Bytes
/
filter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package gocognit
type FilterFunc func(stat Stat, index int) bool
func NewTopFilter(top int) FilterFunc {
return func(_ Stat, i int) bool {
return i < top
}
}
func NewComplexityFilter(over int) FilterFunc {
return func(stat Stat, _ int) bool {
return stat.Complexity > over
}
}
type Filter struct {
filterFuncs []FilterFunc
}
func (f *Filter) Apply(original []Stat) []Stat {
filtered := make([]Stat, 0, len(original))
// loop all stats
for i, stat := range original {
keep := true
// loop all filters
for _, filter := range f.filterFuncs {
keep = filter(stat, i)
if !keep {
break
}
}
if keep {
filtered = append(filtered, stat)
}
}
return filtered
}
func (f *Filter) AddFilter(filterFunc FilterFunc) {
f.filterFuncs = append(f.filterFuncs, filterFunc)
}