Skip to content

Commit

Permalink
Merge pull request #67 from Teamwork/in-folded-stringslice
Browse files Browse the repository at this point in the history
Add InFoldedStringSlice & StringMap
  • Loading branch information
cesartw authored Nov 12, 2021
2 parents 61c8125 + 7922df9 commit 194b7ef
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 1 deletion.
22 changes: 21 additions & 1 deletion sliceutil/sliceutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func CSVtoInt64Slice(csv string) ([]int64, error) {
return ints, nil
}

// InStringSlice reports whether str is within list
// InStringSlice reports whether str is within list(case-sensitive)
func InStringSlice(list []string, str string) bool {
for _, item := range list {
if item == str {
Expand All @@ -101,6 +101,16 @@ func InStringSlice(list []string, str string) bool {
return false
}

// InFoldedStringSlice reports whether str is within list(case-insensitive)
func InFoldedStringSlice(list []string, str string) bool {
for _, item := range list {
if strings.EqualFold(item, str) {
return true
}
}
return false
}

// InIntSlice reports whether i is within list
func InIntSlice(list []int, i int) bool {
for _, item := range list {
Expand Down Expand Up @@ -194,3 +204,13 @@ func FilterInt(list []int64, fun func(int64) bool) []int64 {
func FilterIntEmpty(e int64) bool {
return e != 0
}

// StringMap returns a list strings where each item in list has been modified by f
func StringMap(list []string, f func(string) string) []string {
ret := make([]string, len(list))
for i := range list {
ret[i] = f(list[i])
}

return ret
}
53 changes: 53 additions & 0 deletions sliceutil/sliceutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,31 @@ func TestInStringSlice(t *testing.T) {
}
}

func TestInFoldedStringSlice(t *testing.T) {
tests := []struct {
list []string
find string
expected bool
}{
{[]string{"hello"}, "hello", true},
{[]string{"HELLO"}, "hello", true},
{[]string{"hello"}, "HELLO", true},
{[]string{"hello"}, "hell", false},
{[]string{"hello", "world", "test"}, "world", true},
{[]string{"hello", "world", "test"}, "", false},
{[]string{}, "", false},
}

for i, tc := range tests {
t.Run(fmt.Sprintf("test-%v", i), func(t *testing.T) {
got := InFoldedStringSlice(tc.list, tc.find)
if got != tc.expected {
t.Errorf(diff.Cmp(tc.expected, got))
}
})
}
}

func TestInIntSlice(t *testing.T) {
tests := []struct {
list []int
Expand Down Expand Up @@ -413,6 +438,7 @@ func TestFilterInt(t *testing.T) {
})
}
}

func TestChooseString(t *testing.T) {
tests := []struct {
in []string
Expand Down Expand Up @@ -475,3 +501,30 @@ func TestRemoveString(t *testing.T) {
})
}
}

func TestStringMap(t *testing.T) {
cases := []struct {
in []string
want []string
f func(string) string
}{
{
in: []string{"a", "b", "c"},
want: []string{"", "", ""},
f: func(string) string { return "" },
},
{
in: []string{"a", "b", "c"},
want: []string{"aa", "bb", "cc"},
f: func(c string) string { return c + c },
},
}
for _, tc := range cases {
t.Run("", func(t *testing.T) {
out := StringMap(tc.in, tc.f)
if !reflect.DeepEqual(tc.want, out) {
t.Errorf("\nout: %#v\nwant: %#v\n", out, tc.want)
}
})
}
}

0 comments on commit 194b7ef

Please sign in to comment.