Skip to content

Commit

Permalink
Merge pull request #62 from EmilGeorgiev/implment_function_slice_cont…
Browse files Browse the repository at this point in the history
…ains_string_ignore_cases

implement a function "slice.ContainsStringIgnoreCase"
  • Loading branch information
thrawn01 authored May 5, 2020
2 parents d481c71 + 90e632b commit 52bc0f3
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 0 deletions.
18 changes: 18 additions & 0 deletions slice/string.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package slice

import "strings"

// ContainsString checks if a given slice of strings contains the provided string.
// If a modifier func is provided, it is called with the slice item before the comparation.
// haystack := []string{"one", "Two", "Three"}
Expand All @@ -17,3 +19,19 @@ func ContainsString(s string, slice []string, modifier func(s string) string) bo
}
return false
}

// ContainsStringEqualFold checks if a given slice of strings contains the provided string
// as ignore the cases.
//
// haystack := []string{"aa", "bb", "Cc"}
// if slice.ContainsStringEqualFold(haystack, "cC") {
// // Do thing
// }
func ContainsStringEqualFold(s string, slice []string) bool {
for _, item := range slice {
if strings.EqualFold(item, s) {
return true
}
}
return false
}
102 changes: 102 additions & 0 deletions slice/string_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package slice_test

import (
"strings"
"testing"

"github.com/mailgun/holster/slice"
"github.com/stretchr/testify/assert"
)

func TestContainsString(t *testing.T) {
tests := []struct {
name string
slice []string
str string
modifier func(string) string
want bool
}{
{
name: "Slice contains a specific string",
slice: []string{"aa", "bb", "CC"},
modifier: strings.ToLower,
str: "CC",
want: true,
},
{
name: "Slice contains a string, but it is with upper cases and modifier is nil",
slice: []string{"aa", "bb", "CC"},
str: "cc",
want: false,
},
{
name: "Slice contains a string with upper cases and modifier ToLower is provided",
slice: []string{"AA", "bb", "cc"},
modifier: strings.ToLower,
str: "aa",
want: true,
},
{
name: "Slice does not contains string",
slice: []string{"AA", "bb", "cc"},
str: "notExist",
want: false,
},
{
name: "Empty slice",
slice: []string{},
str: "notExist",
want: false,
},
}

for _, tt := range tests {
got := slice.ContainsString(tt.str, tt.slice, tt.modifier)
assert.Equal(t, tt.want, got)
}
}

func TestContainsStringIgnoreCase(t *testing.T) {
tests := []struct {
name string
slice []string
str string
want bool
}{
{
name: "Slice contains a specific string, but with different upper case",
slice: []string{"aa", "bb", "cC"},
str: "Cc",
want: true,
},
{
name: "Slice contains a string, but it is with upper case",
slice: []string{"aa", "bb", "CC"},
str: "cc",
want: true,
},
{
name: "Slice contains a string, but it is with lower cases",
slice: []string{"aa", "bb", "cc"},
str: "AA",
want: true,
},
{
name: "Slice does not contains string",
slice: []string{"AA", "bb", "cc"},
str: "notExist",
want: false,
},
{
name: "Empty slice",
slice: []string{},
str: "notExist",
want: false,
},
}

for _, tt := range tests {
got := slice.ContainsStringEqualFold(tt.str, tt.slice)
assert.Equal(t, tt.want, got)
}
}

0 comments on commit 52bc0f3

Please sign in to comment.