Skip to content

Commit

Permalink
Adds AnyValueInMap helper (#8)
Browse files Browse the repository at this point in the history
* Adds AnyValueInMap helper

* fix linting error

* fix go version in lint action
  • Loading branch information
aidenwallis authored Jun 14, 2022
1 parent 4e56de3 commit a90c5be
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
steps:
- uses: actions/setup-go@v3
with:
go-version: 1.17
go-version: 1.18
- uses: actions/checkout@v3
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
Expand Down
11 changes: 11 additions & 0 deletions utils/any_value_in_map.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package utils

// AnyValueInMap iterates through values and returns true if any has a key in the map.
func AnyValueInMap[T comparable, V any](m map[T]V, values ...T) bool {
for _, v := range values {
if _, ok := m[v]; ok {
return true
}
}
return false
}
21 changes: 21 additions & 0 deletions utils/any_value_in_map_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package utils_test

import (
"testing"

"github.com/aidenwallis/go-utils/utils"
"github.com/stretchr/testify/assert"
)

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

m := map[string]struct{}{
"1": {},
"2": {},
"3": {},
}

assert.True(t, utils.AnyValueInMap(m, "foobar", "1"))
assert.False(t, utils.AnyValueInMap(m, "foobar"))
}
2 changes: 1 addition & 1 deletion utils/ternary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ import (
func TestTernary(t *testing.T) {
t.Parallel()

assert.Equal(t, "a", utils.Ternary(1 == 1, "a", "b"))
assert.Equal(t, "a", utils.Ternary(1 == 1, "a", "b")) // nolint:staticcheck // intended
assert.Equal(t, "b", utils.Ternary(1 == 2, "a", "b"))
}

0 comments on commit a90c5be

Please sign in to comment.