File size: 633 Bytes
6380833
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
43
44
package strcase

import (
	"strings"
	"unicode"
)

func SnakeToCamel(snake string) string {
	isToUpper := false
	camel := ""

	for i, ch := range snake {
		if ch == '_' {
			isToUpper = true
		} else {
			if isToUpper && i > 0 {
				camel += string(unicode.ToUpper(ch))
				isToUpper = false
			} else {
				camel += string(ch)
			}
		}
	}

	return camel
}

func CamelToSnake(camel string) string {
	var snake strings.Builder

	for i, ch := range camel {
		if unicode.IsUpper(ch) {
			if i > 0 {
				snake.WriteRune('_')
			}
			snake.WriteRune(unicode.ToLower(ch))
		} else {
			snake.WriteRune(ch)
		}
	}

	return snake.String()
}