-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter-extended-glob.go
90 lines (75 loc) 路 1.9 KB
/
filter-extended-glob.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package nav
import (
"io/fs"
"path/filepath"
"strings"
"github.com/samber/lo"
)
type ExtendedGlobFilter struct {
Filter
baseGlob string
suffixes []string
anyExtension bool
exclusion string
}
func filterFileByExtendedGlob(name, base, exclusion string, suffixes []string, anyExtension bool) bool {
extension := filepath.Ext(name)
baseName := strings.ToLower(strings.TrimSuffix(name, extension))
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
},
func() bool {
return lo.TernaryF(extension == "",
func() bool {
return len(suffixes) == 0
},
func() bool {
return lo.Contains(
suffixes, strings.ToLower(strings.TrimPrefix(extension, ".")),
)
},
)
},
)
}
// IsMatch does this item match the filter
func (f *ExtendedGlobFilter) IsMatch(item *TraverseItem) bool {
if f.IsApplicable(item) {
result := lo.TernaryF(item.IsDirectory(),
func() bool {
result, _ := filepath.Match(f.baseGlob, strings.ToLower(item.Extension.Name))
return result
},
func() bool {
return filterFileByExtendedGlob(
item.Extension.Name, f.baseGlob, f.exclusion, f.suffixes, f.anyExtension,
)
},
)
return f.invert(result)
}
return f.ifNotApplicable
}
// CompoundExtendedGlobFilter =======================================================
type CompoundExtendedGlobFilter struct {
CompoundFilter
baseGlob string
exclusion string
suffixes []string
anyExtension bool
}
func (f *CompoundExtendedGlobFilter) Matching(children []fs.DirEntry) []fs.DirEntry {
return lo.Filter(children, func(entry fs.DirEntry, _ int) bool {
name := entry.Name()
return f.invert(filterFileByExtendedGlob(
name, f.baseGlob, f.exclusion, f.suffixes, f.anyExtension,
))
})
}