Browse Source

StrToInt, IntToStr

Volodymyr Tkach 1 year ago
parent
commit
698ec9cfd6
3 changed files with 59 additions and 3 deletions
  1. 4 1
      README.md
  2. 23 2
      utils/http/helpers/helpers.go
  3. 32 0
      utils/http/helpers/helpers_test.go

+ 4 - 1
README.md

@@ -146,7 +146,8 @@ func HandleTextCss(data string) http.Handler
 func HandleTextJavaScript(data string) http.Handler
 func HandleTextPlain(data string) http.Handler
 func HandleTextXml(data string) http.Handler
-func IntToStr(value int64) string
+func IntToStr(value int) string
+func IntToStr64(value int64) string
 func Md5Hash(str []byte) string
 func MinifyHtmlCode(str string) string
 func MinifyHtmlJsCode(str string) string
@@ -155,6 +156,8 @@ func RespondAsInternalServerError(w http.ResponseWriter, r *http.Request)
 func RespondAsMethodNotAllowed(w http.ResponseWriter, r *http.Request)
 func SessionStart(w http.ResponseWriter, r *http.Request) (*session.Session, error)
 func SetLanguageCookie(w http.ResponseWriter, r *http.Request) error
+func StrToInt(value string) int
+func StrToInt64(value string) int64
 ```
 
 ## utils/http/logger

+ 23 - 2
utils/http/helpers/helpers.go

@@ -28,7 +28,8 @@ import (
 // func HandleTextJavaScript(data string) http.Handler
 // func HandleTextPlain(data string) http.Handler
 // func HandleTextXml(data string) http.Handler
-// func IntToStr(value int64) string
+// func IntToStr(value int) string
+// func IntToStr64(value int64) string
 // func Md5Hash(str []byte) string
 // func MinifyHtmlCode(str string) string
 // func MinifyHtmlJsCode(str string) string
@@ -37,6 +38,8 @@ import (
 // func RespondAsMethodNotAllowed(w http.ResponseWriter, r *http.Request)
 // func SessionStart(w http.ResponseWriter, r *http.Request) (*session.Session, error)
 // func SetLanguageCookie(w http.ResponseWriter, r *http.Request) error
+// func StrToInt(value string) int
+// func StrToInt64(value string) int64
 
 var mHtml = regexp.MustCompile(`>[\n\t\r]+<`)
 var mHtmlLeft = regexp.MustCompile(`>[\n\t\r]+`)
@@ -162,7 +165,11 @@ func HandleTextXml(data string) http.Handler {
 	return HandleFile(data, "text/xml")
 }
 
-func IntToStr(value int64) string {
+func IntToStr(value int) string {
+	return strconv.FormatInt(int64(value), 10)
+}
+
+func IntToStr64(value int64) string {
 	return strconv.FormatInt(value, 10)
 }
 
@@ -281,6 +288,20 @@ func SetLanguageCookie(w http.ResponseWriter, r *http.Request) error {
 	return nil
 }
 
+func StrToInt(value string) int {
+	if v, err := strconv.Atoi(value); err == nil {
+		return v
+	}
+	return 0
+}
+
+func StrToInt64(value string) int64 {
+	if v, err := strconv.ParseInt(value, 10, 64); err == nil {
+		return v
+	}
+	return 0
+}
+
 // -----------------------------------------------------------------------------
 
 // For funcs which write some data to http.ResponseWriter

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

@@ -345,6 +345,16 @@ var _ = Describe("helpers", func() {
 			})
 		})
 
+		Context("IntToStr64", func() {
+			It("convert int to string", func() {
+				Expect(helpers.IntToStr64(1)).To(Equal("1"))
+				Expect(helpers.IntToStr64(5)).To(Equal("5"))
+				Expect(helpers.IntToStr64(50)).To(Equal("50"))
+				Expect(helpers.IntToStr64(100)).To(Equal("100"))
+				Expect(helpers.IntToStr64(1000)).To(Equal("1000"))
+			})
+		})
+
 		Context("RespondAsBadRequest", func() {
 			BeforeEach(func() {
 				var handler = func() http.HandlerFunc {
@@ -469,6 +479,28 @@ var _ = Describe("helpers", func() {
 		})
 	})
 
+	Context("StrToInt", func() {
+		It("convert int to string", func() {
+			Expect(helpers.StrToInt("")).To(Equal(0))
+			Expect(helpers.StrToInt("1")).To(Equal(1))
+			Expect(helpers.StrToInt("5")).To(Equal(5))
+			Expect(helpers.StrToInt("50")).To(Equal(50))
+			Expect(helpers.StrToInt("100")).To(Equal(100))
+			Expect(helpers.StrToInt("1000")).To(Equal(1000))
+		})
+	})
+
+	Context("StrToInt64", func() {
+		It("convert int to string", func() {
+			Expect(helpers.StrToInt64("")).To(Equal(int64(0)))
+			Expect(helpers.StrToInt64("1")).To(Equal(int64(1)))
+			Expect(helpers.StrToInt64("5")).To(Equal(int64(5)))
+			Expect(helpers.StrToInt64("50")).To(Equal(int64(50)))
+			Expect(helpers.StrToInt64("100")).To(Equal(int64(100)))
+			Expect(helpers.StrToInt64("1000")).To(Equal(int64(1000)))
+		})
+	})
+
 	Context("FakeResponseWriter", func() {
 		It("write data to fake response writer", func() {
 			var someHandleFunc = func(w http.ResponseWriter) {