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:
Ian Coleman 2019-11-13 09:52:45 +11:00
parent 198ca931c7
commit 13740a6cff
2 changed files with 33 additions and 24 deletions

View File

@ -34,10 +34,11 @@ 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)
@ -53,13 +54,17 @@ 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)
} }

View File

@ -82,11 +82,11 @@ func TestToSnakeWithIgnore(t *testing.T) {
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 + "')")
} }