diff --git a/go.sum b/go.sum index 27edad9..666eecc 100644 --- a/go.sum +++ b/go.sum @@ -5,6 +5,7 @@ github.com/cockroachdb/apd v1.1.0/go.mod h1:8Sl8LxpKi29FqWXR16WEFZRNSz3SoPzUzeMe github.com/cockroachdb/cockroach-go v0.0.0-20181001143604-e0a95dfd547c/go.mod h1:XGLbWH/ujMcbPbhZq52Nv6UrCghb1yGn//133kEsvDk= github.com/codegangsta/negroni v1.0.0/go.mod h1:v0y3T5G7Y1UlFfyxFn/QLRU4a2EuNau2iZY63YTKWo0= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= github.com/dustin/go-humanize v0.0.0-20180713052910-9f541cc9db5d/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk= @@ -34,6 +35,7 @@ github.com/gobuffalo/envy v1.6.7/go.mod h1:N+GkhhZ/93bGZc6ZKhJLP6+m+tCNPKwgSpH9k github.com/gobuffalo/envy v1.6.8/go.mod h1:N+GkhhZ/93bGZc6ZKhJLP6+m+tCNPKwgSpH9kaifseQ= github.com/gobuffalo/envy v1.6.9/go.mod h1:N+GkhhZ/93bGZc6ZKhJLP6+m+tCNPKwgSpH9kaifseQ= github.com/gobuffalo/envy v1.6.10/go.mod h1:X0CFllQjTV5ogsnUrg+Oks2yTI+PU2dGYBJOEI2D1Uo= +github.com/gobuffalo/envy v1.6.11 h1:dCKSFypLRvqaaUtyzkfKKF2j35ce5agsqfyIrRmm02E= github.com/gobuffalo/envy v1.6.11/go.mod h1:Fiq52W7nrHGDggFPhn2ZCcHw4u/rqXkqo+i7FB6EAcg= github.com/gobuffalo/events v1.0.3/go.mod h1:Txo8WmqScapa7zimEQIwgiJBvMECMe9gJjsKNPN3uZw= github.com/gobuffalo/events v1.0.7/go.mod h1:z8txf6H9jWhQ5Scr7YPLWg/cgXBRj8Q4uYI+rsVCCSQ= @@ -170,6 +172,7 @@ github.com/jackc/fake v0.0.0-20150926172116-812a484cc733/go.mod h1:WrMFNQdiFJ80s github.com/jackc/pgx v3.2.0+incompatible/go.mod h1:0ZGrqGqkRlliWnWB4zKnWtjbSWbGkVEFm4TeybAXq+I= github.com/jmoiron/sqlx v0.0.0-20180614180643-0dae4fefe7c0/go.mod h1:IiEW3SEiiErVyFdH8NTuWjSifiEQKUoyK3LNqr2kCHU= github.com/joho/godotenv v1.2.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg= +github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc= github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg= github.com/karrick/godirwalk v1.7.5/go.mod h1:2c9FRhkDxdIbgkOnCEvnSWs71Bhugbl46shStcFDJ34= github.com/karrick/godirwalk v1.7.7/go.mod h1:2c9FRhkDxdIbgkOnCEvnSWs71Bhugbl46shStcFDJ34= @@ -216,7 +219,9 @@ github.com/onsi/gomega v1.4.2/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1Cpa github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY= github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/rogpeppe/go-internal v1.0.0 h1:o4VLZ5jqHE+HahLT6drNtSGTrrUA3wPBmtpgqtdbClo= github.com/rogpeppe/go-internal v1.0.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0= github.com/serenize/snaker v0.0.0-20171204205717-a683aaf2d516/go.mod h1:Yow6lPLSAXx2ifx470yD/nUe22Dv5vBvxK/UK9UUTVs= diff --git a/ident.go b/ident.go index 8c58455..78b51d4 100644 --- a/ident.go +++ b/ident.go @@ -2,7 +2,6 @@ package flect import ( "encoding" - "regexp" "strings" "unicode" "unicode/utf8" @@ -29,8 +28,6 @@ func New(s string) Ident { return i } -var splitRx = regexp.MustCompile("[^\\p{L}]") - func toParts(s string) []string { parts := []string{} s = strings.TrimSpace(s) diff --git a/name/folder.go b/name/folder.go index 16b1839..0f1e5ee 100644 --- a/name/folder.go +++ b/name/folder.go @@ -5,7 +5,7 @@ import ( "strings" ) -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 diff --git a/name/name_test.go b/name/name_test.go index f2895a5..a0f201a 100644 --- a/name/name_test.go +++ b/name/name_test.go @@ -1,8 +1,6 @@ package name import ( - "runtime" - "strings" "testing" "github.com/stretchr/testify/require" @@ -83,10 +81,3 @@ func Test_MarshalText(t *testing.T) { r.NoError((&n).UnmarshalText([]byte("bates"))) r.Equal("bates", n.String()) } - -func osify(s string) string { - if runtime.GOOS == "windows" { - return strings.Replace(s, "/", "\\", -1) - } - return s -} diff --git a/name/var_case.go b/name/var_case.go index 804ce10..734f408 100644 --- a/name/var_case.go +++ b/name/var_case.go @@ -31,3 +31,19 @@ func VarCasePlural(s string) string { func (i Ident) VarCasePlural() Ident { return Ident{i.Group().Pluralize().Camelize()} } + +// VarCase version of a name. +// foo_bar = fooBar +// admin/widget = adminWidget +// Users = users +func (i Ident) VarCase() Ident { + return Ident{i.Camelize()} +} + +// VarCase version of a name. +// foo_bar = fooBar +// admin/widget = adminWidget +// Users = users +func VarCase(s string) string { + return New(s).VarCase().String() +} diff --git a/name/var_case_test.go b/name/var_case_test.go index f15e821..a8f9734 100644 --- a/name/var_case_test.go +++ b/name/var_case_test.go @@ -51,3 +51,27 @@ func Test_VarCasePlural(t *testing.T) { }) } } + +func Test_VarCase(t *testing.T) { + table := []tt{ + {"foo_bar", "fooBar"}, + {"admin/widget", "adminWidget"}, + {"widget", "widget"}, + {"widgets", "widgets"}, + {"User", "user"}, + {"FooBar", "fooBar"}, + {"FooBars", "fooBars"}, + {"status", "status"}, + {"statuses", "statuses"}, + {"Status", "status"}, + {"Statuses", "statuses"}, + } + + for _, tt := range table { + t.Run(tt.act, func(st *testing.T) { + r := require.New(st) + r.Equal(tt.exp, VarCase(tt.act)) + r.Equal(tt.exp, VarCase(tt.exp)) + }) + } +} diff --git a/titleize.go b/titleize.go index e7a20d3..cbbf08a 100644 --- a/titleize.go +++ b/titleize.go @@ -20,8 +20,7 @@ func Titleize(s string) string { func (i Ident) Titleize() Ident { var parts []string for _, part := range i.Parts { - var x string - x = string(unicode.ToTitle(rune(part[0]))) + x := string(unicode.ToTitle(rune(part[0]))) if len(part) > 1 { x += part[1:] }