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