mirror of
https://github.com/rjNemo/underscore
synced 2026-06-06 02:26:42 +00:00
feat: each
This commit is contained in:
parent
fa9989c181
commit
b4e8b7acc8
2 changed files with 29 additions and 0 deletions
7
each.go
Normal file
7
each.go
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
package underscore
|
||||||
|
|
||||||
|
func Each[T any](values []T, predicate func(T)) {
|
||||||
|
for _, v := range values {
|
||||||
|
predicate(v)
|
||||||
|
}
|
||||||
|
}
|
||||||
22
each_test.go
Normal file
22
each_test.go
Normal file
|
|
@ -0,0 +1,22 @@
|
||||||
|
package underscore_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/rjNemo/underscore"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestEach(t *testing.T) {
|
||||||
|
names := []string{"Alice", "Bob", "Charles"}
|
||||||
|
want := []string{"Hi Alice", "Hi Bob", "Hi Charles"}
|
||||||
|
|
||||||
|
res := make([]string, 0)
|
||||||
|
underscore.Each(names, func(n string) {
|
||||||
|
res = append(res, fmt.Sprintf("Hi %s", n))
|
||||||
|
})
|
||||||
|
|
||||||
|
assert.Equal(t, want, res)
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue