diff --git a/README.md b/README.md index 31978f0..208be6c 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/contains.go b/contains.go new file mode 100644 index 0000000..ac66b96 --- /dev/null +++ b/contains.go @@ -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 +} diff --git a/contains_test.go b/contains_test.go new file mode 100644 index 0000000..d67ad61 --- /dev/null +++ b/contains_test.go @@ -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)) +}