From 9821b8bf3777f08e85ccfae77f973e8838c542f2 Mon Sep 17 00:00:00 2001 From: Yonghwan SO Date: Sat, 3 Sep 2022 23:02:47 +0900 Subject: [PATCH] fixed #24 by adjusting the function's conditions --- name/folder.go | 39 ++++++++++++++++++++++----------------- name/folder_test.go | 10 +++++++--- underscore.go | 12 ++++++------ 3 files changed, 35 insertions(+), 26 deletions(-) diff --git a/name/folder.go b/name/folder.go index 0f1e5ee..5b7b978 100644 --- a/name/folder.go +++ b/name/folder.go @@ -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, "")) } diff --git a/name/folder_test.go b/name/folder_test.go index 78ef6e4..42f6f00 100644 --- a/name/folder_test.go +++ b/name/folder_test.go @@ -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 { diff --git a/underscore.go b/underscore.go index e1466d9..d42859a 100644 --- a/underscore.go +++ b/underscore.go @@ -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 {