Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed #24 by adjusting the function's conditions #60

Merged
merged 1 commit into from
Sep 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
sio4 marked this conversation as resolved.
Show resolved Hide resolved
s = i.Underscore().String()
s = strings.Replace(s, "_", "/", -1)
sio4 marked this conversation as resolved.
Show resolved Hide resolved
}
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