mirror of
https://github.com/rjNemo/underscore
synced 2026-06-06 02:26:42 +00:00
each (#10)
* feat: maps map
* ✨: return initial slice
Co-authored-by: Ruidy <rnemausat@newstore.com>
This commit is contained in:
parent
f73905ddb0
commit
7d422c59d3
2 changed files with 14 additions and 1 deletions
4
each.go
4
each.go
|
|
@ -1,8 +1,10 @@
|
|||
package underscore
|
||||
|
||||
// Each iterates over a slice of elements, yielding each in turn to an action function.
|
||||
func Each[T any](values []T, action func(T)) {
|
||||
// Returns the slice for chaining.
|
||||
func Each[T any](values []T, action func(T)) []T {
|
||||
for _, v := range values {
|
||||
action(v)
|
||||
}
|
||||
return values
|
||||
}
|
||||
|
|
|
|||
11
each_test.go
11
each_test.go
|
|
@ -20,3 +20,14 @@ func TestEach(t *testing.T) {
|
|||
|
||||
assert.Equal(t, want, res)
|
||||
}
|
||||
|
||||
func TestEachReturnsInitialSlice(t *testing.T) {
|
||||
names := []string{"Alice", "Bob", "Charles"}
|
||||
want := []string{"Alice", "Bob", "Charles"}
|
||||
|
||||
res := make([]string, 0)
|
||||
|
||||
assert.Equal(t, want, underscore.Each(names, func(n string) {
|
||||
res = append(res, fmt.Sprintf("Hi %s", n))
|
||||
}))
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue