Browse Source

InArrayInt, InArrayInt64, InArrayStr

Volodymyr Tkach 1 month ago
parent
commit
72e25b22ae
2 changed files with 45 additions and 0 deletions
  1. 30 0
      utils/http/helpers/helpers.go
  2. 15 0
      utils/http/helpers/helpers_test.go

+ 30 - 0
utils/http/helpers/helpers.go

@@ -28,6 +28,9 @@ import (
 // func HandleTextJavaScript(data string) http.Handler
 // func HandleTextPlain(data string) http.Handler
 // func HandleTextXml(data string) http.Handler
+// func InArrayInt(num int, arr []int) bool
+// func InArrayInt64(num int64, arr []int64) bool
+// func InArrayStr(str string, arr []string) bool
 // func IntToStr(value int) string
 // func IntToStr64(value int64) string
 // func Md5Hash(str []byte) string
@@ -161,6 +164,33 @@ func HandleTextPlain(data string) http.Handler {
 	return HandleFile(data, "text/plain")
 }
 
+func InArrayInt(num int, arr []int) bool {
+	for _, v := range arr {
+		if num == v {
+			return true
+		}
+	}
+	return false
+}
+
+func InArrayInt64(num int64, arr []int64) bool {
+	for _, v := range arr {
+		if num == v {
+			return true
+		}
+	}
+	return false
+}
+
+func InArrayStr(str string, arr []string) bool {
+	for _, v := range arr {
+		if str == v {
+			return true
+		}
+	}
+	return false
+}
+
 func HandleTextXml(data string) http.Handler {
 	return HandleFile(data, "text/xml")
 }

+ 15 - 0
utils/http/helpers/helpers_test.go

@@ -311,6 +311,21 @@ var _ = Describe("helpers", func() {
 			})
 		})
 
+		Context("InArrayInt", func() {
+			Expect(helpers.InArrayInt(3, []int{1, 2, 3, 4, 5})).To(BeTrue())
+			Expect(helpers.InArrayInt(9, []int{1, 2, 3, 4, 5})).To(BeFalse())
+		})
+
+		Context("InArrayInt64", func() {
+			Expect(helpers.InArrayInt64(3, []int64{1, 2, 3, 4, 5})).To(BeTrue())
+			Expect(helpers.InArrayInt64(9, []int64{1, 2, 3, 4, 5})).To(BeFalse())
+		})
+
+		Context("InArrayStr", func() {
+			Expect(helpers.InArrayStr("3", []string{"1", "2", "3"})).To(BeTrue())
+			Expect(helpers.InArrayStr("9", []string{"1", "2", "3"})).To(BeFalse())
+		})
+
 		Context("HandleTextXml", func() {
 			BeforeEach(func() {
 				srv = httptest.NewServer(helpers.HandleTextXml("MyContent"))