Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add util methods from core #295

Merged
merged 13 commits into from
Dec 21, 2023
Prev Previous commit
Next Next commit
Add AllEqual
  • Loading branch information
dimriou committed Dec 19, 2023
commit e77cfff12df206c9abd0dd0332f7aef7509b916f
10 changes: 10 additions & 0 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,13 @@ func WrapIfError(err *error, msg string) {
*err = errors.Wrap(*err, msg)
jmank88 marked this conversation as resolved.
Show resolved Hide resolved
}
}

// AllEqual returns true iff all the provided elements are equal to each other.
func AllEqual[T comparable](elems ...T) bool {
for i := 1; i < len(elems); i++ {
if elems[i] != elems[0] {
return false
}
}
return true
}
17 changes: 17 additions & 0 deletions pkg/utils/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package utils_test

import (
"testing"

"github.com/stretchr/testify/require"

"github.com/smartcontractkit/chainlink-common/pkg/utils"
)

func TestAllEqual(t *testing.T) {
t.Parallel()

require.False(t, utils.AllEqual(1, 2, 3, 4, 5))
require.True(t, utils.AllEqual(1, 1, 1, 1, 1))
require.False(t, utils.AllEqual(1, 1, 1, 2, 1, 1, 1))
}
Loading