Skip to content
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

Adjust filter helpers API based on current use cases. #2

Merged
merged 1 commit into from
Oct 19, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 10 additions & 31 deletions filter/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,19 @@ import (
"path/filepath"
)

// Extensions returns a filter function that ignores any of the given file
// extensions.
func Extensions(exts ...string) Func {
return func(path string, _ os.FileInfo) bool {
for _, ext := range exts {
if filepath.Ext(path) == ext {
return true
}
}
return false
}
}

// Not negates the given filter, such that:
// FilesWithExtensions returns a filter that ignores files (but not directories) that
// have any of the given extensions. For example:
//
// Not(Extensions(".go", ".html"))
// filter.FilesWithExtensions(".go", ".html")
//
// would ignore any file that is not a .go or .html file.
func Not(filter Func) Func {
// Would ignore both .go and .html files. It would not ignore any directories.
func FilesWithExtensions(exts ...string) Func {
return func(path string, fi os.FileInfo) bool {
return !filter(path, fi)
}
}

// Combine combines multiple filter functions into one. Effectively it is an
// multiple OR operator, that is:
//
// Combine(Extensions(".go"), Extensions(".html"))
//
// would ignore both .go and .html files.
func Combine(filters ...Func) Func {
return func(path string, fi os.FileInfo) bool {
for _, filter := range filters {
if filter(path, fi) {
if fi.IsDir() {
return false
}
for _, ext := range exts {
if filepath.Ext(path) == ext {
return true
}
}
Expand Down