mirror of
https://github.com/duke-git/lancet.git
synced 2026-11-16 05:12:11 +08:00
feat: add UpperCamelCase
This commit is contained in:
1 parent
b252b415fa
commit
de682f2214
5 files changed
+58
-2
No files matched your search
@@ -34,6 +34,23 @@ func CamelCase(s string) string {
|
||||
return builder.String()
|
||||
}
|
||||
|
||||
// UpperCamelCase coverts string to UpperCamelCase string. Non letters and numbers will be ignored.
|
||||
// Play: https://go.dev/play/p/todo
|
||||
func UpperCamelCase(s string) string {
|
||||
var builder strings.Builder
|
||||
|
||||
strs := splitIntoStrings(s, false)
|
||||
for _, str := range strs {
|
||||
// if i == 0 {
|
||||
// builder.WriteString(strings.ToLower(str))
|
||||
// } else {
|
||||
builder.WriteString(Capitalize(str))
|
||||
// }
|
||||
}
|
||||
|
||||
return builder.String()
|
||||
}
|
||||
|
||||
// Capitalize converts the first character of a string to upper case and the remaining to lower case.
|
||||
// Play: https://go.dev/play/p/2OAjgbmAqHZ
|
||||
func Capitalize(s string) string {
|
||||
|
||||
@@ -94,6 +94,21 @@ func ExampleCamelCase() {
|
||||
// foo11Bar
|
||||
}
|
||||
|
||||
func ExampleUpperCamelCase() {
|
||||
strings := []string{"", "foobar", "&FOO:BAR$BAZ", "$foo%", "Foo-#1😄$_%^&*(1bar"}
|
||||
|
||||
for _, v := range strings {
|
||||
s := UpperCamelCase(v)
|
||||
fmt.Println(s)
|
||||
}
|
||||
// Output:
|
||||
//
|
||||
// Foobar
|
||||
// FooBarBaz
|
||||
// Foo
|
||||
// Foo11Bar
|
||||
}
|
||||
|
||||
func ExampleCapitalize() {
|
||||
strings := []string{"", "Foo", "_foo", "fooBar", "foo-bar"}
|
||||
|
||||
|
||||
@@ -30,6 +30,30 @@ func TestCamelCase(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpperCamelCase(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
assert := internal.NewAssert(t, "TestUpperCamelCase")
|
||||
|
||||
tests := []struct {
|
||||
input string
|
||||
expected string
|
||||
}{
|
||||
{"", ""},
|
||||
{"foobar", "Foobar"},
|
||||
{"&FOO:BAR$BAZ", "FooBarBaz"},
|
||||
{"fooBar", "FooBar"},
|
||||
{"FOObar", "FoObar"},
|
||||
{"$foo%", "Foo"},
|
||||
{" $#$Foo 22 bar ", "Foo22Bar"},
|
||||
{"Foo-#1😄$_%^&*(1bar", "Foo11Bar"},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
assert.Equal(tt.expected, UpperCamelCase(tt.input))
|
||||
}
|
||||
}
|
||||
|
||||
func TestCapitalize(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
|
||||
Reference in new issue
Block a user