-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake.go
42 lines (37 loc) · 916 Bytes
/
snake.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package stringutils
import (
"strings"
"unicode"
)
// SnakeCase convert camelCase to snake_case
func SnakeCase(value string) string {
output := ""
// trim all spaces
input := strings.TrimSpace(value)
for index, r := range input {
nextIsUpper := false
nextIsNumber := false
if index+1 < len(input) {
nextIsUpper = unicode.IsUpper(rune(input[index+1]))
nextIsNumber = unicode.IsNumber(rune(input[index+1]))
}
// treat acronym is a word, e.g: JSONData -> json_data
if unicode.IsUpper(r) && !nextIsUpper && index > 0 {
output += "_" + string(r)
} else if r == ' ' || r == '-' {
// Prevent add double "_"
if !nextIsUpper {
output += "_"
}
} else {
output += string(r)
if unicode.IsLetter(r) && nextIsNumber {
output += "_"
}
if unicode.IsNumber(r) && !nextIsNumber && index != len(input)-1 {
output += "_"
}
}
}
return strings.ToLower(output)
}