1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-12 16:52:29 +08:00

feat: add ToMap for stream

This commit is contained in:
Pei PeiDong
2025-04-02 10:53:46 +08:00
parent a8a92844f3
commit 4947327ed6
3 changed files with 51 additions and 0 deletions

View File

@@ -400,3 +400,26 @@ func TestStream_LastIndexOf(t *testing.T) {
assert.Equal(-1, s.LastIndexOf(0, func(a, b int) bool { return a == b }))
assert.Equal(4, s.LastIndexOf(2, func(a, b int) bool { return a == b }))
}
func TestStream_ToMap(t *testing.T) {
assert := internal.NewAssert(t, "TestStream_ToMap")
type Person struct {
Name string
Age int
}
s := FromSlice([]Person{
{Name: "Tom", Age: 10},
{Name: "Jim", Age: 20},
{Name: "Mike", Age: 30},
})
m := ToMap(s, func(p Person) (string, Person) {
return p.Name, p
})
expected := map[string]Person{
"Tom": {Name: "Tom", Age: 10},
"Jim": {Name: "Jim", Age: 20},
"Mike": {Name: "Mike", Age: 30},
}
assert.EqualValues(expected, m)
}