Skip to content

Commit

Permalink
fix #56: New api ToPascalCase and change camelCase to correct value
Browse files Browse the repository at this point in the history
  • Loading branch information
huandu committed Jun 6, 2024
1 parent 6ae33fe commit 1040c04
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 9 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ _Keep this table sorted by Function in ascending order._
| [Count](https://godoc.org/github.com/huandu/xstrings#Count) | `String#count` in Ruby | [#16](https://github.com/huandu/xstrings/issues/16) |
| [Delete](https://godoc.org/github.com/huandu/xstrings#Delete) | `String#delete` in Ruby | [#17](https://github.com/huandu/xstrings/issues/17) |
| [ExpandTabs](https://godoc.org/github.com/huandu/xstrings#ExpandTabs) | `str.expandtabs` in Python | [#27](https://github.com/huandu/xstrings/issues/27) |
| [FirstRuneToLower](https://godoc.org/github.com/huandu/xstrings#FirstRuneToLower) | `lcfirst` in PHP or Perl | [#15](https://github.com/huandu/xstrings/issues/15) |
| [FirstRuneToUpper](https://godoc.org/github.com/huandu/xstrings#FirstRuneToUpper) | `String#capitalize` in Ruby; `ucfirst` in PHP or Perl | [#15](https://github.com/huandu/xstrings/issues/15) |
| [FirstRuneToLower](https://godoc.org/github.com/huandu/xstrings#FirstRuneToLower) | `lcfirst` in PHP or Perl | [#15](https://github.com/huandu/xstrings/issues/15) |
| [FirstRuneToUpper](https://godoc.org/github.com/huandu/xstrings#FirstRuneToUpper) | `String#capitalize` in Ruby; `ucfirst` in PHP or Perl | [#15](https://github.com/huandu/xstrings/issues/15) |
| [Insert](https://godoc.org/github.com/huandu/xstrings#Insert) | `String#insert` in Ruby | [#18](https://github.com/huandu/xstrings/issues/18) |
| [LastPartition](https://godoc.org/github.com/huandu/xstrings#LastPartition) | `str.rpartition` in Python; `String#rpartition` in Ruby | [#19](https://github.com/huandu/xstrings/issues/19) |
| [LeftJustify](https://godoc.org/github.com/huandu/xstrings#LeftJustify) | `str.ljust` in Python; `String#ljust` in Ruby | [#28](https://github.com/huandu/xstrings/issues/28) |
Expand All @@ -50,14 +50,15 @@ _Keep this table sorted by Function in ascending order._
| [RightJustify](https://godoc.org/github.com/huandu/xstrings#RightJustify) | `str.rjust` in Python; `String#rjust` in Ruby | [#29](https://github.com/huandu/xstrings/issues/29) |
| [RuneWidth](https://godoc.org/github.com/huandu/xstrings#RuneWidth) | - | [#27](https://github.com/huandu/xstrings/issues/27) |
| [Scrub](https://godoc.org/github.com/huandu/xstrings#Scrub) | `String#scrub` in Ruby | [#20](https://github.com/huandu/xstrings/issues/20) |
| [Shuffle](https://godoc.org/github.com/huandu/xstrings#Shuffle) | `str_shuffle` in PHP | [#13](https://github.com/huandu/xstrings/issues/13) |
| [ShuffleSource](https://godoc.org/github.com/huandu/xstrings#ShuffleSource) | `str_shuffle` in PHP | [#13](https://github.com/huandu/xstrings/issues/13) |
| [Shuffle](https://godoc.org/github.com/huandu/xstrings#Shuffle) | `str_shuffle` in PHP | [#13](https://github.com/huandu/xstrings/issues/13) |
| [ShuffleSource](https://godoc.org/github.com/huandu/xstrings#ShuffleSource) | `str_shuffle` in PHP | [#13](https://github.com/huandu/xstrings/issues/13) |
| [Slice](https://godoc.org/github.com/huandu/xstrings#Slice) | `mb_substr` in PHP | [#9](https://github.com/huandu/xstrings/issues/9) |
| [Squeeze](https://godoc.org/github.com/huandu/xstrings#Squeeze) | `String#squeeze` in Ruby | [#11](https://github.com/huandu/xstrings/issues/11) |
| [Successor](https://godoc.org/github.com/huandu/xstrings#Successor) | `String#succ` or `String#next` in Ruby | [#22](https://github.com/huandu/xstrings/issues/22) |
| [SwapCase](https://godoc.org/github.com/huandu/xstrings#SwapCase) | `str.swapcase` in Python; `String#swapcase` in Ruby | [#12](https://github.com/huandu/xstrings/issues/12) |
| [ToCamelCase](https://godoc.org/github.com/huandu/xstrings#ToCamelCase) | `String#camelize` in RoR | [#1](https://github.com/huandu/xstrings/issues/1) |
| [ToKebab](https://godoc.org/github.com/huandu/xstrings#ToKebabCase) | - | [#41](https://github.com/huandu/xstrings/issues/41) |
| [ToPascalCase](https://godoc.org/github.com/huandu/xstrings#ToPascalCase) | - | [#1](https://github.com/huandu/xstrings/issues/1) |
| [ToSnakeCase](https://godoc.org/github.com/huandu/xstrings#ToSnakeCase) | `String#underscore` in RoR | [#1](https://github.com/huandu/xstrings/issues/1) |
| [Translate](https://godoc.org/github.com/huandu/xstrings#Translate) | `str.translate` in Python; `String#tr` in Ruby; `strtr` in PHP; `tr///` in Perl | [#21](https://github.com/huandu/xstrings/issues/21) |
| [Width](https://godoc.org/github.com/huandu/xstrings#Width) | `mb_strwidth` in PHP | [#26](https://github.com/huandu/xstrings/issues/26) |
Expand Down
44 changes: 42 additions & 2 deletions convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,37 @@ import (
//
// Some samples.
//
// "some_words" => "someWords"
// "http_server" => "httpServer"
// "no_https" => "noHttps"
// "_complex__case_" => "_complex_Case_"
// "some words" => "someWords"
// "GOLANG_IS_GREAT" => "golangIsGreat"
func ToCamelCase(str string) string {
return toCamelCase(str, false)
}

// ToPascalCase is to convert words separated by space, underscore and hyphen to pascal case.
//
// Some samples.
//
// "some_words" => "SomeWords"
// "http_server" => "HttpServer"
// "no_https" => "NoHttps"
// "_complex__case_" => "_Complex_Case_"
// "some words" => "SomeWords"
func ToCamelCase(str string) string {
// "GOLANG_IS_GREAT" => "GolangIsGreat"
func ToPascalCase(str string) string {
return toCamelCase(str, true)
}

func toCamelCase(str string, isBig bool) string {
if len(str) == 0 {
return ""
}

buf := &stringBuilder{}
var isFirstRuneUpper bool
var r0, r1 rune
var size int

Expand All @@ -33,7 +53,14 @@ func ToCamelCase(str string) string {
str = str[size:]

if !isConnector(r0) {
r0 = unicode.ToUpper(r0)
isFirstRuneUpper = unicode.IsUpper(r0)

if isBig {
r0 = unicode.ToUpper(r0)
} else {
r0 = unicode.ToLower(r0)
}

break
}

Expand All @@ -60,12 +87,25 @@ func ToCamelCase(str string) string {
}

if isConnector(r1) {
isFirstRuneUpper = unicode.IsUpper(r0)
r0 = unicode.ToUpper(r0)
} else {
if isFirstRuneUpper {
if unicode.IsUpper(r0) {
r0 = unicode.ToLower(r0)
} else {
isFirstRuneUpper = false
}
}

buf.WriteRune(r1)
}
}

if isFirstRuneUpper && !isBig {
r0 = unicode.ToLower(r0)
}

buf.WriteRune(r0)
return buf.String()
}
Expand Down
25 changes: 22 additions & 3 deletions convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,37 @@ func TestToSnakeCaseAndToKebabCase(t *testing.T) {

func TestToCamelCase(t *testing.T) {
runTestCases(t, ToCamelCase, _M{
"http_server": "httpServer",
"_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",

"": "",
})
}

func TestToPascalCase(t *testing.T) {
runTestCases(t, ToPascalCase, _M{
"http_server": "HttpServer",
"_camel_case": "_CamelCase",
"no_https": "NoHttps",
"_complex__case_": "_Complex_Case_",
" complex -case ": " Complex Case ",
"all": "All",
"GOLANG_IS_GREAT": "GOLANGISGREAT",
"GOLANG": "GOLANG",
"GOLANG_IS_GREAT": "GolangIsGreat",
"GOLANG": "Golang",
"a": "A",
"好": "好",

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

"": "",
})
Expand Down

0 comments on commit 1040c04

Please sign in to comment.