Skip to content

Commit 6c57914

Browse files
committed
feat(map_): Contains, KeyChecker
1 parent 97781cd commit 6c57914

File tree

2 files changed

+81
-5
lines changed

2 files changed

+81
-5
lines changed

map_/api.go

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -434,13 +434,16 @@ func NotEmpty[M ~map[K]V, K comparable, V any](val M) bool {
434434

435435
// Get returns the value by the specified key from the map m or zero if the map doesn't contain that key
436436
func Get[M ~map[K]V, K comparable, V any](m M, key K) V {
437-
return m[key]
437+
val, _ := GetOk(m, key)
438+
return val
438439
}
439440

440441
// GetOk returns the value, and true by the specified key from the map m or zero and false if the map doesn't contain that key
441-
func GetOk[M ~map[K]V, K comparable, V any](m M, key K) (V, bool) {
442-
v, ok := m[key]
443-
return v, ok
442+
func GetOk[M ~map[K]V, K comparable, V any](m M, key K) (val V, ok bool) {
443+
if m != nil {
444+
val, ok = m[key]
445+
}
446+
return val, ok
444447
}
445448

446449
// Getter creates a function that can be used for retrieving a value from the map m by a key
@@ -452,3 +455,18 @@ func Getter[M ~map[K]V, K comparable, V any](m M) func(key K) V {
452455
func GetterOk[M ~map[K]V, K comparable, V any](m M) func(key K) (V, bool) {
453456
return func(key K) (V, bool) { return GetOk(m, key) }
454457
}
458+
459+
// Contains checks is the map contains a key
460+
func Contains[M ~map[K]V, K comparable, V any](m M, key K) (ok bool) {
461+
if m != nil {
462+
_, ok = m[key]
463+
}
464+
return ok
465+
}
466+
467+
// KeyChecker creates a function that can be used to check if the map contains a key
468+
func KeyChecker[M ~map[K]V, K comparable, V any](m M) func(key K) (ok bool) {
469+
return func(key K) (ok bool) {
470+
return Contains(m, key)
471+
}
472+
}

map_/test/api_test.go

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"strconv"
55
"testing"
66

7+
"github.com/stretchr/testify/assert"
8+
79
"github.com/m4gshm/gollections/convert/ptr"
810
"github.com/m4gshm/gollections/map_"
911
"github.com/m4gshm/gollections/map_/clone"
@@ -12,7 +14,6 @@ import (
1214
"github.com/m4gshm/gollections/op"
1315
"github.com/m4gshm/gollections/slice"
1416
"github.com/m4gshm/gollections/slice/clone/sort"
15-
"github.com/stretchr/testify/assert"
1617
)
1718

1819
type entity struct{ val string }
@@ -178,3 +179,60 @@ func Test_ToSliceErrorable(t *testing.T) {
178179
})
179180
assert.Equal(t, slice.Of(2, 4, 6), sort.Of(result))
180181
}
182+
183+
func Test_Filter(t *testing.T) {
184+
elements := map[int]string{4: "4", 2: "2", 1: "1", 3: "3"}
185+
186+
result := map_.Filter(elements, func(key int, val string) bool { return key <= 2 || val == "4" })
187+
check := map_.KeyChecker(result)
188+
assert.Equal(t, 3, len(result))
189+
assert.True(t, check(1))
190+
assert.True(t, check(2))
191+
assert.False(t, check(3))
192+
assert.True(t, check(4))
193+
}
194+
195+
func Test_FilterKeys(t *testing.T) {
196+
elements := map[int]string{4: "4", 2: "2", 1: "1", 3: "3"}
197+
198+
result := map_.FilterKeys(elements, func(key int) bool { return key <= 2 })
199+
check := map_.KeyChecker(result)
200+
assert.Equal(t, 2, len(result))
201+
assert.True(t, check(1))
202+
assert.True(t, check(2))
203+
assert.False(t, check(3))
204+
assert.False(t, check(4))
205+
}
206+
207+
func Test_FilterValues(t *testing.T) {
208+
elements := map[int]string{4: "4", 2: "2", 1: "1", 3: "3"}
209+
210+
result := map_.FilterValues(elements, func(val string) bool { return val <= "2" })
211+
check := map_.KeyChecker(result)
212+
assert.Equal(t, 2, len(result))
213+
assert.True(t, check(1))
214+
assert.True(t, check(2))
215+
assert.False(t, check(3))
216+
assert.False(t, check(4))
217+
}
218+
219+
func Test_Getter(t *testing.T) {
220+
elements := map[int]string{4: "4", 2: "2", 1: "1", 3: "3"}
221+
222+
getter := map_.Getter(elements)
223+
assert.Equal(t, "1", getter(1))
224+
assert.Equal(t, "2", getter(2))
225+
assert.Equal(t, "3", getter(3))
226+
assert.Equal(t, "4", getter(4))
227+
228+
nilGetter := map_.Getter[map[int]string](nil)
229+
230+
assert.Equal(t, "", nilGetter(0))
231+
232+
getterOk := map_.GetterOk(elements)
233+
234+
_, ok := getterOk(1)
235+
assert.True(t, ok)
236+
_, ok = getterOk(0)
237+
assert.False(t, ok)
238+
}

0 commit comments

Comments
 (0)