Skip to content

Commit

Permalink
Merge pull request #9 from mattcarmody/lcFirst-bug
Browse files Browse the repository at this point in the history
Fix LcFirst bug with leading multi-byte character. Addresses #8
  • Loading branch information
gobeam authored Nov 8, 2021
2 parents eb5ffb4 + 571d4cc commit 342f99b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 15 deletions.
6 changes: 3 additions & 3 deletions stringy.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,10 @@ func (i *input) Last(length int) string {
// function which return StringManipulation interface
func (i *input) LcFirst() string {
input := getInput(*i)
for i, v := range input {
return string(unicode.ToLower(v)) + input[i+1:]
for _, v := range input {
return string(unicode.ToLower(v)) + input[len(string(v)):]
}
return input
return ""
}

// Lines returns slice of strings by removing white space characters
Expand Down
40 changes: 28 additions & 12 deletions stringy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,18 +123,34 @@ func TestInput_KebabCase(t *testing.T) {
}

func TestInput_LcFirst(t *testing.T) {
str := New("This is an all lower")
against := "this is an all lower"
if val := str.LcFirst(); val != against {
t.Errorf("Expected: to be %s but got: %s", against, val)
}
}

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

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

Expand Down

0 comments on commit 342f99b

Please sign in to comment.