Skip to content

Commit

Permalink
Adjust filter helpers API based on current use cases.
Browse files Browse the repository at this point in the history
It seems that providing a specific higher-level API based on real-world
needs is better than providing multiple lower-level general APIs. This
addresses most common uses and therefore provides most value.

Other filter helpers can be added for future use cases, and for really
custom ad-hoc filters, a custom func literal can always be created.
  • Loading branch information
dmitshur committed Oct 19, 2015
1 parent 5d34ca8 commit 6294156
Showing 1 changed file with 10 additions and 31 deletions.
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

0 comments on commit 6294156

Please sign in to comment.