mirror of
https://github.com/rjNemo/underscore
synced 2026-06-11 13:06:48 +00:00
feat: contains
This commit is contained in:
parent
4c7f3f7868
commit
0f07a76950
3 changed files with 46 additions and 1 deletions
19
README.md
19
README.md
|
|
@ -1,4 +1,21 @@
|
||||||
# Underscore
|
# 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
14
contains.go
Normal 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
14
contains_test.go
Normal 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))
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue