Skip to content

Replace Split() with Fields() in Title() #30 #32

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions stringy.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ func (i *input) LcFirst() string {

// Lines returns slice of strings by removing white space characters
func (i *input) Lines() []string {
input := getInput(*i)
input := getInput(*i)
result := matchWordRegexp.ReplaceAllString(input, " ")
return strings.Fields(strings.TrimSpace(result))
}
Expand Down Expand Up @@ -363,7 +363,7 @@ func (i *input) ToLower() (result string) {
// it can be chained on function which return StringManipulation interface
func (i *input) Title() (result string) {
input := getInput(*i)
wordArray := strings.Split(input, " ")
wordArray := strings.Fields(input)
for i, word := range wordArray {
wordArray[i] = strings.ToUpper(string(word[0])) + strings.ToLower(word[1:])
}
Expand Down
67 changes: 63 additions & 4 deletions stringy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,10 +313,69 @@ 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)
tests := []struct {
name string
input string
want string
}{
{
name: "basic title case",
input: "this is just AN eXample",
want: "This Is Just An Example",
},
{
name: "multiple spaces",
input: "hello world",
want: "Hello World",
},
{
name: "tabs and spaces",
input: "hello\t\t\tworld ",
want: "Hello World",
},
{
name: "newlines",
input: "hello\nworld",
want: "Hello World",
},
{
name: "mixed whitespace",
input: "hello \t\n world",
want: "Hello World",
},
{
name: "leading and trailing whitespace",
input: " hello world ",
want: "Hello World",
},
{
name: "single word",
input: "hello",
want: "Hello",
},
{
name: "empty string",
input: "",
want: "",
},
{
name: "only whitespace",
input: " \t\n ",
want: "",
},
{
name: "special characters",
input: "hello-world",
want: "Hello-world",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := New(tt.input).Title(); got != tt.want {
t.Errorf("Title() = %q, want %q", got, tt.want)
}
})
}
}

Expand Down