underscore/contains_by_test.go
Ruidy 1031038d42
feat: add Chunk, ContainsBy, UniqueBy, ParallelMap, map helpers
- Add `Chunk` to split slices into groups of size n.
- Add `ContainsBy` for predicate-based containment checks.
- Add `UniqueBy` to deduplicate slices by key selector.
- Add `ParallelMap` for concurrent mapping with context and error
handling.
- Add `maps.Keys` and `maps.Values` helpers for extracting map
keys/values.
- Update README and docs for new features.
- Refactor `Contains` to use `slices.Contains`.
2025-09-01 18:03:38 -04:00

25 lines
632 B
Go

package underscore_test
import (
"testing"
"github.com/stretchr/testify/assert"
u "github.com/rjNemo/underscore"
)
func TestContainsBy(t *testing.T) {
nums := []int{1, 3, 5, 8}
assert.True(t, u.ContainsBy(nums, func(n int) bool { return n%2 == 0 }))
assert.False(t, u.ContainsBy(nums, func(n int) bool { return n < 0 }))
}
func TestContainsByStruct(t *testing.T) {
type user struct {
ID int
Name string
}
users := []user{{1, "a"}, {2, "b"}, {3, "c"}}
assert.True(t, u.ContainsBy(users, func(u user) bool { return u.ID == 2 }))
assert.False(t, u.ContainsBy(users, func(u user) bool { return u.Name == "z" }))
}