diff --git a/README.md b/README.md index 67085e4..bcc06b6 100644 --- a/README.md +++ b/README.md @@ -6,8 +6,6 @@ ![underscore](https://socialify.git.ci/rjNemo/underscore/image?description=1&font=Raleway&language=1&logo=https%3A%2F%2Fgithub.com%2FrjNemo%2Funderscore%2Fblob%2Fmain%2Fdocs%2Fstatic%2Flogo.png%3Fraw%3Dtrue&name=1&pattern=Floating%20Cogs&theme=Light) - - `underscore` is a `Go` library providing useful functional programming helpers without extending any built-in objects. It is mostly a port from the `underscore.js` library based on generics brought by `go1.18`. @@ -88,9 +86,9 @@ go test ./... ### Collections +- `All` - `Contains` (only numerics values at the moment) - `Each` -- `Every` - `Filter` - `Find` - `Map` @@ -105,7 +103,7 @@ go test ./... Calling `chain.Of` will cause all future method calls to return wrapped values. When you've finished the computation, call `Value` to retrieve the final value. -Methods not returning a slice such as `Reduce`, `Every`, `Some`, will break the `Chain` and return `Value` instantly. +Methods not returning a slice such as `Reduce`, `All`, `Some`, will break the `Chain` and return `Value` instantly. ## Built With diff --git a/every.go b/all.go similarity index 56% rename from every.go rename to all.go index fd30f90..065a5a6 100644 --- a/every.go +++ b/all.go @@ -1,8 +1,8 @@ package underscore -// Every returns true if all the values in the slice pass the predicate truth test. +// All 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 { +func All[T any](values []T, predicate func(T) bool) bool { for _, v := range values { if !predicate(v) { return false diff --git a/every_test.go b/all_test.go similarity index 66% rename from every_test.go rename to all_test.go index 2a6474d..90ab9dd 100644 --- a/every_test.go +++ b/all_test.go @@ -8,14 +8,14 @@ import ( u "github.com/rjNemo/underscore" ) -func TestEvery(t *testing.T) { +func TestAll(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)) + assert.True(t, u.All(nums, isOdd)) } -func TestNotEvery(t *testing.T) { +func TestNotAll(t *testing.T) { nums := []int{1, 3, 5, 7, 9, 10} isOdd := func(n int) bool { return n%2 != 0 } - assert.False(t, u.Every(nums, isOdd)) + assert.False(t, u.All(nums, isOdd)) } diff --git a/chain/chain.go b/chain/chain.go index 5e392cd..2bf7ee3 100644 --- a/chain/chain.go +++ b/chain/chain.go @@ -13,7 +13,7 @@ type Chain[T constraints.Ordered] struct { // Of starts a Chain. All future method calls will return Chain structs. When you've finished the computation, // call Value to retrieve the final value. // -// Methods not returning a slice such as Reduce, Every, Some, will break the chain and return Value instantly. +// Methods not returning a slice such as Reduce, All, Some, will break the chain and return Value instantly. func Of[T constraints.Ordered](value []T) Chain[T] { return Chain[T]{Value: value} } @@ -29,11 +29,11 @@ func (c Chain[T]) Each(action func(T)) { u.Each(c.Value, action) } -// Every returns true if all the values in the slice pass the predicate truth test. +// All 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. // Breaks the Chain. -func (c Chain[T]) Every(predicate func(T) bool) bool { - return u.Every(c.Value, predicate) +func (c Chain[T]) All(predicate func(T) bool) bool { + return u.All(c.Value, predicate) } // Filter looks through each value in the slice, returning a slice of all the values that pass a truth test (predicate). diff --git a/chain/chain_test.go b/chain/chain_test.go index 53458dc..106a46b 100644 --- a/chain/chain_test.go +++ b/chain/chain_test.go @@ -57,7 +57,7 @@ func TestChainFilterMapEvery(t *testing.T) { assert.True(t, chain.Of(nums). Filter(isEven). Map(toSquare). - Every(func(n int) bool { return n%4 == 0 })) + All(func(n int) bool { return n%4 == 0 })) } func TestChainFilterMapFind(t *testing.T) { diff --git a/docs/content/docs/chaining.md b/docs/content/docs/chaining.md index 7dfc750..0fa9bb1 100644 --- a/docs/content/docs/chaining.md +++ b/docs/content/docs/chaining.md @@ -8,18 +8,18 @@ date: 2021-12-31T13:11:41-04:00 Calling `NewChain` will cause all future method calls to return wrapped objects. When you've finished the computation, call `Value` to retrieve the final value. -Methods not returning a collection such as `Reduce`, `Every`, `Some`, will break the chain and return `Value` instantly. +Methods not returning a collection such as `Reduce`, `All`, `Some`, will break the chain and return `Value` instantly. ```go package main import ( "fmt" - u "github.com/rjNemo/underscore" + "github.com/rjNemo/underscore/chain" ) func main() { - sum := u.NewChain([]int{1, 2, 3, 4, 5, 6, 7, 8, 9}). + sum := chain.Of([]int{1, 2, 3, 4, 5, 6, 7, 8, 9}). // filter even numbers from the slice Filter(func(n int) bool { return n%2 == 0 }). // square every number in the slice diff --git a/docs/content/docs/collections.md b/docs/content/docs/collections.md index 70801b4..47dd4f1 100644 --- a/docs/content/docs/collections.md +++ b/docs/content/docs/collections.md @@ -2,6 +2,26 @@ title: "Collections" date: 2021-12-30T13:24:39-04:00 --- + +## All + +`All` 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. + +```go +package main + +import ( + "fmt" + u "github.com/rjNemo/underscore" +) + +func main() { + nums := []int{1, 3, 5, 7, 9} + isOdd := func(n int) bool { return n%2 != 0 } + fmt.Println(u.All(nums, isOdd)) // true +} +``` ## Contains @@ -45,26 +65,6 @@ func main() { } ``` -## Every - -`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. - -```go -package main - -import ( - "fmt" - u "github.com/rjNemo/underscore" -) - -func main() { - nums := []int{1, 3, 5, 7, 9} - isOdd := func(n int) bool { return n%2 != 0 } - fmt.Println(u.Every(nums, isOdd)) // true -} -``` - ## Filter `Filter` looks through each value in the slice, returning a slice of all the values that pass a truth test (predicate).