feat: contains

This commit is contained in:
Ruidy 2021-12-29 13:46:29 -04:00
parent 4c7f3f7868
commit 0f07a76950
3 changed files with 46 additions and 1 deletions

View file

@ -1,4 +1,21 @@
# Underscore
A port from the `underscore.js` library based on generics brought by `go1.18`.
`underscore` is a `Go` library that provides 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`.
## Functions
`underscore` provides 100s of functions that support your favorite functional helpers
### Collections
- `map`
- `filter`
- `reduce`
- `each`
- `some`
- `every`
- `find`
- `contains` (only numerics values at the moment)

14
contains.go Normal file
View file

@ -0,0 +1,14 @@
package underscore
type numbers interface {
int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64 | float32 | float64
}
func Contains[T numbers](values []T, value T) bool {
for _, v := range values {
if v == value {
return true
}
}
return false
}

14
contains_test.go Normal file
View file

@ -0,0 +1,14 @@
package underscore_test
import (
"testing"
"github.com/stretchr/testify/assert"
u "github.com/rjNemo/underscore"
)
func TestContains(t *testing.T) {
nums := []int{1, 3, 5, 7, 9}
assert.True(t, u.Contains(nums, 5))
}