Skip to content

Commit

Permalink
Merge pull request #44 from huandu/issue-ToCamelCase-fail-to-convert-…
Browse files Browse the repository at this point in the history
…single-rune-string

Fix #43. convert single rune string correctly
  • Loading branch information
huandu authored Nov 21, 2019
2 parents 8bbcf2f + 0ff57c8 commit 874a673
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
8 changes: 6 additions & 2 deletions convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,22 @@ func ToCamelCase(str string) string {
str = str[size:]

if r0 != '_' {
r0 = unicode.ToUpper(r0)
break
}

buf.WriteRune(r0)
}

if len(str) == 0 {
// A special case for a string contains only 1 rune.
if size != 0 {
buf.WriteRune(r0)
}

return buf.String()
}

r0 = unicode.ToUpper(r0)

for len(str) > 0 {
r1 = r0
r0, size = utf8.DecodeRuneInString(str)
Expand Down
9 changes: 8 additions & 1 deletion convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ func TestToSnakeCaseAndToKebabCase(t *testing.T) {
"HTTP20xOK": "http_20x_ok",
"HTTP20XStatus": "http_20x_status",
"HTTP-20xStatus": "http_20x_status",
"a": "a",

" sentence case ": "__sentence_case__",
" sentence case ": "__sentence_case__",
" Mixed-hyphen case _and SENTENCE_case and UPPER-case": "_mixed_hyphen_case__and_sentence_case_and_upper_case",

"": "",
Expand All @@ -53,6 +54,8 @@ func TestToCamelCase(t *testing.T) {
"all": "All",
"GOLANG_IS_GREAT": "GolangIsGreat",
"GOLANG": "Golang",
"a": "A",
"好": "好",

"": "",
})
Expand All @@ -62,6 +65,7 @@ func TestSwapCase(t *testing.T) {
runTestCases(t, SwapCase, _M{
"swapCase": "SWAPcASE",
"Θ~λa云Ξπ": "θ~ΛA云ξΠ",
"a": "A",

"": "",
})
Expand All @@ -72,6 +76,7 @@ func TestFirstRuneToUpper(t *testing.T) {
"hello, world!": "Hello, world!",
"Hello, world!": "Hello, world!",
"你好,世界!": "你好,世界!",
"a": "A",

"": "",
})
Expand All @@ -82,6 +87,8 @@ func TestFirstRuneToLower(t *testing.T) {
"hello, world!": "hello, world!",
"Hello, world!": "hello, world!",
"你好,世界!": "你好,世界!",
"a": "a",
"A": "a",

"": "",
})
Expand Down
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
module github.com/huandu/xstrings
module github.com/huandu/xstrings

go 1.12

0 comments on commit 874a673

Please sign in to comment.