mirror of
https://github.com/duke-git/lancet.git
synced 2026-03-01 00:35:28 +08:00
refactor: refact CamelCase function
This commit is contained in:
@@ -10,73 +10,21 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// CamelCase covert string to camelCase string.
|
// CamelCase covert string to camelCase string.
|
||||||
|
// non letters and numbers will be ignored
|
||||||
|
// eg. "Foo-#1😄$_%^&*(1bar" => "foo11Bar"
|
||||||
func CamelCase(s string) string {
|
func CamelCase(s string) string {
|
||||||
var runes [][]rune
|
var builder strings.Builder
|
||||||
lastCharType := 0
|
|
||||||
charType := 0
|
|
||||||
|
|
||||||
// split into fields based on type of unicode character
|
strs := splitIntoStrings(s, false)
|
||||||
for _, r := range s {
|
for i, str := range strs {
|
||||||
switch true {
|
|
||||||
case unicode.IsLower(r):
|
|
||||||
charType = 1
|
|
||||||
case unicode.IsUpper(r):
|
|
||||||
charType = 2
|
|
||||||
case unicode.IsDigit(r):
|
|
||||||
charType = 3
|
|
||||||
default:
|
|
||||||
charType = 4
|
|
||||||
}
|
|
||||||
|
|
||||||
if charType == lastCharType {
|
|
||||||
runes[len(runes)-1] = append(runes[len(runes)-1], r)
|
|
||||||
} else {
|
|
||||||
runes = append(runes, []rune{r})
|
|
||||||
}
|
|
||||||
lastCharType = charType
|
|
||||||
}
|
|
||||||
|
|
||||||
for i := 0; i < len(runes)-1; i++ {
|
|
||||||
if unicode.IsUpper(runes[i][0]) && unicode.IsLower(runes[i+1][0]) {
|
|
||||||
runes[i+1] = append([]rune{runes[i][len(runes[i])-1]}, runes[i+1]...)
|
|
||||||
runes[i] = runes[i][:len(runes[i])-1]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// filter all non letters and none digit
|
|
||||||
var filterRunes [][]rune
|
|
||||||
for _, r := range runes {
|
|
||||||
if len(r) == 0 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if unicode.IsLetter(r[0]) || unicode.IsDigit(r[0]) {
|
|
||||||
filterRunes = append(filterRunes, r)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
result := ""
|
|
||||||
|
|
||||||
// capitalize
|
|
||||||
for i, r := range filterRunes {
|
|
||||||
if i == 0 {
|
if i == 0 {
|
||||||
for j := range r {
|
builder.WriteString(strings.ToLower(str))
|
||||||
r[j] = unicode.ToLower(r[j])
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
for j := range r {
|
builder.WriteString(Capitalize(str))
|
||||||
if j == 0 {
|
|
||||||
r[0] = unicode.ToUpper(r[0])
|
|
||||||
} else {
|
|
||||||
r[j] = unicode.ToLower(r[j])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if len(r) > 0 {
|
|
||||||
result = result + string(r)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return result
|
return builder.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Capitalize converts the first character of a string to upper case and the remaining to lower case.
|
// Capitalize converts the first character of a string to upper case and the remaining to lower case.
|
||||||
|
|||||||
Reference in New Issue
Block a user