From 48bc679579131f9950ef946371e46b088664e4dc Mon Sep 17 00:00:00 2001 From: Jan Galek Date: Sun, 22 Dec 2024 23:49:34 +0100 Subject: [PATCH 1/3] [Add] coverage to tests --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index ca4a860..11b3038 100644 --- a/Makefile +++ b/Makefile @@ -4,4 +4,4 @@ install: go mod tidy && go mod vendor tests: - go test -v ./tests \ No newline at end of file + go test -v -covermode=set ./... -coverprofile=coverage.txt && go tool cover -func=coverage.txt \ No newline at end of file From 6c26f6850c4f2e193547085a401c3073d6add145 Mon Sep 17 00:00:00 2001 From: Jan Galek Date: Sun, 22 Dec 2024 23:49:58 +0100 Subject: [PATCH 2/3] [Fix] typo in versions title --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 0385507..ea8e8c9 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ utils [![Go Report Card](https://goreportcard.com/badge/github.com/gouef/utils)](https://goreportcard.com/report/github.com/gouef/utils) [![codecov](https://codecov.io/github/gouef/utils/branch/main/graph/badge.svg?token=YUG8EMH6Q8)](https://codecov.io/github/gouef/utils) -## Vesions +## Versions ![Stable Version](https://img.shields.io/github/v/release/gouef/utils?label=Stable&labelColor=green) ![GitHub Release](https://img.shields.io/github/v/release/gouef/utils?label=RC&include_prereleases&filter=*rc*&logoSize=diago) ![GitHub Release](https://img.shields.io/github/v/release/gouef/utils?label=Beta&include_prereleases&filter=*beta*&logoSize=diago) From 9c6fda1a129fd3f175169b2253ad05c149e087b1 Mon Sep 17 00:00:00 2001 From: Jan Galek Date: Sun, 22 Dec 2024 23:50:34 +0100 Subject: [PATCH 3/3] [Add] InListArray function --- array.go | 11 +++++++++++ tests/array_test.go | 21 +++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/array.go b/array.go index cbc9b84..5dcd9c1 100644 --- a/array.go +++ b/array.go @@ -13,6 +13,17 @@ func InArray[T comparable](needle T, haystack []T) bool { return false } +func InListArray[T comparable](needles []T, haystack []T) bool { + + for _, needle := range needles { + if InArray(needle, haystack) { + return true + } + } + + return false +} + func Explode(separator string, stringStr string) []string { if separator == "" { return []string{stringStr} diff --git a/tests/array_test.go b/tests/array_test.go index c6dc971..2a39469 100644 --- a/tests/array_test.go +++ b/tests/array_test.go @@ -26,6 +26,27 @@ func TestInArray(t *testing.T) { } } +func TestInListArray(t *testing.T) { + tests := []struct { + needle []string + haystack []string + expected bool + }{ + {[]string{"apple"}, []string{"apple", "banana", "cherry"}, true}, + {[]string{"cherry"}, []string{"apple", "banana", "cherry"}, true}, + {[]string{"pineapple", "banana"}, []string{"apple", "banana", "cherry", "pineapple"}, true}, + {[]string{"orange"}, []string{"apple", "banana", "cherry"}, false}, + {[]string{""}, []string{"apple", "banana", "cherry"}, false}, + } + + for _, tt := range tests { + t.Run(utils.Implode("-", tt.needle), func(t *testing.T) { + result := utils.InListArray(tt.needle, tt.haystack) + assert.Equal(t, tt.expected, result) + }) + } +} + func TestExplode(t *testing.T) { tests := []struct { separator string