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
|
|
|
|
|
|
|
"github.com/sahilm/fuzzy"
|
2023-07-24 05:06:42 +02:00
|
|
|
"github.com/samber/lo"
|
2020-11-28 04:14:48 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func FuzzySearch(needle string, haystack []string) []string {
|
|
|
|
if needle == "" {
|
|
|
|
return []string{}
|
|
|
|
}
|
|
|
|
|
|
|
|
matches := fuzzy.Find(needle, haystack)
|
|
|
|
sort.Sort(matches)
|
|
|
|
|
2023-07-24 05:06:42 +02:00
|
|
|
return lo.Map(matches, func(match fuzzy.Match, _ int) string {
|
2022-03-19 10:12:58 +02:00
|
|
|
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
|
|
|
)
|
|
|
|
}
|