Skip to content

Commit

Permalink
Add consider camel case
Browse files Browse the repository at this point in the history
  • Loading branch information
triole committed Aug 4, 2024
1 parent ac92b20 commit 89c8b36
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
3 changes: 2 additions & 1 deletion src/normalize.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"strings"

"github.com/triole/logseal"
"gopkg.in/yaml.v2"
yaml "gopkg.in/yaml.v2"

_ "embed"
)
Expand All @@ -33,6 +33,7 @@ func (ps *tPaths) normalizeAll() (r string) {

func normalize(pth tPath, replacerSchemes tReplacerSchemes) (r tPath) {
r = pth
r.Name = toSnakeCase(r.Name)
r.Name = strings.ToLower(r.Name)
if r.IsFolder {
r.Name = strings.Replace(r.Name, ".", "_", -1)
Expand Down
3 changes: 2 additions & 1 deletion src/normalize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func TestNormalize(t *testing.T) {
validateNormalize("hello there 11", "txt", "hello_there11", "txt", rs, t)
validateNormalize("hello there 222", "txt", "hello_there_222", "txt", rs, t)
validateNormalize("hello there 3333", "txt", "hello_there_3333", "txt", rs, t)
validateNormalize("Hello HereAndThere 3333", "txt", "hello_here_and_there_3333", "txt", rs, t)
}

func validateNormalize(inpName, inpExt, expName, expExt string, rs tReplacerSchemes, t *testing.T) {
Expand All @@ -29,7 +30,7 @@ func validateNormalize(inpName, inpExt, expName, expExt string, rs tReplacerSche
if res.Extension != exp.Extension || res.Name != exp.Name {
t.Errorf(
"\ntest normalize path validation failed"+
"\ninput %s\nresult %s\nexpectation %s",
"\ninp %s\nres %s\nexp %s",
printPath(inp), printPath(res), printPath(exp),
)
}
Expand Down
26 changes: 20 additions & 6 deletions src/rx.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,35 @@
package main

import "regexp"
import (
"regexp"
"strings"
)

var (
matchFirstCap = regexp.MustCompile("(.)([A-Z][a-z]+)")
matchAllCap = regexp.MustCompile("([a-z0-9])([A-Z])")
)

func rxMatch(rx string, str string) (b bool) {
re, _ := regexp.Compile(rx)
b = re.MatchString(str)
return
}

func rxFind(rx string, str string) (r string) {
temp, _ := regexp.Compile(rx)
r = temp.FindString(str)
return
}
// func rxFind(rx string, str string) (r string) {
// temp, _ := regexp.Compile(rx)
// r = temp.FindString(str)
// return
// }

func rxReplaceAll(basestring, regex, newstring string) (r string) {
rx := regexp.MustCompile(regex)
r = rx.ReplaceAllString(basestring, newstring)
return
}

func toSnakeCase(str string) string {
snake := matchFirstCap.ReplaceAllString(str, "${1}_${2}")
snake = matchAllCap.ReplaceAllString(snake, "${1}_${2}")
return strings.ToLower(snake)
}

0 comments on commit 89c8b36

Please sign in to comment.