2020-11-28 04:14:48 +02:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sort"
|
2023-05-27 12:38:37 +02:00
|
|
|
"strings"
|
2020-11-28 04:14:48 +02:00
|
|
|
|
2022-03-19 10:12:58 +02:00
|
|
|
"github.com/jesseduffield/generics/slices"
|
2020-11-28 04:14:48 +02:00
|
|
|
"github.com/sahilm/fuzzy"
|
|
|
|
)
|
|
|
|
|
|
|
|
func FuzzySearch(needle string, haystack []string) []string {
|
|
|
|
if needle == "" {
|
|
|
|
return []string{}
|
|
|
|
}
|
|
|
|
|
|
|
|
matches := fuzzy.Find(needle, haystack)
|
|
|
|
sort.Sort(matches)
|
|
|
|
|
2022-03-19 10:12:58 +02:00
|
|
|
return slices.Map(matches, func(match fuzzy.Match) string {
|
|
|
|
return match.Str
|
|
|
|
})
|
2020-11-28 04:14:48 +02:00
|
|
|
}
|
2023-05-27 12:38:37 +02:00
|
|
|
|
2023-06-03 06:56:15 +02:00
|
|
|
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 {
|
2023-05-27 12:38:37 +02:00
|
|
|
return strings.Contains(
|
2023-06-03 06:56:15 +02:00
|
|
|
strings.ToLower(haystack),
|
|
|
|
strings.ToLower(needle),
|
2023-05-27 12:38:37 +02:00
|
|
|
)
|
|
|
|
}
|