Skip to content

Commit

Permalink
feat: add exists map
Browse files Browse the repository at this point in the history
  • Loading branch information
katallaxie authored Oct 25, 2024
1 parent 079a597 commit 6e5ec05
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
6 changes: 6 additions & 0 deletions mapx/mapx.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,9 @@ func Keep[T1 comparable, T2 any](m map[T1]T2, keys ...T1) {
}
}
}

// Exists checks if a key exists in a map.
func Exists[T1 comparable, T2 any](m map[T1]T2, key T1) bool {
_, ok := m[key]
return ok
}
10 changes: 10 additions & 0 deletions mapx/mapx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,13 @@ func TestKeep(t *testing.T) {
2: "two",
}, m)
}

func TestExists(t *testing.T) {
m := map[int]string{
1: "one",
2: "two",
3: "three",
}
assert.True(t, mapx.Exists(m, 2))
assert.False(t, mapx.Exists(m, 4))
}

0 comments on commit 6e5ec05

Please sign in to comment.