Add StringTitle

This commit is contained in:
George Suntres
2026-03-30 18:29:58 -04:00
parent 65eec3c3b1
commit d829099a45
9 changed files with 73 additions and 0 deletions

View File

@@ -0,0 +1 @@
file_10.json

0
.file/dir1/file1.json Normal file
View File

2
.gitignore vendored
View File

@@ -22,5 +22,7 @@
!*.sh !*.sh
!*.md !*.md
!*.json
# ...even if they are in subdirectories # ...even if they are in subdirectories
!*/ !*/

25
file.go Normal file
View File

@@ -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()))
}
}
}

22
file_test.go Normal file
View File

@@ -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")
}
}

1
go.mod
View File

@@ -6,4 +6,5 @@ require (
github.com/go-viper/mapstructure/v2 v2.5.0 github.com/go-viper/mapstructure/v2 v2.5.0
github.com/ianlancetaylor/jsonschema v0.0.0-20251021232724-46ecbf32a0a5 github.com/ianlancetaylor/jsonschema v0.0.0-20251021232724-46ecbf32a0a5
go.mongodb.org/mongo-driver/v2 v2.5.0 go.mongodb.org/mongo-driver/v2 v2.5.0
golang.org/x/text v0.30.0
) )

2
go.sum
View File

@@ -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= 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 h1:yXUhImUjjAInNcpTcAlPHiT7bIXhshCTL3jVBkF3xaE=
go.mongodb.org/mongo-driver/v2 v2.5.0/go.mod h1:yOI9kBsufol30iFsl1slpdq1I0eHPzybRWdyYUs8K/0= 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=

View File

@@ -4,6 +4,17 @@ import (
"testing" "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) { func TestStringNormalize(t *testing.T) {
v := " some text here " v := " some text here "

View File

@@ -3,10 +3,19 @@ package commons
import ( import (
"regexp" "regexp"
"strings" "strings"
"golang.org/x/text/cases"
"golang.org/x/text/language"
) )
var caserTitle cases.Caser = cases.Title(language.English)
var whitespace = regexp.MustCompile(`\s+`) 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. // StringNormalize will replace arbitrary lengths of consecutive white space with a single one.
func StringNormalize(s string) string { func StringNormalize(s string) string {
s = whitespace.ReplaceAllString(s, " ") s = whitespace.ReplaceAllString(s, " ")