Skip to content

Commit

Permalink
fixed #24 by adjusting the function's conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
sio4 committed Sep 3, 2022
1 parent a385761 commit a5e963f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 26 deletions.
39 changes: 22 additions & 17 deletions name/folder.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,39 @@ package name
import (
"regexp"
"strings"

"github.com/gobuffalo/flect"
)

var alphanum = regexp.MustCompile(`[^a-zA-Z0-9_\-\/]+`)
var alphanum = regexp.MustCompile(`[^a-zA-Z0-9_]+`)

// Folder creates a suitable folder name
// admin/widget = admin/widget
// foo_bar = foo_bar
// U$ser = u_ser
// Folder returns a suitable folder name. It removes any special characters
// from the given string `s` and returns a string consists of alpha-numeric
// characters.
// admin/widget --> admin/widget
// adminUser --> admin_user
// foo_bar --> foo_bar
// Admin --> admin
// U$ser --> u_ser
func Folder(s string, exts ...string) string {
return New(s).Folder(exts...).String()
}

// Folder creates a suitable folder name
// admin/widget = admin/widget
// foo_bar = foo/bar
// U$ser = u/ser
// Folder returns a suitable folder name. It removes any special characters
// from the given string `s` and returns a string consists of alpha-numeric
// characters.
// admin/widget --> admin/widget
// adminUser --> admin_user
// foo_bar --> foo_bar
// Admin --> admin
// U$ser --> u_ser
func (i Ident) Folder(exts ...string) Ident {
var parts []string

s := i.Original
if i.Pascalize().String() == s {
s = i.Underscore().String()
s = strings.Replace(s, "_", "/", -1)
}
for _, part := range strings.Split(s, "/") {
part = strings.ToLower(part)
part = alphanum.ReplaceAllString(part, "")
for _, part := range strings.Split(i.Original, "/") {
part = alphanum.ReplaceAllString(flect.Underscore(part), "")
parts = append(parts, part)
}

return New(strings.Join(parts, "/") + strings.Join(exts, ""))
}
10 changes: 7 additions & 3 deletions name/folder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,18 @@ import (
func Test_Folder(t *testing.T) {
table := []tt{
{"", ""},
{"foo_bar", "foo_bar"},
{"admin/widget", "admin/widget"},
{"admin/widgets", "admin/widgets"},
{"widget", "widget"},
{"widgets", "widgets"},
{"User", "user"},
{"U$er", "uer"},
{"AdminUser", "admin/user"},
{"U$er", "u_er"},
{"adminuser", "adminuser"},
{"Adminuser", "adminuser"},
{"AdminUser", "admin_user"},
{"adminUser", "admin_user"},
{"admin-user", "admin_user"},
{"admin_user", "admin_user"},
}

for _, tt := range table {
Expand Down
12 changes: 6 additions & 6 deletions underscore.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ import (
)

// Underscore a string
// bob dylan = bob_dylan
// Nice to see you! = nice_to_see_you
// widgetID = widget_id
// bob dylan --> bob_dylan
// Nice to see you! --> nice_to_see_you
// widgetID --> widget_id
func Underscore(s string) string {
return New(s).Underscore().String()
}

// Underscore a string
// bob dylan = bob_dylan
// Nice to see you! = nice_to_see_you
// widgetID = widget_id
// bob dylan --> bob_dylan
// Nice to see you! --> nice_to_see_you
// widgetID --> widget_id
func (i Ident) Underscore() Ident {
out := make([]string, 0, len(i.Parts))
for _, part := range i.Parts {
Expand Down

0 comments on commit a5e963f

Please sign in to comment.