Skip to content

Commit c100f4f

Browse files
author
Jaybeecave
committed
Added Kebab Case
1 parent 0d9e788 commit c100f4f

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

helpers.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package 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+
}

0 commit comments

Comments
 (0)