Compare commits
6 Commits
Author | SHA1 | Date |
---|---|---|
|
160b3321ef | |
|
1fbc94aa49 | |
|
c5bd7662f2 | |
|
81a1f6d97c | |
|
349cdf01d6 | |
|
9e5d7e2519 |
2
go.mod
2
go.mod
|
@ -1,3 +1,5 @@
|
|||
module go.fifitido.net/cmd
|
||||
|
||||
go 1.21.3
|
||||
|
||||
require github.com/iancoleman/strcase v0.3.0
|
||||
|
|
2
go.sum
2
go.sum
|
@ -0,0 +1,2 @@
|
|||
github.com/iancoleman/strcase v0.3.0 h1:nTXanmYxhfFAMjZL34Ov6gkzEsSJZ5DbhxWjvSASxEI=
|
||||
github.com/iancoleman/strcase v0.3.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho=
|
44
tpl_funcs.go
44
tpl_funcs.go
|
@ -2,9 +2,10 @@ package cmd
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
"text/template"
|
||||
|
||||
"github.com/iancoleman/strcase"
|
||||
)
|
||||
|
||||
var tplFuncs = template.FuncMap{
|
||||
|
@ -21,6 +22,7 @@ var tplFuncs = template.FuncMap{
|
|||
"sub": tplSub,
|
||||
"dec": tplDec,
|
||||
"mult": tplMult,
|
||||
"pascal": tplPascal,
|
||||
"camel": tplCamel,
|
||||
}
|
||||
|
||||
|
@ -49,19 +51,7 @@ func tplJoin(strs []string, sep string) string {
|
|||
}
|
||||
|
||||
func tplUnder(s string) string {
|
||||
// Remove all characters that are not alphanumeric or spaces or underscores
|
||||
s = regexp.MustCompile("[^a-zA-Z0-9_ -]+").ReplaceAllString(s, "")
|
||||
|
||||
// Replace all spaces with underscores
|
||||
s = strings.ReplaceAll(s, " ", "_")
|
||||
|
||||
// Replace all dashes with underscores
|
||||
s = strings.ReplaceAll(s, "-", "_")
|
||||
|
||||
// Convert to lowercase
|
||||
s = strings.ToLower(s)
|
||||
|
||||
return s
|
||||
return strcase.ToSnake(s)
|
||||
}
|
||||
|
||||
func tplVarPrefix(s string) string {
|
||||
|
@ -100,24 +90,10 @@ func tplMult(a, b int) int {
|
|||
return a * b
|
||||
}
|
||||
|
||||
func tplCamel(s string) string {
|
||||
// Remove all characters that are not alphanumeric or spaces or underscores
|
||||
s = regexp.MustCompile("[^a-zA-Z0-9_ -]+").ReplaceAllString(s, "")
|
||||
|
||||
// Replace all underscores with spaces
|
||||
s = strings.ReplaceAll(s, "_", " ")
|
||||
|
||||
// Replace all dashes with spaces
|
||||
s = strings.ReplaceAll(s, "-", " ")
|
||||
|
||||
// Title case s
|
||||
words := strings.Split(s, " ")
|
||||
for i, word := range words {
|
||||
words[i] = strings.ToTitle(word)
|
||||
}
|
||||
|
||||
// Join words
|
||||
s = strings.Join(words, "")
|
||||
|
||||
return s
|
||||
func tplPascal(s string) string {
|
||||
return strcase.ToCamel(s)
|
||||
}
|
||||
|
||||
func tplCamel(s string) string {
|
||||
return strcase.ToLowerCamel(s)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue