add method ToSnakeWithIgnore
Change-Id: Id75f3d34d1ffc13a53351cdec79b2130cb8ffa66
This commit is contained in:
parent
e506e3ef73
commit
44067336ae
15
snake.go
15
snake.go
@ -31,12 +31,16 @@ import (
|
||||
|
||||
// ToSnake converts a string to snake_case
|
||||
func ToSnake(s string) string {
|
||||
|
||||
return ToDelimited(s, '_')
|
||||
}
|
||||
func ToSnakeWithIgnore(s string,ingore uint8) string {
|
||||
|
||||
return ToScreamingDelimited(s, '_',ingore,false)
|
||||
}
|
||||
// ToScreamingSnake converts a string to SCREAMING_SNAKE_CASE
|
||||
func ToScreamingSnake(s string) string {
|
||||
return ToScreamingDelimited(s, '_', true)
|
||||
return ToScreamingDelimited(s, '_',0, true)
|
||||
}
|
||||
|
||||
// ToKebab converts a string to kebab-case
|
||||
@ -46,16 +50,16 @@ func ToKebab(s string) string {
|
||||
|
||||
// ToScreamingKebab converts a string to SCREAMING-KEBAB-CASE
|
||||
func ToScreamingKebab(s string) string {
|
||||
return ToScreamingDelimited(s, '-', true)
|
||||
return ToScreamingDelimited(s, '-', 0,true)
|
||||
}
|
||||
|
||||
// ToDelimited converts a string to delimited.snake.case (in this case `del = '.'`)
|
||||
func ToDelimited(s string, del uint8) string {
|
||||
return ToScreamingDelimited(s, del, false)
|
||||
return ToScreamingDelimited(s, del, 0,false)
|
||||
}
|
||||
|
||||
// ToScreamingDelimited converts a string to SCREAMING.DELIMITED.SNAKE.CASE (in this case `del = '.'; screaming = true`) or delimited.snake.case (in this case `del = '.'; screaming = false`)
|
||||
func ToScreamingDelimited(s string, del uint8, screaming bool) string {
|
||||
func ToScreamingDelimited(s string, del uint8,ingore uint8 ,screaming bool) string {
|
||||
s = addWordBoundariesToNumbers(s)
|
||||
s = strings.Trim(s, " ")
|
||||
n := ""
|
||||
@ -67,6 +71,9 @@ func ToScreamingDelimited(s string, del uint8, screaming bool) string {
|
||||
if (v >= 'A' && v <= 'Z' && next >= 'a' && next <= 'z') || (v >= 'a' && v <= 'z' && next >= 'A' && next <= 'Z') {
|
||||
nextCaseIsChanged = true
|
||||
}
|
||||
if(ingore >0 && i-1>=0 && s[i-1]==ingore &&nextCaseIsChanged ){
|
||||
nextCaseIsChanged=false
|
||||
}
|
||||
}
|
||||
|
||||
if i > 0 && n[len(n)-1] != del && nextCaseIsChanged {
|
||||
|
@ -58,6 +58,40 @@ func TestToSnake(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
func TestToSnakeWithIgnore(t *testing.T) {
|
||||
cases := [][]string{
|
||||
{"testCase", "test_case"},
|
||||
{"TestCase", "test_case"},
|
||||
{"Test Case", "test_case"},
|
||||
{" Test Case", "test_case"},
|
||||
{"Test Case ", "test_case"},
|
||||
{" Test Case ", "test_case"},
|
||||
{"test", "test"},
|
||||
{"test_case", "test_case"},
|
||||
{"Test", "test"},
|
||||
{"", ""},
|
||||
{"ManyManyWords", "many_many_words"},
|
||||
{"manyManyWords", "many_many_words"},
|
||||
{"AnyKind of_string", "any_kind_of_string"},
|
||||
{"numbers2and55with000", "numbers_2_and_55_with_000"},
|
||||
{"JSONData", "json_data"},
|
||||
{"AwesomeAcitvity.UserID", "awesome_acitvity.user_id","."},
|
||||
{"AwesomeAcitvity.User.Id", "awesome_acitvity.user.id","."},
|
||||
{"AwesomeUsername@Awesome.Com", "awesome_username@awesome._com","@"},
|
||||
}
|
||||
for _, i := range cases {
|
||||
in := i[0]
|
||||
out := i[1]
|
||||
var ingore uint8
|
||||
if len(i)==3{
|
||||
ingore=i[2][0]
|
||||
}
|
||||
result := ToSnakeWithIgnore(in,ingore)
|
||||
if result != out {
|
||||
t.Error("'" + in + "'('" + result + "' != '" + out + "')")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestToDelimited(t *testing.T) {
|
||||
cases := [][]string{
|
||||
@ -139,7 +173,7 @@ func TestToScreamingDelimited(t *testing.T) {
|
||||
for _, i := range cases {
|
||||
in := i[0]
|
||||
out := i[1]
|
||||
result := ToScreamingDelimited(in, '.', true)
|
||||
result := ToScreamingDelimited(in, '.',0, true)
|
||||
if result != out {
|
||||
t.Error("'" + result + "' != '" + out + "'")
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user