From 8f6d809f730f5ec4e24a67c0a3aa061fb98b35f2 Mon Sep 17 00:00:00 2001 From: Evan Fiordeliso Date: Thu, 16 Nov 2023 23:21:23 -0500 Subject: [PATCH] Remove dependency on strcase --- go.mod | 2 -- go.sum | 2 -- tpl_funcs.go | 42 +++++++++++++++++++++++++++++++++--------- 3 files changed, 33 insertions(+), 13 deletions(-) diff --git a/go.mod b/go.mod index 3aac729..f6fed3c 100644 --- a/go.mod +++ b/go.mod @@ -1,5 +1,3 @@ module go.fifitido.net/cmd go 1.21.3 - -require github.com/iancoleman/strcase v0.3.0 diff --git a/go.sum b/go.sum index 6261b6a..e69de29 100644 --- a/go.sum +++ b/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= diff --git a/tpl_funcs.go b/tpl_funcs.go index ab29b83..da545c5 100644 --- a/tpl_funcs.go +++ b/tpl_funcs.go @@ -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 { - return strcase.ToLowerCamel(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) + } + + // Join words + s = strings.Join(words, "") + + return s }