-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.go
41 lines (34 loc) · 946 Bytes
/
utils.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
package gontentful
import (
"fmt"
"regexp"
"strings"
)
func fmtLocale(code string) string {
return strings.ToLower(code)
}
func fmtTableName(contentType string, locale string) string {
return fmt.Sprintf("%s$%s", strings.ToLower(contentType), fmtLocale(locale))
}
func fmtTablePublishName(contentType string, locale string) string {
return fmt.Sprintf("%s$%s$publish", strings.ToLower(contentType), fmtLocale(locale))
}
var camel = regexp.MustCompile("(^[^A-Z]*|[A-Z]*)([A-Z][^A-Z]+|$)")
var snake = regexp.MustCompile(`([_ ]\w)`)
func toSnakeCase(s string) string {
var a []string
for _, sub := range camel.FindAllStringSubmatch(s, -1) {
if sub[1] != "" {
a = append(a, sub[1])
}
if sub[2] != "" {
a = append(a, sub[2])
}
}
return strings.ToLower(strings.Join(a, "_"))
}
func toCamelCase(s string) string {
return snake.ReplaceAllStringFunc(s, func(w string) string {
return strings.ToUpper(string(w[1]))
})
}