Skip to content

Commit

Permalink
basiccheck: add StringHasOneOfSuffixes function
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremmfr committed Aug 16, 2023
1 parent 60a09a1 commit 0d04f27
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# changelog

* add `StringHasOneOfSuffixes` function in `basiccheck` package

## v0.9.0

* add `DelRuneInStringWith` and `FilterRuneInStringWith` functions in `basicalter` package (remove specific rune(s) on the value of a string pointer with functions)
Expand Down
7 changes: 7 additions & 0 deletions basiccheck/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,10 @@ func StringHasOneOfPrefixes(s string, list []string) bool {
return strings.HasPrefix(s, v)
})
}

// StringHasOneOfSuffixes check if string has one of suffix list.
func StringHasOneOfSuffixes(s string, list []string) bool {
return OneInSliceWith(list, func(v string) bool {
return strings.HasSuffix(s, v)
})
}
11 changes: 11 additions & 0 deletions basiccheck/string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,14 @@ func TestStringHasOneOfPrefixes(t *testing.T) {
t.Errorf("StringHasOneOfPrefixes found one of prefixes %v in fozbar", prefStrings)
}
}

func TestStringHasOneOfSuffixes(t *testing.T) {
prefStrings := []string{"foo", "bar"}

if !basiccheck.StringHasOneOfSuffixes("barfoo", prefStrings) {
t.Errorf("StringHasOneOfSuffixes didn't find prefix foo in foobaz")
}
if basiccheck.StringHasOneOfSuffixes("barfoz", prefStrings) {
t.Errorf("StringHasOneOfPrefixes found one of prefixes %v in fozbar", prefStrings)
}
}

0 comments on commit 0d04f27

Please sign in to comment.