From d1e68e0dc3b1613218d8d92d4e0bf26db3d322b6 Mon Sep 17 00:00:00 2001 From: yuu Date: Wed, 11 Oct 2023 20:25:28 +0700 Subject: [PATCH 1/2] feat: ucfirst implemented --- go/readme.md | 8 ++++++ go/ucfirst.go | 22 +++++++++++++++ go/ucfirst_test.go | 70 ++++++++++++++++++++++++++++++++++++++++++++++ main.go | 27 ++++++++++++++++++ 4 files changed, 127 insertions(+) create mode 100644 go/ucfirst.go create mode 100644 go/ucfirst_test.go create mode 100644 main.go diff --git a/go/readme.md b/go/readme.md index 6e2dd27..4f931b9 100644 --- a/go/readme.md +++ b/go/readme.md @@ -222,4 +222,12 @@ shuffle := StrShuffle("abcdef") //This will print something like: "bfdaec" fmt.Println(shuffle) +``` + +### `Ucfirst` + +```go +var converted = pehape.Ucfirst("hello world") +fmt.Println(converted) +//result : "Hello world" ``` \ No newline at end of file diff --git a/go/ucfirst.go b/go/ucfirst.go new file mode 100644 index 0000000..5920cf7 --- /dev/null +++ b/go/ucfirst.go @@ -0,0 +1,22 @@ +package pehape + +import ( + "unicode" + "unicode/utf8" +) + +// The Ucfirst() function converts the first character of a string to uppercase. +// Parameter +// - str => string to be converted +// Return +// - res => converted string +func Ucfirst(str string) string { + if str == "" { + return "" + } + + r, size := utf8.DecodeRuneInString(str) + + u := unicode.ToUpper(r) + return string(u) + str[size:] +} diff --git a/go/ucfirst_test.go b/go/ucfirst_test.go new file mode 100644 index 0000000..86e0c00 --- /dev/null +++ b/go/ucfirst_test.go @@ -0,0 +1,70 @@ +package pehape_test + +import ( + "testing" + + PHP "github.com/teknologi-umum/pehape/go" +) + +func TestUcfirst(t *testing.T) { + type test struct { + param string + expect string + } + + testCases := []test{ + { + param: "Hello", + expect: "Hello", + }, + { + param: "HELLO", + expect: "HELLO", + }, + { + param: "GoLang", + expect: "GoLang", + }, + { + param: "golang", + expect: "Golang", + }, + { + param: "gOLANG", + expect: "GOLANG", + }, + { + param: "g", + expect: "G", + }, + { + param: "!@@#4#Golang", + expect: "!@@#4#Golang", + }, + { + param: "每一次搜索如何在", + expect: "每一次搜索如何在", + }, + { + param: "🙂🤣", + expect: "🙂🤣", + }, + } + + for _, test := range testCases { + if res := PHP.Ucfirst(test.param); res != test.expect { + t.Errorf("expect %s, but got %s", test.expect, res) + } + } + + t.Run("It should return empty string if the given parameter is empty string", func(t *testing.T) { + test := test{ + param: "", + expect: "", + } + + if res := PHP.Ucfirst(test.param); res != test.expect { + t.Errorf("expect empty string, but got %s", res) + } + }) +} diff --git a/main.go b/main.go new file mode 100644 index 0000000..fa41563 --- /dev/null +++ b/main.go @@ -0,0 +1,27 @@ +package main + +import ( + "fmt" + "unicode/utf8" +) + +func main() { + var a = "a" + + var c = "🙂a" + + var b = []rune(a) + + var d = []rune(c) + e, size := utf8.DecodeRuneInString(c) + + fmt.Println(b) + fmt.Println(string(b)) + + fmt.Println(b[0] - 32) + fmt.Println(string(b[0] - 32)) + + fmt.Println(d) + fmt.Println(e) + fmt.Println(size) +} From 39b8f1289153c395406c0dd2fd545ef005bdcec3 Mon Sep 17 00:00:00 2001 From: yuu Date: Fri, 13 Oct 2023 21:17:08 +0700 Subject: [PATCH 2/2] delte unecessary code --- main.go | 27 --------------------------- 1 file changed, 27 deletions(-) delete mode 100644 main.go diff --git a/main.go b/main.go deleted file mode 100644 index fa41563..0000000 --- a/main.go +++ /dev/null @@ -1,27 +0,0 @@ -package main - -import ( - "fmt" - "unicode/utf8" -) - -func main() { - var a = "a" - - var c = "🙂a" - - var b = []rune(a) - - var d = []rune(c) - e, size := utf8.DecodeRuneInString(c) - - fmt.Println(b) - fmt.Println(string(b)) - - fmt.Println(b[0] - 32) - fmt.Println(string(b[0] - 32)) - - fmt.Println(d) - fmt.Println(e) - fmt.Println(size) -}