From d829099a4575c549194c497286a70d80884de83f Mon Sep 17 00:00:00 2001 From: George Suntres Date: Mon, 30 Mar 2026 18:29:58 -0400 Subject: [PATCH] Add StringTitle --- .file/dir1/dir1_1/file_10.json | 1 + .file/dir1/file1.json | 0 .gitignore | 2 ++ file.go | 25 +++++++++++++++++++++++++ file_test.go | 22 ++++++++++++++++++++++ go.mod | 1 + go.sum | 2 ++ string_test.go | 11 +++++++++++ strings.go | 9 +++++++++ 9 files changed, 73 insertions(+) create mode 100644 .file/dir1/dir1_1/file_10.json create mode 100644 .file/dir1/file1.json create mode 100644 file.go create mode 100644 file_test.go diff --git a/.file/dir1/dir1_1/file_10.json b/.file/dir1/dir1_1/file_10.json new file mode 100644 index 0000000..f6ed694 --- /dev/null +++ b/.file/dir1/dir1_1/file_10.json @@ -0,0 +1 @@ +file_10.json \ No newline at end of file diff --git a/.file/dir1/file1.json b/.file/dir1/file1.json new file mode 100644 index 0000000..e69de29 diff --git a/.gitignore b/.gitignore index a66ef27..880c32a 100644 --- a/.gitignore +++ b/.gitignore @@ -22,5 +22,7 @@ !*.sh !*.md +!*.json + # ...even if they are in subdirectories !*/ diff --git a/file.go b/file.go new file mode 100644 index 0000000..53eebc8 --- /dev/null +++ b/file.go @@ -0,0 +1,25 @@ +package commons + +import ( + "log" + "os" + "path/filepath" +) + +// FileDeepScan will scan dir recursively and append any files found to files. +func FileDeepScan(dir string, files *[]string) { + entries, err := os.ReadDir(dir) + if err != nil { + log.Printf("Failed to read %v", err) + return + } + + for _, entry := range entries { + fullPath := filepath.Join(dir, entry.Name()) + if entry.IsDir() { + FileDeepScan(fullPath, files) + } else { + *files = append(*files, filepath.Join(dir, entry.Name())) + } + } +} \ No newline at end of file diff --git a/file_test.go b/file_test.go new file mode 100644 index 0000000..a96fc4d --- /dev/null +++ b/file_test.go @@ -0,0 +1,22 @@ +package commons + +import ( + "testing" + "slices" +) + +func TestFileDeepScan(t *testing.T) { + files := make([]string, 0) + + FileDeepScan(".file", &files) + + l := len(files) + + if l != 2 { + t.Fatalf("Should have found %d files", l) + } + + if !slices.Contains(files, ".file/dir1/file1.json") { + t.Fatal("Should have found file1.json") + } +} \ No newline at end of file diff --git a/go.mod b/go.mod index b70ec7a..d27fdbc 100644 --- a/go.mod +++ b/go.mod @@ -6,4 +6,5 @@ require ( github.com/go-viper/mapstructure/v2 v2.5.0 github.com/ianlancetaylor/jsonschema v0.0.0-20251021232724-46ecbf32a0a5 go.mongodb.org/mongo-driver/v2 v2.5.0 + golang.org/x/text v0.30.0 ) diff --git a/go.sum b/go.sum index 40a8c23..a03e372 100644 --- a/go.sum +++ b/go.sum @@ -8,3 +8,5 @@ github.com/ianlancetaylor/jsonschema v0.0.0-20251021232724-46ecbf32a0a5 h1:x2QxK github.com/ianlancetaylor/jsonschema v0.0.0-20251021232724-46ecbf32a0a5/go.mod h1:KtN3dTgXsLnC5GJBRNmOPd/HUInNcQ84lUCrKJPrvDc= go.mongodb.org/mongo-driver/v2 v2.5.0 h1:yXUhImUjjAInNcpTcAlPHiT7bIXhshCTL3jVBkF3xaE= go.mongodb.org/mongo-driver/v2 v2.5.0/go.mod h1:yOI9kBsufol30iFsl1slpdq1I0eHPzybRWdyYUs8K/0= +golang.org/x/text v0.30.0 h1:yznKA/E9zq54KzlzBEAWn1NXSQ8DIp/NYMy88xJjl4k= +golang.org/x/text v0.30.0/go.mod h1:yDdHFIX9t+tORqspjENWgzaCVXgk0yYnYuSZ8UzzBVM= diff --git a/string_test.go b/string_test.go index deac355..0a4ed24 100644 --- a/string_test.go +++ b/string_test.go @@ -4,6 +4,17 @@ import ( "testing" ) + +func TestStringTitle(t *testing.T) { + v := "string to make title" + + vv := StringTitle(v) + + if vv != "String To Make Title" { + t.Fatalf("Failed to capitalize %s", vv) + } +} + func TestStringNormalize(t *testing.T) { v := " some text here " diff --git a/strings.go b/strings.go index 50e8282..adb0813 100644 --- a/strings.go +++ b/strings.go @@ -3,10 +3,19 @@ package commons import ( "regexp" "strings" + + "golang.org/x/text/cases" + "golang.org/x/text/language" ) +var caserTitle cases.Caser = cases.Title(language.English) + var whitespace = regexp.MustCompile(`\s+`) +func StringTitle(s string) string { + return caserTitle.String(s) +} + // StringNormalize will replace arbitrary lengths of consecutive white space with a single one. func StringNormalize(s string) string { s = whitespace.ReplaceAllString(s, " ")