Skip to content

Commit

Permalink
moved stuff to do with "naming" things to a new package
Browse files Browse the repository at this point in the history
  • Loading branch information
markbates committed Jul 28, 2018
1 parent 2006683 commit 0187bab
Show file tree
Hide file tree
Showing 26 changed files with 587 additions and 593 deletions.
10 changes: 9 additions & 1 deletion camelize.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,21 @@ import (
"unicode"
)

// Camelize returns a camelize version of a string
// bob dylan = bobDylan
// widget_id = widgetID
// WidgetID = widgetID
func Camelize(s string) string {
return New(s).Camelize()
}

// Camelize returns a camelize version of a string
// bob dylan = bobDylan
// widget_id = widgetID
// WidgetID = widgetID
func (i Ident) Camelize() string {
var out []string
for i, part := range i.parts {
for i, part := range i.Parts {
var x string
var capped bool
for _, c := range part {
Expand Down
16 changes: 12 additions & 4 deletions capitalize.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,26 @@ package flect

import "unicode"

// Capitalize will cap the first letter of string
// user = User
// bob dylan = Bob dylan
// widget_id = Widget_id
func Capitalize(s string) string {
return New(s).Capitalize()
}

// Capitalize will cap the first letter of string
// user = User
// bob dylan = Bob dylan
// widget_id = Widget_id
func (i Ident) Capitalize() string {
var x string
if len(i.parts) == 0 {
if len(i.Parts) == 0 {
return ""
}
x = string(unicode.ToTitle(rune(i.original[0])))
if len(i.original) > 1 {
x += i.original[1:]
x = string(unicode.ToTitle(rune(i.Original[0])))
if len(i.Original) > 1 {
x += i.Original[1:]
}
return x
}
3 changes: 2 additions & 1 deletion capitalize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ func Test_Capitalize(t *testing.T) {
table := []tt{
{"", ""},
{"foo", "Foo"},
{"bob dylan", "Bob dylan"},
{"WidgetID", "WidgetID"},
{"widgetID", "WidgetID"},
{"widget_id", "Widget_id"},
{"widget_ID", "Widget_ID"},
{"widget ID", "Widget ID"},
}
Expand Down
102 changes: 0 additions & 102 deletions cover.out

This file was deleted.

24 changes: 0 additions & 24 deletions file.go

This file was deleted.

13 changes: 8 additions & 5 deletions ident.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,22 @@ import (
"unicode/utf8"
)

// Ident represents the string and it's parts
type Ident struct {
original string
parts []string
Original string
Parts []string
}

// String implements fmt.Stringer and returns the original string
func (i Ident) String() string {
return i.original
return i.Original
}

// New creates a new Ident from the string
func New(s string) Ident {
i := Ident{
original: s,
parts: toParts(s),
Original: s,
Parts: toParts(s),
}

return i
Expand Down
8 changes: 4 additions & 4 deletions ident_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ func Test_New(t *testing.T) {
}

for _, tt := range table {
t.Run(tt.original, func(st *testing.T) {
t.Run(tt.Original, func(st *testing.T) {
r := require.New(st)
i := New(tt.original)
r.Equal(tt.original, i.original)
r.Equal(tt.parts, i.parts)
i := New(tt.Original)
r.Equal(tt.Original, i.Original)
r.Equal(tt.Parts, i.Parts)
})
}
}
47 changes: 0 additions & 47 deletions name.go

This file was deleted.

28 changes: 28 additions & 0 deletions name/file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package name

import (
"strings"

"github.com/gobuffalo/flect"
)

// File creates a suitable file name
// admin/widget = admin/widget
// foo_bar = foo_bar
// U$ser = u_ser
func File(s string, exts ...string) string {
return New(s).File(exts...)
}

// File creates a suitable file name
// admin/widget = admin/widget
// foo_bar = foo_bar
// U$ser = u_ser
func (i Ident) File(exts ...string) string {
var parts []string

for _, part := range strings.Split(i.Original, "/") {
parts = append(parts, flect.Underscore(part))
}
return strings.Join(parts, "/") + strings.Join(exts, "")
}
2 changes: 1 addition & 1 deletion file_test.go → name/file_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package flect
package name

import (
"testing"
Expand Down
11 changes: 11 additions & 0 deletions name/ident.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package name

import "github.com/gobuffalo/flect"

type Ident struct {
flect.Ident
}

func New(s string) Ident {
return Ident{flect.New(s)}
}
51 changes: 51 additions & 0 deletions name/name.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package name

import (
"strings"

"github.com/gobuffalo/flect"
)

// Proper pascalizes and singularizes the string
// person = Person
// foo_bar = FooBar
// admin/widgets = AdminWidget
func Proper(s string) string {
return New(s).Proper()
}

// Proper pascalizes and singularizes the string
// person = Person
// foo_bar = FooBar
// admin/widgets = AdminWidget
func (i Ident) Proper() string {
s := i.Singularize()
s = flect.Pascalize(s)
return s
}

// Group pascalizes and pluralizes the string
// person = People
// foo_bar = FooBars
// admin/widget = AdminWidgets
func Group(s string) string {
return New(s).Group()
}

// Group pascalizes and pluralizes the string
// person = People
// foo_bar = FooBars
// admin/widget = AdminWidgets
func (i Ident) Group() string {
var parts []string
if len(i.Original) == 0 {
return i.Original
}
last := i.Parts[len(i.Parts)-1]
for _, part := range i.Parts[:len(i.Parts)-1] {
parts = append(parts, flect.Pascalize(part))
}
last = flect.Pascalize(flect.Pluralize(last))
parts = append(parts, last)
return strings.Join(parts, "")
}
Loading

0 comments on commit 0187bab

Please sign in to comment.