From 0f07a76950967989d1c68c0b05cf60dbe7fec506 Mon Sep 17 00:00:00 2001 From: Ruidy Date: Wed, 29 Dec 2021 13:46:29 -0400 Subject: [PATCH] feat: contains --- README.md | 19 ++++++++++++++++++- contains.go | 14 ++++++++++++++ contains_test.go | 14 ++++++++++++++ 3 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 contains.go create mode 100644 contains_test.go 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)) +}