feat: add stringx.ToCamelCase (#2622)
This commit is contained in:
@@ -2,6 +2,7 @@ package stringx
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"unicode"
|
||||||
|
|
||||||
"github.com/zeromicro/go-zero/core/lang"
|
"github.com/zeromicro/go-zero/core/lang"
|
||||||
)
|
)
|
||||||
@@ -168,6 +169,15 @@ func TakeWithPriority(fns ...func() string) string {
|
|||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ToCamelCase returns the string that converts the first letter to lowercase.
|
||||||
|
func ToCamelCase(s string) string {
|
||||||
|
for i, v := range s {
|
||||||
|
return string(unicode.ToLower(v)) + s[i+1:]
|
||||||
|
}
|
||||||
|
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
// Union merges the strings in first and second.
|
// Union merges the strings in first and second.
|
||||||
func Union(first, second []string) []string {
|
func Union(first, second []string) []string {
|
||||||
set := make(map[string]lang.PlaceholderType)
|
set := make(map[string]lang.PlaceholderType)
|
||||||
|
|||||||
@@ -396,6 +396,61 @@ func TestTakeWithPriority(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestToCamelCase(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
input string
|
||||||
|
expect string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
input: "",
|
||||||
|
expect: "",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: "A",
|
||||||
|
expect: "a",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: "a",
|
||||||
|
expect: "a",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: "hello_world",
|
||||||
|
expect: "hello_world",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: "Hello_world",
|
||||||
|
expect: "hello_world",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: "hello_World",
|
||||||
|
expect: "hello_World",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: "helloWorld",
|
||||||
|
expect: "helloWorld",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: "HelloWorld",
|
||||||
|
expect: "helloWorld",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: "hello World",
|
||||||
|
expect: "hello World",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: "Hello World",
|
||||||
|
expect: "hello World",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
test := test
|
||||||
|
t.Run(test.input, func(t *testing.T) {
|
||||||
|
assert.Equal(t, test.expect, ToCamelCase(test.input))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestUnion(t *testing.T) {
|
func TestUnion(t *testing.T) {
|
||||||
first := []string{
|
first := []string{
|
||||||
"one",
|
"one",
|
||||||
|
|||||||
Reference in New Issue
Block a user