From 2c325e3c761380fffd02be8b0d6fa83ff7c63b8a Mon Sep 17 00:00:00 2001 From: BenTrapani Date: Fri, 29 Jul 2016 15:28:51 -0400 Subject: [PATCH] Add logic to convert camel case to snake case --- utils.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 utils.go diff --git a/utils.go b/utils.go new file mode 100644 index 0000000..c5db63c --- /dev/null +++ b/utils.go @@ -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) +}