diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2023-05-27 20:38:37 +1000 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2023-07-03 12:54:13 +1000 |
commit | bf5871cc4fe8bd7f431e5603447ea0bc29b3d642 (patch) | |
tree | 41f0daea55a9517e88b26c540abd55591fb0196b /pkg/utils | |
parent | 13326344f092e23d4a54887505c92060377adeb6 (diff) |
Case insensitive string comparison
Diffstat (limited to 'pkg/utils')
-rw-r--r-- | pkg/utils/fuzzy_search_test.go | 53 | ||||
-rw-r--r-- | pkg/utils/search.go (renamed from pkg/utils/fuzzy_search.go) | 8 | ||||
-rw-r--r-- | pkg/utils/search_test.go | 80 |
3 files changed, 88 insertions, 53 deletions
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/fuzzy_search.go b/pkg/utils/search.go index 5fce3dde9..e90d2b5b0 100644 --- a/pkg/utils/fuzzy_search.go +++ b/pkg/utils/search.go @@ -2,6 +2,7 @@ package utils import ( "sort" + "strings" "github.com/jesseduffield/generics/slices" "github.com/sahilm/fuzzy" @@ -19,3 +20,10 @@ func FuzzySearch(needle string, haystack []string) []string { return match.Str }) } + +func CaseInsensitiveContains(a, b string) bool { + return strings.Contains( + strings.ToLower(a), + strings.ToLower(b), + ) +} 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)) + } +} |