Tidy up snake.go
Comment width reduced If condition reworded for readability Typo ingore/ignore Rename del to delimiter Run gofmt
This commit is contained in:
parent
198ca931c7
commit
13740a6cff
41
snake.go
41
snake.go
@ -34,13 +34,14 @@ func ToSnake(s string) string {
|
|||||||
|
|
||||||
return ToDelimited(s, '_')
|
return ToDelimited(s, '_')
|
||||||
}
|
}
|
||||||
func ToSnakeWithIgnore(s string,ingore uint8) string {
|
func ToSnakeWithIgnore(s string, ignore uint8) string {
|
||||||
|
|
||||||
return ToScreamingDelimited(s, '_',ingore,false)
|
return ToScreamingDelimited(s, '_', ignore, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ToScreamingSnake converts a string to SCREAMING_SNAKE_CASE
|
// ToScreamingSnake converts a string to SCREAMING_SNAKE_CASE
|
||||||
func ToScreamingSnake(s string) string {
|
func ToScreamingSnake(s string) string {
|
||||||
return ToScreamingDelimited(s, '_',0, true)
|
return ToScreamingDelimited(s, '_', 0, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ToKebab converts a string to kebab-case
|
// ToKebab converts a string to kebab-case
|
||||||
@ -50,16 +51,20 @@ func ToKebab(s string) string {
|
|||||||
|
|
||||||
// ToScreamingKebab converts a string to SCREAMING-KEBAB-CASE
|
// ToScreamingKebab converts a string to SCREAMING-KEBAB-CASE
|
||||||
func ToScreamingKebab(s string) string {
|
func ToScreamingKebab(s string) string {
|
||||||
return ToScreamingDelimited(s, '-', 0,true)
|
return ToScreamingDelimited(s, '-', 0, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ToDelimited converts a string to delimited.snake.case (in this case `del = '.'`)
|
// ToDelimited converts a string to delimited.snake.case
|
||||||
func ToDelimited(s string, del uint8) string {
|
// (in this case `delimiter = '.'`)
|
||||||
return ToScreamingDelimited(s, del, 0,false)
|
func ToDelimited(s string, delimiter uint8) string {
|
||||||
|
return ToScreamingDelimited(s, delimiter, 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`)
|
// ToScreamingDelimited converts a string to SCREAMING.DELIMITED.SNAKE.CASE
|
||||||
func ToScreamingDelimited(s string, del uint8,ingore uint8 ,screaming bool) string {
|
// (in this case `delimiter = '.'; screaming = true`)
|
||||||
|
// or delimited.snake.case
|
||||||
|
// (in this case `delimiter = '.'; screaming = false`)
|
||||||
|
func ToScreamingDelimited(s string, delimiter uint8, ignore uint8, screaming bool) string {
|
||||||
s = addWordBoundariesToNumbers(s)
|
s = addWordBoundariesToNumbers(s)
|
||||||
s = strings.Trim(s, " ")
|
s = strings.Trim(s, " ")
|
||||||
n := ""
|
n := ""
|
||||||
@ -68,24 +73,28 @@ func ToScreamingDelimited(s string, del uint8,ingore uint8 ,screaming bool) stri
|
|||||||
nextCaseIsChanged := false
|
nextCaseIsChanged := false
|
||||||
if i+1 < len(s) {
|
if i+1 < len(s) {
|
||||||
next := s[i+1]
|
next := s[i+1]
|
||||||
if (v >= 'A' && v <= 'Z' && next >= 'a' && next <= 'z') || (v >= 'a' && v <= 'z' && next >= 'A' && next <= 'Z') {
|
vIsCap := v >= 'A' && v <= 'Z'
|
||||||
|
vIsLow := v >= 'a' && v <= 'z'
|
||||||
|
nextIsCap := next >= 'A' && next <= 'Z'
|
||||||
|
nextIsLow := next >= 'a' && next <= 'z'
|
||||||
|
if (vIsCap && nextIsLow) || (vIsLow && nextIsCap) {
|
||||||
nextCaseIsChanged = true
|
nextCaseIsChanged = true
|
||||||
}
|
}
|
||||||
if(ingore >0 && i-1>=0 && s[i-1]==ingore &&nextCaseIsChanged ){
|
if ignore > 0 && i-1 >= 0 && s[i-1] == ignore && nextCaseIsChanged {
|
||||||
nextCaseIsChanged=false
|
nextCaseIsChanged = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if i > 0 && n[len(n)-1] != del && nextCaseIsChanged {
|
if i > 0 && n[len(n)-1] != delimiter && nextCaseIsChanged {
|
||||||
// add underscore if next letter case type is changed
|
// add underscore if next letter case type is changed
|
||||||
if v >= 'A' && v <= 'Z' {
|
if v >= 'A' && v <= 'Z' {
|
||||||
n += string(del) + string(v)
|
n += string(delimiter) + string(v)
|
||||||
} else if v >= 'a' && v <= 'z' {
|
} else if v >= 'a' && v <= 'z' {
|
||||||
n += string(v) + string(del)
|
n += string(v) + string(delimiter)
|
||||||
}
|
}
|
||||||
} else if v == ' ' || v == '_' || v == '-' {
|
} else if v == ' ' || v == '_' || v == '-' {
|
||||||
// replace spaces/underscores with delimiters
|
// replace spaces/underscores with delimiters
|
||||||
n += string(del)
|
n += string(delimiter)
|
||||||
} else {
|
} else {
|
||||||
n = n + string(v)
|
n = n + string(v)
|
||||||
}
|
}
|
||||||
|
@ -75,18 +75,18 @@ func TestToSnakeWithIgnore(t *testing.T) {
|
|||||||
{"AnyKind of_string", "any_kind_of_string"},
|
{"AnyKind of_string", "any_kind_of_string"},
|
||||||
{"numbers2and55with000", "numbers_2_and_55_with_000"},
|
{"numbers2and55with000", "numbers_2_and_55_with_000"},
|
||||||
{"JSONData", "json_data"},
|
{"JSONData", "json_data"},
|
||||||
{"AwesomeAcitvity.UserID", "awesome_acitvity.user_id","."},
|
{"AwesomeAcitvity.UserID", "awesome_acitvity.user_id", "."},
|
||||||
{"AwesomeAcitvity.User.Id", "awesome_acitvity.user.id","."},
|
{"AwesomeAcitvity.User.Id", "awesome_acitvity.user.id", "."},
|
||||||
{"AwesomeUsername@Awesome.Com", "awesome_username@awesome._com","@"},
|
{"AwesomeUsername@Awesome.Com", "awesome_username@awesome._com", "@"},
|
||||||
}
|
}
|
||||||
for _, i := range cases {
|
for _, i := range cases {
|
||||||
in := i[0]
|
in := i[0]
|
||||||
out := i[1]
|
out := i[1]
|
||||||
var ingore uint8
|
var ignore uint8
|
||||||
if len(i)==3{
|
if len(i) == 3 {
|
||||||
ingore=i[2][0]
|
ignore = i[2][0]
|
||||||
}
|
}
|
||||||
result := ToSnakeWithIgnore(in,ingore)
|
result := ToSnakeWithIgnore(in, ignore)
|
||||||
if result != out {
|
if result != out {
|
||||||
t.Error("'" + in + "'('" + result + "' != '" + out + "')")
|
t.Error("'" + in + "'('" + result + "' != '" + out + "')")
|
||||||
}
|
}
|
||||||
@ -173,7 +173,7 @@ func TestToScreamingDelimited(t *testing.T) {
|
|||||||
for _, i := range cases {
|
for _, i := range cases {
|
||||||
in := i[0]
|
in := i[0]
|
||||||
out := i[1]
|
out := i[1]
|
||||||
result := ToScreamingDelimited(in, '.',0, true)
|
result := ToScreamingDelimited(in, '.', 0, true)
|
||||||
if result != out {
|
if result != out {
|
||||||
t.Error("'" + result + "' != '" + out + "'")
|
t.Error("'" + result + "' != '" + out + "'")
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user