Skip to content

Commit

Permalink
fix: camelcase and pascalcase transformation logic
Browse files Browse the repository at this point in the history
  • Loading branch information
42atomys committed May 16, 2024
1 parent ec86e0b commit ef13a9f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 19 deletions.
31 changes: 22 additions & 9 deletions strings_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,17 @@ import (
// }
// Use `style` to transform "ExampleText" to "example_text"
type caseStyle struct {
Separator rune // Character that separates words.
CapitalizeNext bool // Whether to capitalize the first character of each word.
ForceLowercase bool // Whether to force all characters to lowercase.
ForceUppercase bool // Whether to force all characters to uppercase.
Separator rune // Character that separates words.
CapitalizeFirst bool // Whether to capitalize the first character of the string.
CapitalizeNext bool // Whether to capitalize the first character of each word.
ForceLowercase bool // Whether to force all characters to lowercase.
ForceUppercase bool // Whether to force all characters to uppercase.
}

var (
camelCaseStyle = caseStyle{Separator: -1, CapitalizeNext: true, ForceLowercase: true}
camelCaseStyle = caseStyle{Separator: -1, CapitalizeNext: true, CapitalizeFirst: false, ForceLowercase: true}
kebabCaseStyle = caseStyle{Separator: '-', ForceLowercase: true}
pascalCaseStyle = caseStyle{Separator: -1, CapitalizeNext: true}
pascalCaseStyle = caseStyle{Separator: -1, CapitalizeFirst: true, CapitalizeNext: true}
snakeCaseStyle = caseStyle{Separator: '_', ForceLowercase: true}
dotCaseStyle = caseStyle{Separator: '.', ForceLowercase: true}
pathCaseStyle = caseStyle{Separator: '/', ForceLowercase: true}
Expand Down Expand Up @@ -702,8 +703,13 @@ func (fh *FunctionHandler) Squote(elements ...any) string {
func (fh *FunctionHandler) transformString(style caseStyle, str string) string {
var result strings.Builder
result.Grow(len(str) + 10) // Allocate a bit more for potential separators
capitalizeNext := style.CapitalizeNext
var lastRune, nextRune rune = 0, 0

var capitalizeNext = style.CapitalizeNext
var lastRune, lastLetter, nextRune rune = 0, 0, 0

if !style.CapitalizeFirst {
capitalizeNext = false
}

for i, r := range str {
if i+1 < len(str) {
Expand All @@ -714,7 +720,10 @@ func (fh *FunctionHandler) transformString(style caseStyle, str string) string {
if style.Separator != -1 && (lastRune != style.Separator) {
result.WriteRune(style.Separator)
}
capitalizeNext = true
if lastLetter != 0 {
capitalizeNext = true
}

lastRune = style.Separator
continue
}
Expand All @@ -739,7 +748,11 @@ func (fh *FunctionHandler) transformString(style caseStyle, str string) string {
} else {
result.WriteRune(r)
}

lastRune = r // Update lastRune to the current rune
if unicode.IsLetter(r) {
lastLetter = r
}
}

return result.String()
Expand Down
21 changes: 11 additions & 10 deletions strings_functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,16 +279,17 @@ func TestSquote(t *testing.T) {
func TestToCamelCase(t *testing.T) {
var tests = testCases{
{"TestEmpty", `{{ "" | toCamelCase }}`, "", nil},
{"TestCamelCase", `{{ "foo bar" | toCamelCase }}`, "FooBar", nil},
{"TestCamelCaseWithSpace", `{{ "foo bar" | toCamelCase }}`, "FooBar", nil},
{"TestCamelCaseWithUnderscore", `{{ "foo_bar" | toCamelCase }}`, "FooBar", nil},
{"TestCamelCaseWithHyphen", `{{ "foo-bar" | toCamelCase }}`, "FooBar", nil},
{"TestCamelCaseWithMixed", `{{ "foo-bar_baz" | toCamelCase }}`, "FooBarBaz", nil},
{"", `{{ toCamelCase "_complex__case_" }}`, "ComplexCase", nil},
{"", `{{ toCamelCase "_camel_case" }}`, "CamelCase", nil},
{"", `{{ toCamelCase "http_server" }}`, "HttpServer", nil},
{"", `{{ toCamelCase "no_https" }}`, "NoHttps", nil},
{"", `{{ toCamelCase "all" }}`, "All", nil},
{"TestCamelCase", `{{ "foo bar" | toCamelCase }}`, "fooBar", nil},
{"TestCamelCaseWithUpperCase", `{{ "FoO bar" | toCamelCase }}`, "fooBar", nil},
{"TestCamelCaseWithSpace", `{{ "foo bar" | toCamelCase }}`, "fooBar", nil},
{"TestCamelCaseWithUnderscore", `{{ "foo_bar" | toCamelCase }}`, "fooBar", nil},
{"TestCamelCaseWithHyphen", `{{ "foo-bar" | toCamelCase }}`, "fooBar", nil},
{"TestCamelCaseWithMixed", `{{ "foo-bar_baz" | toCamelCase }}`, "fooBarBaz", nil},
{"", `{{ toCamelCase "___complex__case_" }}`, "complexCase", nil},
{"", `{{ toCamelCase "_camel_case" }}`, "camelCase", nil},
{"", `{{ toCamelCase "http_server" }}`, "httpServer", nil},
{"", `{{ toCamelCase "no_https" }}`, "noHttps", nil},
{"", `{{ toCamelCase "all" }}`, "all", nil},
}

runTestCases(t, tests)
Expand Down

0 comments on commit ef13a9f

Please sign in to comment.