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, '_')
|
||||
}
|
||||
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
|
||||
func ToScreamingSnake(s string) string {
|
||||
return ToScreamingDelimited(s, '_',0, true)
|
||||
return ToScreamingDelimited(s, '_', 0, true)
|
||||
}
|
||||
|
||||
// ToKebab converts a string to kebab-case
|
||||
@ -50,16 +51,20 @@ func ToKebab(s string) string {
|
||||
|
||||
// ToScreamingKebab converts a string to SCREAMING-KEBAB-CASE
|
||||
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 = '.'`)
|
||||
func ToDelimited(s string, del uint8) string {
|
||||
return ToScreamingDelimited(s, del, 0,false)
|
||||
// ToDelimited converts a string to delimited.snake.case
|
||||
// (in this case `delimiter = '.'`)
|
||||
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`)
|
||||
func ToScreamingDelimited(s string, del uint8,ingore uint8 ,screaming bool) string {
|
||||
// ToScreamingDelimited converts a string to SCREAMING.DELIMITED.SNAKE.CASE
|
||||
// (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 = strings.Trim(s, " ")
|
||||
n := ""
|
||||
@ -68,24 +73,28 @@ func ToScreamingDelimited(s string, del uint8,ingore uint8 ,screaming bool) stri
|
||||
nextCaseIsChanged := false
|
||||
if i+1 < len(s) {
|
||||
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
|
||||
}
|
||||
if(ingore >0 && i-1>=0 && s[i-1]==ingore &&nextCaseIsChanged ){
|
||||
nextCaseIsChanged=false
|
||||
if ignore > 0 && i-1 >= 0 && s[i-1] == ignore && nextCaseIsChanged {
|
||||
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
|
||||
if v >= 'A' && v <= 'Z' {
|
||||
n += string(del) + string(v)
|
||||
n += string(delimiter) + string(v)
|
||||
} else if v >= 'a' && v <= 'z' {
|
||||
n += string(v) + string(del)
|
||||
n += string(v) + string(delimiter)
|
||||
}
|
||||
} else if v == ' ' || v == '_' || v == '-' {
|
||||
// replace spaces/underscores with delimiters
|
||||
n += string(del)
|
||||
n += string(delimiter)
|
||||
} else {
|
||||
n = n + string(v)
|
||||
}
|
||||
|
@ -75,18 +75,18 @@ func TestToSnakeWithIgnore(t *testing.T) {
|
||||
{"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","@"},
|
||||
{"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]
|
||||
var ignore uint8
|
||||
if len(i) == 3 {
|
||||
ignore = i[2][0]
|
||||
}
|
||||
result := ToSnakeWithIgnore(in,ingore)
|
||||
result := ToSnakeWithIgnore(in, ignore)
|
||||
if result != out {
|
||||
t.Error("'" + in + "'('" + result + "' != '" + out + "')")
|
||||
}
|
||||
@ -173,7 +173,7 @@ func TestToScreamingDelimited(t *testing.T) {
|
||||
for _, i := range cases {
|
||||
in := i[0]
|
||||
out := i[1]
|
||||
result := ToScreamingDelimited(in, '.',0, true)
|
||||
result := ToScreamingDelimited(in, '.', 0, true)
|
||||
if result != out {
|
||||
t.Error("'" + result + "' != '" + out + "'")
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user