-
Notifications
You must be signed in to change notification settings - Fork 1
/
template.go
46 lines (39 loc) · 1.15 KB
/
template.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
43
44
45
46
package pkg
import (
"github.com/google/uuid"
"regexp"
"strings"
"text/template"
)
// var matchFirstCap = regexp.MustCompile("(.)([A-Z][a-z]+)")
// var matchAllCap = regexp.MustCompile("([a-z0-9])([A-Z])")
// var removeSpace = regexp.MustCompile("( )")
func KebabCase(str string) string {
// snake := matchFirstCap.ReplaceAllString(str, "${1}-${2}")
// snake = matchAllCap.ReplaceAllString(snake, "${1}-${2}")
// snake = removeSpace.ReplaceAllString(snake, "${2}")
re := regexp.MustCompile(`[^a-zA-Z0-9]+`)
result := re.ReplaceAllString(str, "-")
return strings.ToLower(result)
}
// Define a custom function to capitalize the first letter of a string.
func StartCase(str string) string {
if len(str) == 0 {
return ""
}
return strings.ToUpper(str[0:1]) + strings.ToLower(str[1:])
}
func AppId(length int) string {
id := uuid.New()
val := id.String()
return strings.ReplaceAll(strings.ToUpper(val), "-", "")[0:length]
}
func NewTemplate() *template.Template {
tmpl := template.New("configGenerator").Funcs(template.FuncMap{
"capitalize": StartCase,
"appId": AppId,
"kebabCase": KebabCase,
"toLower": strings.ToLower,
})
return tmpl
}