Support case sensitive filtering

This commit is contained in:
Jesse Duffield 2023-06-03 14:56:15 +10:00
parent 9df634f13f
commit 7d7399a89f
2 changed files with 23 additions and 4 deletions

View file

@ -66,7 +66,7 @@ func (self *FilteredList[T]) applyFilter() {
}
func (self *FilteredList[T]) match(haystack string, needle string) bool {
return utils.CaseInsensitiveContains(haystack, needle)
return utils.CaseAwareContains(haystack, needle)
}
func (self *FilteredList[T]) UnfilteredIndex(index int) int {

View file

@ -21,9 +21,28 @@ func FuzzySearch(needle string, haystack []string) []string {
})
}
func CaseInsensitiveContains(a, b string) bool {
func CaseAwareContains(haystack, needle string) bool {
// if needle contains an uppercase letter, we'll do a case sensitive search
if ContainsUppercase(needle) {
return strings.Contains(haystack, needle)
}
return CaseInsensitiveContains(haystack, needle)
}
func ContainsUppercase(s string) bool {
for _, r := range s {
if r >= 'A' && r <= 'Z' {
return true
}
}
return false
}
func CaseInsensitiveContains(haystack, needle string) bool {
return strings.Contains(
strings.ToLower(a),
strings.ToLower(b),
strings.ToLower(haystack),
strings.ToLower(needle),
)
}