From 9a5336a8f97483624c2dc59ea318927de90f297e Mon Sep 17 00:00:00 2001 From: Samuel Berthe Date: Wed, 24 Sep 2025 23:18:51 +0200 Subject: [PATCH] feat: adding hassuffix + hasprefix (#680) --- README.md | 30 ++++++++++++++++++++++++++++++ find.go | 32 ++++++++++++++++++++++++++++++++ find_test.go | 16 ++++++++++++++++ 3 files changed, 78 insertions(+) diff --git a/README.md b/README.md index d0321ac..4f80a8f 100644 --- a/README.md +++ b/README.md @@ -227,6 +227,8 @@ Supported search helpers: - [IndexOf](#indexof) - [LastIndexOf](#lastindexof) +- [HasPrefix](#hasprefix) +- [HasSuffix](#hassuffix) - [Find](#find) - [FindIndexOf](#findindexof) - [FindLastIndexOf](#findlastindexof) @@ -2546,6 +2548,34 @@ notFound := lo.LastIndexOf([]int{0, 1, 2, 1, 2, 3}, 6) // -1 ``` +### HasPrefix + +Returns true if the collection has the prefix. + +```go +ok := lo.HasPrefix([]int{1, 2, 3, 4}, []int{42}) +// false + +ok := lo.HasPrefix([]int{1, 2, 3, 4}, []int{1, 2}) +// true +``` + +[[play](https://go.dev/play/p/SrljzVDpMQM)] + +### HasSuffix + +Returns true if the collection has the suffix. + +```go +ok := lo.HasSuffix([]int{1, 2, 3, 4}, []int{42}) +// false + +ok := lo.HasSuffix([]int{1, 2, 3, 4}, []int{3, 4}) +// true +``` + +[[play](https://go.dev/play/p/bJeLetQNAON)] + ### Find Searches for an element in a slice based on a predicate. Returns element and true if element was found. diff --git a/find.go b/find.go index 0bee773..e93e3ba 100644 --- a/find.go +++ b/find.go @@ -36,6 +36,38 @@ func LastIndexOf[T comparable](collection []T, element T) int { return -1 } +// HasPrefix returns true if the collection has the prefix. +// Play: https://go.dev/play/p/SrljzVDpMQM +func HasPrefix[T comparable](collection []T, prefix []T) bool { + if len(collection) < len(prefix) { + return false + } + + for i := range prefix { + if collection[i] != prefix[i] { + return false + } + } + + return true +} + +// HasSuffix returns true if the collection has the suffix. +// Play: https://go.dev/play/p/bJeLetQNAON +func HasSuffix[T comparable](collection []T, suffix []T) bool { + if len(collection) < len(suffix) { + return false + } + + for i := range suffix { + if collection[len(collection)-len(suffix)+i] != suffix[i] { + return false + } + } + + return true +} + // Find searches for an element in a slice based on a predicate. Returns element and true if element was found. // Play: https://go.dev/play/p/Eo7W0lvKTky func Find[T any](collection []T, predicate func(item T) bool) (T, bool) { diff --git a/find_test.go b/find_test.go index 6bc1b59..340490d 100644 --- a/find_test.go +++ b/find_test.go @@ -30,6 +30,22 @@ func TestLastIndexOf(t *testing.T) { is.Equal(-1, result2) } +func TestHasPrefix(t *testing.T) { + t.Parallel() + is := assert.New(t) + + is.True(HasPrefix([]int{1, 2, 3, 4}, []int{1, 2})) + is.False(HasPrefix([]int{1, 2, 3, 4}, []int{42})) +} + +func TestHasSuffix(t *testing.T) { + t.Parallel() + is := assert.New(t) + + is.True(HasSuffix([]int{1, 2, 3, 4}, []int{3, 4})) + is.False(HasSuffix([]int{1, 2, 3, 4}, []int{42})) +} + func TestFind(t *testing.T) { t.Parallel() is := assert.New(t)