File tree Expand file tree Collapse file tree 1 file changed +27
-1
lines changed
Expand file tree Collapse file tree 1 file changed +27
-1
lines changed Original file line number Diff line number Diff line change 11package helpers
22
3- import "strconv"
3+ import (
4+ "strconv"
5+ "strings"
6+ "unicode"
7+ )
48
59// TextFromNumber Returns the text based form of a number.
610// e.g. 1 would return One
@@ -77,3 +81,25 @@ func Round(val float64) int {
7781 }
7882 return int (val + 0.5 )
7983}
84+
85+ func KebabCase (in string ) string {
86+ out := SnakeCase (in )
87+ out = strings .Replace (out , "_" , "-" , - 1 )
88+ return out
89+ }
90+
91+ func SnakeCase (in string ) string {
92+ in = strings .Replace (in , " " , "_" , - 1 )
93+ runes := []rune (in )
94+ length := len (runes )
95+
96+ var out []rune
97+ for i := 0 ; i < length ; i ++ {
98+ if i > 0 && unicode .IsUpper (runes [i ]) && ((i + 1 < length && unicode .IsLower (runes [i + 1 ])) || unicode .IsLower (runes [i - 1 ])) {
99+ out = append (out , '_' )
100+ }
101+ out = append (out , unicode .ToLower (runes [i ]))
102+ }
103+
104+ return strings .Replace (string (out ), "__" , "_" , - 1 )
105+ }
You can’t perform that action at this time.
0 commit comments