Skip to content

Commit 6e5ec05

Browse files
authored
feat: add exists map
1 parent 079a597 commit 6e5ec05

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

mapx/mapx.go

+6
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,9 @@ func Keep[T1 comparable, T2 any](m map[T1]T2, keys ...T1) {
1919
}
2020
}
2121
}
22+
23+
// Exists checks if a key exists in a map.
24+
func Exists[T1 comparable, T2 any](m map[T1]T2, key T1) bool {
25+
_, ok := m[key]
26+
return ok
27+
}

mapx/mapx_test.go

+10
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,13 @@ func TestKeep(t *testing.T) {
3232
2: "two",
3333
}, m)
3434
}
35+
36+
func TestExists(t *testing.T) {
37+
m := map[int]string{
38+
1: "one",
39+
2: "two",
40+
3: "three",
41+
}
42+
assert.True(t, mapx.Exists(m, 2))
43+
assert.False(t, mapx.Exists(m, 4))
44+
}

0 commit comments

Comments
 (0)