Skip to content

Added generic filtering function for PathList #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions list.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,12 @@ func (p *PathList) FilterOutHiddenFiles() {
p.FilterOutPrefix(".")
}

func (p *PathList) filter(filter func(*Path) bool) {
// Filter will remove all the elements of the list that do not match
// the specified acceptor function
func (p *PathList) Filter(acceptorFunc func(*Path) bool) {
res := (*p)[:0]
for _, path := range *p {
if filter(path) {
if acceptorFunc(path) {
res = append(res, path)
}
}
Expand All @@ -106,31 +108,31 @@ func (p *PathList) FilterOutPrefix(prefixes ...string) {
filterFunction := func(path *Path) bool {
return !path.HasPrefix(prefixes...)
}
p.filter(filterFunction)
p.Filter(filterFunction)
}

// FilterPrefix remove all entries not having one of the specified prefixes
func (p *PathList) FilterPrefix(prefixes ...string) {
filterFunction := func(path *Path) bool {
return path.HasPrefix(prefixes...)
}
p.filter(filterFunction)
p.Filter(filterFunction)
}

// FilterOutSuffix remove all entries having one of the specified suffixes
func (p *PathList) FilterOutSuffix(suffixies ...string) {
filterFunction := func(path *Path) bool {
return !path.HasSuffix(suffixies...)
}
p.filter(filterFunction)
p.Filter(filterFunction)
}

// FilterSuffix remove all entries not having one of the specified suffixes
func (p *PathList) FilterSuffix(suffixies ...string) {
filterFunction := func(path *Path) bool {
return path.HasSuffix(suffixies...)
}
p.filter(filterFunction)
p.Filter(filterFunction)
}

// Add adds a Path to the PathList
Expand Down
6 changes: 6 additions & 0 deletions list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,10 @@ func TestListFilters(t *testing.T) {
l16 := list.Clone()
l16.FilterPrefix()
require.Equal(t, "[]", fmt.Sprintf("%s", l16))

l17 := list.Clone()
l17.Filter(func(p *Path) bool {
return p.Base() == "bbbb"
})
require.Equal(t, "[bbbb aaaa/bbbb]", fmt.Sprintf("%s", l17))
}