/
filter.go
79 lines (71 loc) · 1.7 KB
/
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package collision
import "github.com/oakmound/oak/v3/event"
// A Filter will take a set of collision spaces
// and return the subset that match some requirement
type Filter func([]*Space) []*Space
// FirstLabel returns the first space that has a label in the input, or nothing
func FirstLabel(ls ...Label) Filter {
return func(sps []*Space) []*Space {
for _, s := range sps {
for _, l := range ls {
if s.Label == l {
return []*Space{s}
}
}
}
return []*Space{}
}
}
// With will filter spaces so that only those returning true
// from the input keepFn will be in the output
func With(keepFn func(*Space) bool) Filter {
return func(sps []*Space) []*Space {
out := make([]*Space, 0, len(sps))
for _, s := range sps {
if keepFn(s) {
out = append(out, s)
}
}
return out
}
}
// Without will filter spaces so that no spaces returning true
// from the input tossFn will be in the output
func Without(tossFn func(*Space) bool) Filter {
return With(func(s *Space) bool {
return !tossFn(s)
})
}
// WithoutCIDs will return no spaces with a CID in the input
func WithoutCIDs(cids ...event.CID) Filter {
return Without(func(s *Space) bool {
for _, c := range cids {
if s.CID == c {
return true
}
}
return false
})
}
// WithLabels will only return spaces with a label in the input
func WithLabels(ls ...Label) Filter {
return With(func(s *Space) bool {
for _, l := range ls {
if s.Label == l {
return true
}
}
return false
})
}
// WithoutLabels will return no spaces with a label in the input
func WithoutLabels(ls ...Label) Filter {
return With(func(s *Space) bool {
for _, l := range ls {
if s.Label == l {
return false
}
}
return true
})
}