Skip to content

Commit

Permalink
fix #45. ToCamelCase is aware of space and hyphen
Browse files Browse the repository at this point in the history
  • Loading branch information
huandu committed Jan 12, 2020
1 parent 874a673 commit 73cc880
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 18 deletions.
36 changes: 18 additions & 18 deletions convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@ import (
"unicode/utf8"
)

// ToCamelCase can convert all lower case characters behind underscores
// to upper case character.
// Underscore character will be removed in result except following cases.
// * More than 1 underscore.
// "a__b" => "A_B"
// * At the beginning of string.
// "_a" => "_A"
// * At the end of string.
// "ab_" => "Ab_"
// ToCamelCase is to convert words separated by space, underscore and hyphen to camel case.
//
// Some samples.
// "some_words" => "SomeWords"
// "http_server" => "HttpServer"
// "no_https" => "NoHttps"
// "_complex__case_" => "_Complex_Case_"
// "some words" => "SomeWords"
func ToCamelCase(str string) string {
if len(str) == 0 {
return ""
Expand All @@ -28,12 +27,12 @@ func ToCamelCase(str string) string {
var r0, r1 rune
var size int

// leading '_' will appear in output.
// leading connector will appear in output.
for len(str) > 0 {
r0, size = utf8.DecodeRuneInString(str)
str = str[size:]

if r0 != '_' {
if !isConnector(r0) {
r0 = unicode.ToUpper(r0)
break
}
Expand All @@ -55,18 +54,15 @@ func ToCamelCase(str string) string {
r0, size = utf8.DecodeRuneInString(str)
str = str[size:]

if r1 == '_' && r0 == '_' {
if isConnector(r0) && isConnector(r1) {
buf.WriteRune(r1)
continue
}

if r1 == '_' {
if isConnector(r1) {
r0 = unicode.ToUpper(r0)
} else {
r0 = unicode.ToLower(r0)
}

if r1 != '_' {
buf.WriteRune(r1)
}
}
Expand Down Expand Up @@ -164,7 +160,7 @@ func camelCaseToLowerCase(str string, connector rune) string {
}

if !unicode.IsUpper(r0) {
if r0 == '_' || r0 == ' ' || r0 == '-' {
if isConnector(r0) {
r0 = connector

buf.WriteRune(unicode.ToLower(r1))
Expand Down Expand Up @@ -198,7 +194,7 @@ func camelCaseToLowerCase(str string, connector rune) string {
buf.WriteRune(r0)

default:
if r0 == ' ' || r0 == '-' || r0 == '_' {
if isConnector(r0) {
r0 = connector
}

Expand All @@ -209,6 +205,10 @@ func camelCaseToLowerCase(str string, connector rune) string {
return buf.String()
}

func isConnector(r rune) bool {
return r == '-' || r == '_' || unicode.IsSpace(r)
}

// SwapCase will swap characters case from upper to lower or lower to upper.
func SwapCase(str string) string {
var r rune
Expand Down
4 changes: 4 additions & 0 deletions convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func TestToSnakeCaseAndToKebabCase(t *testing.T) {

" sentence case ": "__sentence_case__",
" Mixed-hyphen case _and SENTENCE_case and UPPER-case": "_mixed_hyphen_case__and_sentence_case_and_upper_case",
"FROM CamelCase to snake/kebab-case": "from_camel_case_to_snake/kebab_case",

"": "",
"Abc\uFFFDE\uFFFDf\uFFFDd\uFFFD2\uFFFD00Z\uFFFDZZ\uFFFDZZ": "abc\uFFFD_e\uFFFDf\uFFFDd\uFFFD_2\uFFFD_00z\uFFFD_zz\uFFFD_zz",
Expand All @@ -51,12 +52,15 @@ func TestToCamelCase(t *testing.T) {
"_camel_case": "_CamelCase",
"no_https": "NoHttps",
"_complex__case_": "_Complex_Case_",
" complex -case ": " Complex Case ",
"all": "All",
"GOLANG_IS_GREAT": "GolangIsGreat",
"GOLANG": "Golang",
"a": "A",
"好": "好",

"FROM CamelCase to snake/kebab-case": "FromCamelcaseToSnake/kebabCase",

"": "",
})
}
Expand Down

0 comments on commit 73cc880

Please sign in to comment.