From b3896cb20cba26352127864ea9291162364d7bd6 Mon Sep 17 00:00:00 2001 From: Roshan Ranabhat Date: Fri, 20 Jan 2023 17:14:12 +0545 Subject: [PATCH 1/2] feat: title case functionality added --- example/main.go | 3 +++ stringy.go | 12 ++++++++++++ stringy_test.go | 8 ++++++++ 3 files changed, 23 insertions(+) diff --git a/example/main.go b/example/main.go index f6b8339..4a0e28f 100644 --- a/example/main.go +++ b/example/main.go @@ -83,4 +83,7 @@ func main() { acronym := stringy.New("Laugh Out Loud") fmt.Println(acronym.Acronym().ToLower()) // lol + + title := stringy.New("this is just AN eXample") + fmt.Println(title.Title()) // This Is Just An Example } diff --git a/stringy.go b/stringy.go index 801a7ab..7d6ef5f 100644 --- a/stringy.go +++ b/stringy.go @@ -39,6 +39,7 @@ type StringManipulation interface { Surround(with string) string SnakeCase(rule ...string) StringManipulation Tease(length int, indicator string) string + Title() string ToLower() string ToUpper() string UcFirst() string @@ -337,6 +338,17 @@ func (i *input) ToLower() (result string) { return strings.ToLower(input) } +// Title makes first letter of each word of user input to uppercase +// it can be chained on function which return StringManipulation interface +func (i *input) Title() (result string) { + input := getInput(*i) + wordArray := strings.Split(input, " ") + for i, word := range wordArray { + wordArray[i] = strings.ToUpper(string(word[0])) + strings.ToLower(word[1:]) + } + return strings.Join(wordArray, " ") +} + // ToUpper makes all string of user input to uppercase // it can be chained on function which return StringManipulation interface func (i *input) ToUpper() string { diff --git a/stringy_test.go b/stringy_test.go index 0ab5ede..bc536b0 100644 --- a/stringy_test.go +++ b/stringy_test.go @@ -292,6 +292,14 @@ func TestInput_TeaseEmpty(t *testing.T) { } } +func TestInput_Title(t *testing.T) { + str := New("this is just AN eXample") + against := "This Is Just An Example" + if val := str.Title(); val != against { + t.Errorf("Expected: to be %s but got: %s", against, val) + } +} + func TestInput_UcFirst(t *testing.T) { tests := []struct { name string From 7d229793dacdad14c3d367eeb2531f7bb69ceaba Mon Sep 17 00:00:00 2001 From: Roshan Ranabhat Date: Fri, 20 Jan 2023 17:15:37 +0545 Subject: [PATCH 2/2] chore: update README.md for title case function --- README.md | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 9fa31f6..5b88722 100644 --- a/README.md +++ b/README.md @@ -55,7 +55,7 @@ Convert string to camel case, snake case, kebab case / slugify, custom delimiter Suffix Acronym - + Title @@ -306,6 +306,15 @@ Tease takes two params length and indicator and it shortens given string on pass fmt.Println(teaseString.Tease(20, "...")) // Hello My name is Ros... ``` +#### Title() string + +Title returns string with first letter of each word in uppercase it can be chained on function which return StringManipulation interface. + +```go + title := stringy.New("hello roshan") + fmt.Println(title.Title()) // Hello Roshan +``` + #### ToLower() string