Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions sx.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,3 +232,47 @@ func PascalCase[T StringOrStringSlice](input T, opts ...CaseOption) string {
return ""
}
}

// lowercaseWord converts the first letter to lowercase
func lowercaseWord(word string) string {
if word == "" {
return word
}

r, size := utf8.DecodeRuneInString(word)
if size == 0 {
return word
}

return string(unicode.ToLower(r)) + word[size:]
}

// CamelCase converts input to camelCase
func CamelCase[T StringOrStringSlice](input T, opts ...CaseOption) string {
switch v := any(input).(type) {
case string:
pascalCase := PascalCase(v, opts...)
return lowercaseWord(pascalCase)
Comment on lines +254 to +255
Copy link

Copilot AI Sep 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Converting to PascalCase first then lowercasing introduces unnecessary work. Consider implementing camelCase directly by reusing the PascalCase logic but applying lowercaseWord to the first word only, similar to how the slice case is handled.

Suggested change
pascalCase := PascalCase(v, opts...)
return lowercaseWord(pascalCase)
options := CaseConfig{}
for _, opt := range opts {
opt(&options)
}
words := splitByCaseWithCustomSeparators(v, nil)
result := joinWords(words, "", func(word string, i int) string {
normalized := normalizeWord(word, options.Normalize)
if i == 0 {
return lowercaseWord(normalized)
}
return capitalizeWord(normalized)
})
return result

Copilot uses AI. Check for mistakes.
case []string:
if len(v) == 0 {
return ""
}

options := CaseConfig{}
for _, opt := range opts {
opt(&options)
}

result := joinWords(v, "", func(word string, i int) string {
normalized := normalizeWord(word, options.Normalize)
if i == 0 {
return lowercaseWord(normalized)
}

return capitalizeWord(normalized)
})
return result
default:
return ""
}
}
95 changes: 95 additions & 0 deletions sx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,3 +274,98 @@ func TestPascalCaseWithSlice(t *testing.T) {
})
}
}

func TestCamelCase(t *testing.T) {
tests := []struct {
name string
input string
expected string
options []sx.CaseOption
}{
{
name: "PascalCase to camelCase",
input: "PascalCase",
expected: "pascalCase",
},
{
name: "kebab-case to camelCase",
input: "kebab-case",
expected: "kebabCase",
},
{
name: "snake_case to camelCase",
input: "snake_case",
expected: "snakeCase",
},
{
name: "XMLHttpRequest",
input: "XMLHttpRequest",
expected: "xMLHttpRequest",
},
{
name: "XMLHttpRequest normalized",
input: "XMLHttpRequest",
expected: "xmlHttpRequest",
options: []sx.CaseOption{sx.WithNormalize(true)},
},
{
name: "empty string",
input: "",
expected: "",
},
{
name: "single word",
input: "Word",
expected: "word",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := sx.CamelCase(tt.input, tt.options...)
if result != tt.expected {
t.Errorf("CamelCase(%q) = %q, want %q", tt.input, result, tt.expected)
}
})
}
}

func TestCamelCaseWithSlice(t *testing.T) {
tests := []struct {
name string
input []string
expected string
options []sx.CaseOption
}{
{
name: "string slice",
input: []string{"hello", "world", "test"},
expected: "helloWorldTest",
},
{
name: "string slice normalized",
input: []string{"HELLO", "WORLD", "TEST"},
expected: "helloWorldTest",
options: []sx.CaseOption{sx.WithNormalize(true)},
},
{
name: "empty slice",
input: []string{},
expected: "",
},
{
name: "single item slice",
input: []string{"Word"},
expected: "word",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := sx.CamelCase(tt.input, tt.options...)
if result != tt.expected {
t.Errorf("CamelCase(%v) = %q, want %q", tt.input, result, tt.expected)
}
})
}
}
Loading