Remove dependency on strcase
This commit is contained in:
parent
73d7db2078
commit
8f6d809f73
2
go.mod
2
go.mod
|
@ -1,5 +1,3 @@
|
||||||
module go.fifitido.net/cmd
|
module go.fifitido.net/cmd
|
||||||
|
|
||||||
go 1.21.3
|
go 1.21.3
|
||||||
|
|
||||||
require github.com/iancoleman/strcase v0.3.0
|
|
||||||
|
|
2
go.sum
2
go.sum
|
@ -1,2 +0,0 @@
|
||||||
github.com/iancoleman/strcase v0.3.0 h1:nTXanmYxhfFAMjZL34Ov6gkzEsSJZ5DbhxWjvSASxEI=
|
|
||||||
github.com/iancoleman/strcase v0.3.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho=
|
|
40
tpl_funcs.go
40
tpl_funcs.go
|
@ -2,10 +2,9 @@ package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"text/template"
|
"text/template"
|
||||||
|
|
||||||
"github.com/iancoleman/strcase"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var tplFuncs = template.FuncMap{
|
var tplFuncs = template.FuncMap{
|
||||||
|
@ -22,7 +21,6 @@ var tplFuncs = template.FuncMap{
|
||||||
"sub": tplSub,
|
"sub": tplSub,
|
||||||
"dec": tplDec,
|
"dec": tplDec,
|
||||||
"mult": tplMult,
|
"mult": tplMult,
|
||||||
"pascal": tplPascal,
|
|
||||||
"camel": tplCamel,
|
"camel": tplCamel,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -51,7 +49,19 @@ func tplJoin(strs []string, sep string) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func tplUnder(s string) string {
|
func tplUnder(s string) string {
|
||||||
return strcase.ToSnake(s)
|
// 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
|
||||||
}
|
}
|
||||||
|
|
||||||
func tplVarPrefix(s string) string {
|
func tplVarPrefix(s string) string {
|
||||||
|
@ -90,10 +100,24 @@ func tplMult(a, b int) int {
|
||||||
return a * b
|
return a * b
|
||||||
}
|
}
|
||||||
|
|
||||||
func tplPascal(s string) string {
|
func tplCamel(s string) string {
|
||||||
return strcase.ToCamel(s)
|
// 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)
|
||||||
}
|
}
|
||||||
|
|
||||||
func tplCamel(s string) string {
|
// Join words
|
||||||
return strcase.ToLowerCamel(s)
|
s = strings.Join(words, "")
|
||||||
|
|
||||||
|
return s
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue