strcase/snake.go
bestplay d8275f2535
Update snake.go
Consider userID to user_id
2018-04-27 15:32:37 +08:00

33 lines
729 B
Go

// Package strcase converts strings to snake_case or CamelCase
package strcase
import (
"strings"
)
// Converts a string to snake_case
func ToSnake(s string) string {
s = addWordBoundariesToNumbers(s)
s = strings.Trim(s, " ")
n := ""
for i, v := range s {
// treat acronyms as words, eg for JSONData -> JSON is a whole word
preIsCapital := false
if i - 1 > 0 {
w := s[i-1]
preIsCapital = w >= 'A' && w <= 'Z'
}
if i > 0 && v >= 'A' && v <= 'Z' && n[len(n)-1] != '_' && !preIsCapital {
// add underscore if next letter is a capital
n += "_" + string(v)
} else if v == ' ' {
// replace spaces with underscores
n += "_"
} else {
n = n + string(v)
}
}
n = strings.ToLower(n)
return n
}