mirror of
https://github.com/rjNemo/underscore
synced 2026-06-06 02:26:42 +00:00
feat: every
This commit is contained in:
parent
dd6d077c36
commit
4c7f3f7868
2 changed files with 27 additions and 0 deletions
12
every.go
Normal file
12
every.go
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
package underscore
|
||||
|
||||
// Every returns true if all the values in the slice pass the predicate truth test.
|
||||
// Short-circuits and stops traversing the slice if a false element is found.
|
||||
func Every[T any](values []T, predicate func(T) bool) bool {
|
||||
for _, v := range values {
|
||||
if !predicate(v) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
15
every_test.go
Normal file
15
every_test.go
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
package underscore_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
u "github.com/rjNemo/underscore"
|
||||
)
|
||||
|
||||
func TestEvery(t *testing.T) {
|
||||
nums := []int{1, 3, 5, 7, 9}
|
||||
isOdd := func(n int) bool { return n%2 != 0 }
|
||||
assert.True(t, u.Every(nums, isOdd))
|
||||
}
|
||||
Loading…
Reference in a new issue