change name *Case to Case* for case functions of package gstr

This commit is contained in:
jianchenma
2021-01-08 16:24:08 +08:00
parent e141b8e098
commit 8365ce9d29
2 changed files with 89 additions and 34 deletions

View File

@ -1,4 +1,4 @@
// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved.
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
@ -6,15 +6,15 @@
//
// | Function | Result |
// |-----------------------------------|--------------------|
// | SnakeCase(s) | any_kind_of_string |
// | SnakeScreamingCase(s) | ANY_KIND_OF_STRING |
// | KebabCase(s) | any-kind-of-string |
// | KebabScreamingCase(s) | ANY-KIND-OF-STRING |
// | DelimitedCase(s, '.') | any.kind.of.string |
// | DelimitedScreamingCase(s, '.') | ANY.KIND.OF.STRING |
// | CamelCase(s) | AnyKindOfString |
// | CamelLowerCase(s) | anyKindOfString |
// | SnakeFirstUpperCase(RGBCodeMd5) | rgb_code_md5 |
// | CaseSnake(s) | any_kind_of_string |
// | CaseSnakeScreaming(s) | ANY_KIND_OF_STRING |
// | CaseSnakeFirstUpper("RGBCodeMd5") | rgb_code_md5 |
// | CaseKebab(s) | any-kind-of-string |
// | CaseKebabScreaming(s) | ANY-KIND-OF-STRING |
// | CaseDelimited(s, '.') | any.kind.of.string |
// | CaseDelimitedScreaming(s, '.') | ANY.KIND.OF.STRING |
// | CaseCamel(s) | AnyKindOfString |
// | CaseCamelLower(s) | anyKindOfString |
package gstr
@ -30,12 +30,24 @@ var (
)
// CamelCase converts a string to CamelCase.
// Deprecated, use CaseCamel instead.
func CamelCase(s string) string {
return CaseCamel(s)
}
// CaseCamel converts a string to CamelCase.
func CaseCamel(s string) string {
return toCamelInitCase(s, true)
}
// CamelLowerCase converts a string to lowerCamelCase.
// Deprecated, use CaseCamelLower instead.
func CamelLowerCase(s string) string {
return CaseCamelLower(s)
}
// CaseCamelLower converts a string to lowerCamelCase.
func CaseCamelLower(s string) string {
if s == "" {
return s
}
@ -46,19 +58,38 @@ func CamelLowerCase(s string) string {
}
// SnakeCase converts a string to snake_case.
// Deprecated, use CaseSnake instead.
func SnakeCase(s string) string {
return CaseSnake(s)
}
// CaseSnake converts a string to snake_case.
func CaseSnake(s string) string {
return DelimitedCase(s, '_')
}
// SnakeScreamingCase converts a string to SNAKE_CASE_SCREAMING.
// Deprecated, use CaseSnakeScreaming instead.
func SnakeScreamingCase(s string) string {
return CaseSnakeScreaming(s)
}
// CaseSnakeScreaming converts a string to SNAKE_CASE_SCREAMING.
func CaseSnakeScreaming(s string) string {
return DelimitedScreamingCase(s, '_', true)
}
// SnakeFirstUpperCase converts a string from RGBCodeMd5 to rgb_code_md5.
// The length of word should not be too long
// TODO for efficiency should change regexp to traversing string in future
// Deprecated, use CaseSnakeFirstUpper instead.
func SnakeFirstUpperCase(word string, underscore ...string) string {
return CaseSnakeFirstUpper(word, underscore...)
}
// CaseSnakeFirstUpper converts a string from RGBCodeMd5 to rgb_code_md5.
// The length of word should not be too long
// TODO for efficiency should change regexp to traversing string in future
func CaseSnakeFirstUpper(word string, underscore ...string) string {
replace := "_"
if len(underscore) > 0 {
replace = underscore[0]
@ -84,23 +115,47 @@ func SnakeFirstUpperCase(word string, underscore ...string) string {
return TrimLeft(word, replace)
}
// KebabCase converts a string to kebab-case
// KebabCase converts a string to kebab-case.
// Deprecated, use CaseKebab instead.
func KebabCase(s string) string {
return DelimitedCase(s, '-')
return CaseKebab(s)
}
// CaseKebab converts a string to kebab-case
func CaseKebab(s string) string {
return CaseDelimited(s, '-')
}
// KebabScreamingCase converts a string to KEBAB-CASE-SCREAMING.
// Deprecated, use CaseKebabScreaming instead.
func KebabScreamingCase(s string) string {
return DelimitedScreamingCase(s, '-', true)
return CaseKebabScreaming(s)
}
// CaseKebabScreaming converts a string to KEBAB-CASE-SCREAMING.
func CaseKebabScreaming(s string) string {
return CaseDelimitedScreaming(s, '-', true)
}
// DelimitedCase converts a string to snake.case.delimited.
// Deprecated, use CaseDelimited instead.
func DelimitedCase(s string, del uint8) string {
return DelimitedScreamingCase(s, del, false)
return CaseDelimited(s, del)
}
// CaseDelimited converts a string to snake.case.delimited.
func CaseDelimited(s string, del uint8) string {
return CaseDelimitedScreaming(s, del, false)
}
// DelimitedScreamingCase converts a string to DELIMITED.SCREAMING.CASE or delimited.screaming.case.
// Deprecated, use CaseDelimitedScreaming instead.
func DelimitedScreamingCase(s string, del uint8, screaming bool) string {
return CaseDelimitedScreaming(s, del, screaming)
}
// CaseDelimitedScreaming converts a string to DELIMITED.SCREAMING.CASE or delimited.screaming.case.
func CaseDelimitedScreaming(s string, del uint8, screaming bool) string {
s = addWordBoundariesToNumbers(s)
s = strings.Trim(s, " ")
n := ""

View File

@ -1,4 +1,4 @@
// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved.
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
@ -12,7 +12,7 @@ import (
"github.com/gogf/gf/text/gstr"
)
func Test_CamelCase(t *testing.T) {
func Test_CaseCamel(t *testing.T) {
cases := [][]string{
{"test_case", "TestCase"},
{"test", "Test"},
@ -28,14 +28,14 @@ func Test_CamelCase(t *testing.T) {
for _, i := range cases {
in := i[0]
out := i[1]
result := gstr.CamelCase(in)
result := gstr.CaseCamel(in)
if result != out {
t.Error("'" + result + "' != '" + out + "'")
}
}
}
func Test_CamelLowerCase(t *testing.T) {
func Test_CaseCamelLower(t *testing.T) {
cases := [][]string{
{"foo-bar", "fooBar"},
{"TestCase", "testCase"},
@ -45,14 +45,14 @@ func Test_CamelLowerCase(t *testing.T) {
for _, i := range cases {
in := i[0]
out := i[1]
result := gstr.CamelLowerCase(in)
result := gstr.CaseCamelLower(in)
if result != out {
t.Error("'" + result + "' != '" + out + "'")
}
}
}
func Test_SnakeCase(t *testing.T) {
func Test_CaseSnake(t *testing.T) {
cases := [][]string{
{"testCase", "test_case"},
{"TestCase", "test_case"},
@ -75,14 +75,14 @@ func Test_SnakeCase(t *testing.T) {
for _, i := range cases {
in := i[0]
out := i[1]
result := gstr.SnakeCase(in)
result := gstr.CaseSnake(in)
if result != out {
t.Error("'" + in + "'('" + result + "' != '" + out + "')")
}
}
}
func Test_DelimitedCase(t *testing.T) {
func Test_CaseDelimited(t *testing.T) {
cases := [][]string{
{"testCase", "test@case"},
{"TestCase", "test@case"},
@ -106,28 +106,28 @@ func Test_DelimitedCase(t *testing.T) {
for _, i := range cases {
in := i[0]
out := i[1]
result := gstr.DelimitedCase(in, '@')
result := gstr.CaseDelimited(in, '@')
if result != out {
t.Error("'" + in + "' ('" + result + "' != '" + out + "')")
}
}
}
func Test_SnakeScreamingCase(t *testing.T) {
func Test_CaseSnakeScreaming(t *testing.T) {
cases := [][]string{
{"testCase", "TEST_CASE"},
}
for _, i := range cases {
in := i[0]
out := i[1]
result := gstr.SnakeScreamingCase(in)
result := gstr.CaseSnakeScreaming(in)
if result != out {
t.Error("'" + result + "' != '" + out + "'")
}
}
}
func Test_KebabCase(t *testing.T) {
func Test_CaseKebab(t *testing.T) {
cases := [][]string{
{"testCase", "test-case"},
{"optimization1.0.0", "optimization-1-0-0"},
@ -135,42 +135,42 @@ func Test_KebabCase(t *testing.T) {
for _, i := range cases {
in := i[0]
out := i[1]
result := gstr.KebabCase(in)
result := gstr.CaseKebab(in)
if result != out {
t.Error("'" + result + "' != '" + out + "'")
}
}
}
func Test_KebabScreamingCase(t *testing.T) {
func Test_CaseKebabScreaming(t *testing.T) {
cases := [][]string{
{"testCase", "TEST-CASE"},
}
for _, i := range cases {
in := i[0]
out := i[1]
result := gstr.KebabScreamingCase(in)
result := gstr.CaseKebabScreaming(in)
if result != out {
t.Error("'" + result + "' != '" + out + "'")
}
}
}
func Test_DelimitedScreamingCase(t *testing.T) {
func Test_CaseDelimitedScreaming(t *testing.T) {
cases := [][]string{
{"testCase", "TEST.CASE"},
}
for _, i := range cases {
in := i[0]
out := i[1]
result := gstr.DelimitedScreamingCase(in, '.', true)
result := gstr.CaseDelimitedScreaming(in, '.', true)
if result != out {
t.Error("'" + result + "' != '" + out + "'")
}
}
}
func TestSnakeFirstUpperCase(t *testing.T) {
func Test_CaseSnakeFirstUpper(t *testing.T) {
cases := [][]string{
{"RGBCodeMd5", "rgb_code_md5"},
{"testCase", "test_case"},
@ -186,7 +186,7 @@ func TestSnakeFirstUpperCase(t *testing.T) {
for _, i := range cases {
in := i[0]
out := i[1]
result := gstr.SnakeFirstUpperCase(in)
result := gstr.CaseSnakeFirstUpper(in)
if result != out {
t.Error("'" + result + "' != '" + out + "'")
}