Add logic to convert camel case to snake case

This commit is contained in:
BenTrapani 2016-07-29 15:28:51 -04:00
parent ff3de3c0dd
commit 2c325e3c76

18
utils.go Normal file
View File

@ -0,0 +1,18 @@
package main
import "unicode"
func ToSnake(in string) string {
runes := []rune(in)
length := len(runes)
var out []rune
for i := 0; i < length; i++ {
if i > 0 && unicode.IsUpper(runes[i]) && ((i+1 < length && unicode.IsLower(runes[i+1])) || unicode.IsLower(runes[i-1])) {
out = append(out, '_')
}
out = append(out, unicode.ToLower(runes[i]))
}
return string(out)
}