feat: title case functionality added

This commit is contained in:
Roshan Ranabhat 2023-01-20 17:14:12 +05:45
parent 5727f38c3e
commit b3896cb20c
3 changed files with 23 additions and 0 deletions

View File

@ -83,4 +83,7 @@ func main() {
acronym := stringy.New("Laugh Out Loud") acronym := stringy.New("Laugh Out Loud")
fmt.Println(acronym.Acronym().ToLower()) // lol fmt.Println(acronym.Acronym().ToLower()) // lol
title := stringy.New("this is just AN eXample")
fmt.Println(title.Title()) // This Is Just An Example
} }

View File

@ -39,6 +39,7 @@ type StringManipulation interface {
Surround(with string) string Surround(with string) string
SnakeCase(rule ...string) StringManipulation SnakeCase(rule ...string) StringManipulation
Tease(length int, indicator string) string Tease(length int, indicator string) string
Title() string
ToLower() string ToLower() string
ToUpper() string ToUpper() string
UcFirst() string UcFirst() string
@ -337,6 +338,17 @@ func (i *input) ToLower() (result string) {
return strings.ToLower(input) 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 // ToUpper makes all string of user input to uppercase
// it can be chained on function which return StringManipulation interface // it can be chained on function which return StringManipulation interface
func (i *input) ToUpper() string { func (i *input) ToUpper() string {

View File

@ -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) { func TestInput_UcFirst(t *testing.T) {
tests := []struct { tests := []struct {
name string name string