Skip to content

Commit b3ef88d

Browse files
authored
Merge pull request #27 from arduino/generic-filter
Added generic filtering function for PathList
2 parents 0fabf94 + 25a33ce commit b3ef88d

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

Diff for: list.go

+8-6
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,12 @@ func (p *PathList) FilterOutHiddenFiles() {
9191
p.FilterOutPrefix(".")
9292
}
9393

94-
func (p *PathList) filter(filter func(*Path) bool) {
94+
// Filter will remove all the elements of the list that do not match
95+
// the specified acceptor function
96+
func (p *PathList) Filter(acceptorFunc func(*Path) bool) {
9597
res := (*p)[:0]
9698
for _, path := range *p {
97-
if filter(path) {
99+
if acceptorFunc(path) {
98100
res = append(res, path)
99101
}
100102
}
@@ -106,31 +108,31 @@ func (p *PathList) FilterOutPrefix(prefixes ...string) {
106108
filterFunction := func(path *Path) bool {
107109
return !path.HasPrefix(prefixes...)
108110
}
109-
p.filter(filterFunction)
111+
p.Filter(filterFunction)
110112
}
111113

112114
// FilterPrefix remove all entries not having one of the specified prefixes
113115
func (p *PathList) FilterPrefix(prefixes ...string) {
114116
filterFunction := func(path *Path) bool {
115117
return path.HasPrefix(prefixes...)
116118
}
117-
p.filter(filterFunction)
119+
p.Filter(filterFunction)
118120
}
119121

120122
// FilterOutSuffix remove all entries having one of the specified suffixes
121123
func (p *PathList) FilterOutSuffix(suffixies ...string) {
122124
filterFunction := func(path *Path) bool {
123125
return !path.HasSuffix(suffixies...)
124126
}
125-
p.filter(filterFunction)
127+
p.Filter(filterFunction)
126128
}
127129

128130
// FilterSuffix remove all entries not having one of the specified suffixes
129131
func (p *PathList) FilterSuffix(suffixies ...string) {
130132
filterFunction := func(path *Path) bool {
131133
return path.HasSuffix(suffixies...)
132134
}
133-
p.filter(filterFunction)
135+
p.Filter(filterFunction)
134136
}
135137

136138
// Add adds a Path to the PathList

Diff for: list_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -160,4 +160,10 @@ func TestListFilters(t *testing.T) {
160160
l16 := list.Clone()
161161
l16.FilterPrefix()
162162
require.Equal(t, "[]", fmt.Sprintf("%s", l16))
163+
164+
l17 := list.Clone()
165+
l17.Filter(func(p *Path) bool {
166+
return p.Base() == "bbbb"
167+
})
168+
require.Equal(t, "[bbbb aaaa/bbbb]", fmt.Sprintf("%s", l17))
163169
}

0 commit comments

Comments
 (0)