From 4c7f3f78684040cbc0195c0553f85f0768204c80 Mon Sep 17 00:00:00 2001 From: Ruidy Date: Wed, 29 Dec 2021 13:16:26 -0400 Subject: [PATCH] feat: every --- every.go | 12 ++++++++++++ every_test.go | 15 +++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 every.go create mode 100644 every_test.go diff --git a/every.go b/every.go new file mode 100644 index 0000000..fd30f90 --- /dev/null +++ b/every.go @@ -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 +} diff --git a/every_test.go b/every_test.go new file mode 100644 index 0000000..4f5c371 --- /dev/null +++ b/every_test.go @@ -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)) +}