summaryrefslogtreecommitdiffstats
path: root/pkg/utils
diff options
context:
space:
mode:
Diffstat (limited to 'pkg/utils')
-rw-r--r--pkg/utils/fuzzy_search.go21
-rw-r--r--pkg/utils/fuzzy_search_test.go53
-rw-r--r--pkg/utils/search.go48
-rw-r--r--pkg/utils/search_test.go80
-rw-r--r--pkg/utils/slice.go11
5 files changed, 139 insertions, 74 deletions
diff --git a/pkg/utils/fuzzy_search.go b/pkg/utils/fuzzy_search.go
deleted file mode 100644
index 5fce3dde9..000000000
--- a/pkg/utils/fuzzy_search.go
+++ /dev/null
@@ -1,21 +0,0 @@
-package utils
-
-import (
- "sort"
-
- "github.com/jesseduffield/generics/slices"
- "github.com/sahilm/fuzzy"
-)
-
-func FuzzySearch(needle string, haystack []string) []string {
- if needle == "" {
- return []string{}
- }
-
- matches := fuzzy.Find(needle, haystack)
- sort.Sort(matches)
-
- return slices.Map(matches, func(match fuzzy.Match) string {
- return match.Str
- })
-}
diff --git a/pkg/utils/fuzzy_search_test.go b/pkg/utils/fuzzy_search_test.go
deleted file mode 100644
index 808d83772..000000000
--- a/pkg/utils/fuzzy_search_test.go
+++ /dev/null
@@ -1,53 +0,0 @@
-package utils
-
-import (
- "testing"
-
- "github.com/stretchr/testify/assert"
-)
-
-// TestFuzzySearch is a function.
-func TestFuzzySearch(t *testing.T) {
- type scenario struct {
- needle string
- haystack []string
- expected []string
- }
-
- scenarios := []scenario{
- {
- needle: "",
- haystack: []string{"test"},
- expected: []string{},
- },
- {
- needle: "test",
- haystack: []string{"test"},
- expected: []string{"test"},
- },
- {
- needle: "o",
- haystack: []string{"a", "o", "e"},
- expected: []string{"o"},
- },
- {
- needle: "mybranch",
- haystack: []string{"my_branch", "mybranch", "branch", "this is my branch"},
- expected: []string{"mybranch", "my_branch", "this is my branch"},
- },
- {
- needle: "test",
- haystack: []string{"not a good match", "this 'test' is a good match", "test"},
- expected: []string{"test", "this 'test' is a good match"},
- },
- {
- needle: "test",
- haystack: []string{"Test"},
- expected: []string{"Test"},
- },
- }
-
- for _, s := range scenarios {
- assert.EqualValues(t, s.expected, FuzzySearch(s.needle, s.haystack))
- }
-}
diff --git a/pkg/utils/search.go b/pkg/utils/search.go
new file mode 100644
index 000000000..14b3d7b3e
--- /dev/null
+++ b/pkg/utils/search.go
@@ -0,0 +1,48 @@
+package utils
+
+import (
+ "sort"
+ "strings"
+
+ "github.com/jesseduffield/generics/slices"
+ "github.com/sahilm/fuzzy"
+)
+
+func FuzzySearch(needle string, haystack []string) []string {
+ if needle == "" {
+ return []string{}
+ }
+
+ matches := fuzzy.Find(needle, haystack)
+ sort.Sort(matches)
+
+ return slices.Map(matches, func(match fuzzy.Match) string {
+ return match.Str
+ })
+}
+
+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(haystack),
+ strings.ToLower(needle),
+ )
+}
diff --git a/pkg/utils/search_test.go b/pkg/utils/search_test.go
new file mode 100644
index 000000000..79668c0f5
--- /dev/null
+++ b/pkg/utils/search_test.go
@@ -0,0 +1,80 @@
+package utils
+
+import (
+ "fmt"
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+)
+
+// TestFuzzySearch is a function.
+func TestFuzzySearch(t *testing.T) {
+ type scenario struct {
+ needle string
+ haystack []string
+ expected []string
+ }
+
+ scenarios := []scenario{
+ {
+ needle: "",
+ haystack: []string{"test"},
+ expected: []string{},
+ },
+ {
+ needle: "test",
+ haystack: []string{"test"},
+ expected: []string{"test"},
+ },
+ {
+ needle: "o",
+ haystack: []string{"a", "o", "e"},
+ expected: []string{"o"},
+ },
+ {
+ needle: "mybranch",
+ haystack: []string{"my_branch", "mybranch", "branch", "this is my branch"},
+ expected: []string{"mybranch", "my_branch", "this is my branch"},
+ },
+ {
+ needle: "test",
+ haystack: []string{"not a good match", "this 'test' is a good match", "test"},
+ expected: []string{"test", "this 'test' is a good match"},
+ },
+ {
+ needle: "test",
+ haystack: []string{"Test"},
+ expected: []string{"Test"},
+ },
+ }
+
+ for _, s := range scenarios {
+ assert.EqualValues(t, s.expected, FuzzySearch(s.needle, s.haystack))
+ }
+}
+
+func TestCaseInsensitiveContains(t *testing.T) {
+ testCases := []struct {
+ haystack string
+ needle string
+ expected bool
+ }{
+ {"Hello, World!", "world", true}, // Case-insensitive match
+ {"Hello, World!", "WORLD", true}, // Case-insensitive match
+ {"Hello, World!", "orl", true}, // Case-insensitive match
+ {"Hello, World!", "o, W", true}, // Case-insensitive match
+ {"Hello, World!", "hello", true}, // Case-insensitive match
+ {"Hello, World!", "Foo", false}, // No match
+ {"Hello, World!", "Hello, World!!", false}, // No match
+ {"Hello, World!", "", true}, // Empty needle matches
+ {"", "Hello", false}, // Empty haystack doesn't match
+ {"", "", true}, // Empty strings match
+ {"", " ", false}, // Empty haystack, non-empty needle
+ {" ", "", true}, // Non-empty haystack, empty needle
+ }
+
+ for i, testCase := range testCases {
+ result := CaseInsensitiveContains(testCase.haystack, testCase.needle)
+ assert.Equal(t, testCase.expected, result, fmt.Sprintf("Test case %d failed. Expected '%v', got '%v' for '%s' in '%s'", i, testCase.expected, result, testCase.needle, testCase.haystack))
+ }
+}
diff --git a/pkg/utils/slice.go b/pkg/utils/slice.go
index aff6ae470..4a47f43b1 100644
--- a/pkg/utils/slice.go
+++ b/pkg/utils/slice.go
@@ -113,3 +113,14 @@ func MoveElement[T any](slice []T, from int, to int) []T {
return newSlice
}
+
+func ValuesAtIndices[T any](slice []T, indices []int) []T {
+ result := make([]T, len(indices))
+ for i, index := range indices {
+ // gracefully handling the situation where the index is out of bounds
+ if index < len(slice) {
+ result[i] = slice[index]
+ }
+ }
+ return result
+}