Add StringTitle
This commit is contained in:
1
.file/dir1/dir1_1/file_10.json
Normal file
1
.file/dir1/dir1_1/file_10.json
Normal file
@@ -0,0 +1 @@
|
||||
file_10.json
|
||||
0
.file/dir1/file1.json
Normal file
0
.file/dir1/file1.json
Normal file
2
.gitignore
vendored
2
.gitignore
vendored
@@ -22,5 +22,7 @@
|
||||
!*.sh
|
||||
!*.md
|
||||
|
||||
!*.json
|
||||
|
||||
# ...even if they are in subdirectories
|
||||
!*/
|
||||
|
||||
25
file.go
Normal file
25
file.go
Normal 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
22
file_test.go
Normal 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
1
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
|
||||
)
|
||||
|
||||
2
go.sum
2
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=
|
||||
|
||||
@@ -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 "
|
||||
|
||||
|
||||
@@ -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, " ")
|
||||
|
||||
Reference in New Issue
Block a user