Skip to content

Commit

Permalink
feat(nav): add exclusion to extended glob filter (#410)
Browse files Browse the repository at this point in the history
  • Loading branch information
plastikfan committed Jan 17, 2024
1 parent b31680d commit 46f8e9c
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 7 deletions.
21 changes: 16 additions & 5 deletions xfs/nav/filter-extended-glob.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,22 @@ type ExtendedGlobFilter struct {
baseGlob string
suffixes []string
anyExtension bool
exclusion string
}

func filterFileByExtendedGlob(name, base string, suffixes []string, anyExtension bool) bool {
func filterFileByExtendedGlob(name, base, exclusion string, suffixes []string, anyExtension bool) bool {
extension := filepath.Ext(name)
baseName := strings.ToLower(strings.TrimSuffix(name, extension))
baseMatch, _ := filepath.Match(base, baseName)

return baseMatch && lo.TernaryF(anyExtension,
if baseMatch, _ := filepath.Match(base, baseName); !baseMatch {
return false
}

if excluded, _ := filepath.Match(exclusion, baseName); excluded {
return false
}

return lo.TernaryF(anyExtension,
func() bool {
return true
},
Expand Down Expand Up @@ -50,7 +58,7 @@ func (f *ExtendedGlobFilter) IsMatch(item *TraverseItem) bool {
},
func() bool {
return filterFileByExtendedGlob(
item.Extension.Name, f.baseGlob, f.suffixes, f.anyExtension,
item.Extension.Name, f.baseGlob, f.exclusion, f.suffixes, f.anyExtension,
)
},
)
Expand All @@ -66,6 +74,7 @@ func (f *ExtendedGlobFilter) IsMatch(item *TraverseItem) bool {
type CompoundExtendedGlobFilter struct {
CompoundFilter
baseGlob string
exclusion string
suffixes []string
anyExtension bool
}
Expand All @@ -74,6 +83,8 @@ func (f *CompoundExtendedGlobFilter) Matching(children []fs.DirEntry) []fs.DirEn
return lo.Filter(children, func(entry fs.DirEntry, _ int) bool {
name := entry.Name()

return f.invert(filterFileByExtendedGlob(name, f.baseGlob, f.suffixes, f.anyExtension))
return f.invert(filterFileByExtendedGlob(
name, f.baseGlob, f.exclusion, f.suffixes, f.anyExtension,
))
})
}
40 changes: 40 additions & 0 deletions xfs/nav/filter-extended-glob_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,24 @@ var _ = Describe("Filter Extended glob", Ordered, func() {
scope: nav.ScopeLeafEn,
ifNotApplicable: nav.TriStateBoolFalseEn,
}),

// === with-exclusion ================================================

Entry(nil, &filterTE{
naviTE: naviTE{
message: "universal(any scope): extended glob filter with exclusion",
relative: "rock/PROGRESSIVE-ROCK/Marillion",
subscription: nav.SubscribeFiles,
expectedNoOf: directoryQuantities{
files: 12,
folders: 0,
},
prohibited: []string{"01 - Hotel Hobbies.flac"},
},
name: "files starting with 0, except 01 items and flac suffix",
pattern: "0*/*01*|flac",
scope: nav.ScopeFileEn,
}),
)

DescribeTable("Filter Children (extended glob)",
Expand Down Expand Up @@ -585,5 +603,27 @@ var _ = Describe("Filter Extended glob", Ordered, func() {
pattern: "*|txt",
negate: true,
}),

// === with-exclusion ================================================

Entry(nil, &filterTE{
naviTE: naviTE{
message: "folder(with files): extended glob filter with exclusion",
relative: "rock/PROGRESSIVE-ROCK/Marillion",
subscription: nav.SubscribeFoldersWithFiles,
expectedNoOf: directoryQuantities{
files: 0,
folders: 5,
children: map[string]int{
"Clutching At Straws": 3,
"Fugazi": 3,
"Misplaced Childhood": 3,
"Script for a Jesters Tear": 3,
},
},
},
name: "files starting with 0, except 01 items and flac suffix",
pattern: "0*/*01*|flac",
}),
)
})
26 changes: 24 additions & 2 deletions xfs/nav/new-filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ func newNodeFilter(def *FilterDef) TraverseFilter {
panic(err)
}

base, exclusion := splitGlob(segments[0])

filter = &ExtendedGlobFilter{
Filter: Filter{
name: def.Description,
Expand All @@ -59,11 +61,12 @@ func newNodeFilter(def *FilterDef) TraverseFilter {
negate: def.Negate,
ifNotApplicable: ifNotApplicable,
},
baseGlob: strings.ToLower(segments[0]),
baseGlob: base,
suffixes: lo.Map(suffixes, func(s string, _ int) string {
return strings.ToLower(strings.TrimPrefix(strings.TrimSpace(s), "."))
}),
anyExtension: slices.Contains(suffixes, "*"),
exclusion: exclusion,
}

case FilterTypeRegexEn:
Expand Down Expand Up @@ -126,6 +129,22 @@ func newPolyFilter(polyDef *PolyFilterDef) TraverseFilter {
return filter
}

const (
exclusionDelim = "/"
)

func splitGlob(baseGlob string) (base, exclusion string) {
base = strings.ToLower(baseGlob)

if strings.Contains(base, exclusionDelim) {
constituents := strings.Split(base, exclusionDelim)
base = constituents[0]
exclusion = constituents[1]
}

return base, exclusion
}

func newCompoundFilter(def *CompoundFilterDef) CompoundTraverseFilter {
var (
filter CompoundTraverseFilter
Expand All @@ -142,17 +161,20 @@ func newCompoundFilter(def *CompoundFilterDef) CompoundTraverseFilter {
panic(errors.New("invalid incase filter definition; pattern is missing separator"))
}

base, exclusion := splitGlob(segments[0])

filter = &CompoundExtendedGlobFilter{
CompoundFilter: CompoundFilter{
Name: def.Description,
Pattern: def.Pattern,
Negate: def.Negate,
},
baseGlob: strings.ToLower(segments[0]),
baseGlob: base,
suffixes: lo.Map(suffixes, func(s string, _ int) string {
return strings.ToLower(strings.TrimPrefix(strings.TrimSpace(s), "."))
}),
anyExtension: slices.Contains(suffixes, "*"),
exclusion: exclusion,
}

case FilterTypeRegexEn:
Expand Down

0 comments on commit 46f8e9c

Please sign in to comment.