Remove dependency on strcase

This commit is contained in:
Evan Fiordeliso 2023-11-16 23:21:23 -05:00
parent 73d7db2078
commit 8f6d809f73
3 changed files with 33 additions and 13 deletions

2
go.mod
View File

@ -1,5 +1,3 @@
module go.fifitido.net/cmd
go 1.21.3
require github.com/iancoleman/strcase v0.3.0

2
go.sum
View File

@ -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=

View File

@ -2,10 +2,9 @@ package cmd
import (
"fmt"
"regexp"
"strings"
"text/template"
"github.com/iancoleman/strcase"
)
var tplFuncs = template.FuncMap{
@ -22,7 +21,6 @@ var tplFuncs = template.FuncMap{
"sub": tplSub,
"dec": tplDec,
"mult": tplMult,
"pascal": tplPascal,
"camel": tplCamel,
}
@ -51,7 +49,19 @@ func tplJoin(strs []string, sep 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 {
@ -90,10 +100,24 @@ func tplMult(a, b int) int {
return a * b
}
func tplPascal(s string) string {
return strcase.ToCamel(s)
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)
}
func tplCamel(s string) string {
return strcase.ToLowerCamel(s)
// Join words
s = strings.Join(words, "")
return s
}