1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-08 06:32:28 +08:00

feat: fix and add KebabCase/UpperKebabCase

This commit is contained in:
dudaodong
2022-12-14 21:09:22 +08:00
parent d0576e028f
commit 5b3d48a1e7
3 changed files with 142 additions and 26 deletions

View File

@@ -157,24 +157,19 @@ func PadStart(source string, size int, padStr string) string {
}
// KebabCase covert string to kebab-case
// non letters and numbers will be ignored
// eg. "Foo-#1😄$_%^&*(1bar" => "foo-1-1-bar"
func KebabCase(s string) string {
re := regexp.MustCompile(`[\W|_]+`)
space := " "
match := re.ReplaceAllString(s, space)
rs := strings.Split(match, space)
strs := splitIntoStrings(s, false)
return strings.Join(strs, "-")
}
var result []string
for _, v := range rs {
splitWords := splitWordsToLower(v)
if len(splitWords) > 0 {
result = append(result, splitWords...)
}
// if v != "" {
// result = append(result, strings.ToLower(v))
// }
}
return strings.Join(result, "-")
// UpperKebabCase covert string to upper KEBAB-CASE
// non letters and numbers will be ignored
// eg. "Foo-#1😄$_%^&*(1bar" => "FOO-1-1-BAR"
func UpperKebabCase(s string) string {
strs := splitIntoStrings(s, true)
return strings.Join(strs, "-")
}
// SnakeCase covert string to snake_case