Skip to content

Commit

Permalink
UcFirst accounts for multi-byte leading characters
Browse files Browse the repository at this point in the history
  • Loading branch information
mattcarmody committed Nov 9, 2021
1 parent 342f99b commit 12032df
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 13 deletions.
4 changes: 2 additions & 2 deletions helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ func replaceStr(input, search, replace, types string) string {
}

func ucfirst(val string) string {
for i, v := range val {
return string(unicode.ToUpper(v)) + val[i+1:]
for _, v := range val {
return string(unicode.ToUpper(v)) + val[len(string(v)):]
}
return ""
}
2 changes: 1 addition & 1 deletion stringy.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"unicode"
)

// input is struct that holds input form user and result
// input is struct that holds input from user and result
type input struct {
Input string
Result string
Expand Down
36 changes: 26 additions & 10 deletions stringy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,18 +276,34 @@ func TestInput_TeaseEmpty(t *testing.T) {
}

func TestInput_UcFirst(t *testing.T) {
str := New("this is test")
against := "This is test"
if val := str.UcFirst(); val != against {
t.Errorf("Expected: to be %s but got: %s", against, val)
tests := []struct {
name string
arg string
want string
}{
{
name: "leading lowercase",
arg: "test input",
want: "Test input",
},
{
name: "empty string",
arg: "",
want: "",
},
{
name: "multi-byte leading character",
arg: "δδδ",
want: "Δδδ",
},
}
}

func TestInput_EmptyUcFirst(t *testing.T) {
str := New("")
against := ""
if val := str.UcFirst(); val != against {
t.Errorf("Expected: to be %s but got: %s", against, val)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := New(tt.arg).UcFirst(); got != tt.want {
t.Errorf("UcFirst(%v) = %v, want %v", tt.arg, got, tt.want)
}
})
}
}

Expand Down

0 comments on commit 12032df

Please sign in to comment.