underscore/maps/map_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

41 lines
824 B
Go

package maps_test
import (
"testing"
"github.com/stretchr/testify/assert"
u "github.com/rjNemo/underscore"
m "github.com/rjNemo/underscore/maps"
)
func TestMap(t *testing.T) {
scores := m.M[string, int]{
"alice": 0,
"bob": 10,
"clara": 7,
"david": 23,
}
hasWon := func(key string, value int) m.M[string, bool] { return m.M[string, bool]{key: value > 21} }
want := m.M[string, bool]{
"alice": false,
"bob": false,
"clara": false,
"david": true,
}
assert.Equal(t, want, m.Map(scores, hasWon))
}
func TestMapSlices(t *testing.T) {
scores := []m.M[string, int]{
{"score": 0},
{"score": 10},
{"score": 7},
{"score": 23},
}
hasWon := func(s m.M[string, int]) bool { return s["score"] > 21 }
want := []bool{false, false, false, true}
assert.Equal(t, want, u.Map(scores, hasWon))
}