Skip to content

Commit

Permalink
better handling of multi-word idents
Browse files Browse the repository at this point in the history
Replace only the last part of an ident when pluralizing
  • Loading branch information
pgier committed Nov 22, 2019
1 parent 92a7634 commit 9602526
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
13 changes: 13 additions & 0 deletions ident.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,19 @@ func toParts(s string) []string {
var _ encoding.TextUnmarshaler = &Ident{}
var _ encoding.TextMarshaler = &Ident{}

// LastPart returns the last part/word of the original string
func (i *Ident) LastPart() string {
if len(i.Parts) == 0 {
return ""
}
return i.Parts[len(i.Parts)-1]
}

// ReplaceSuffix creates a new Ident with the original suffix replaced by new
func (i Ident) ReplaceSuffix(orig string, new string) Ident {
return New(strings.TrimSuffix(i.Original, orig) + new)
}

//UnmarshalText unmarshalls byte array into the Ident
func (i *Ident) UnmarshalText(data []byte) error {
(*i) = New(string(data))
Expand Down
1 change: 1 addition & 0 deletions name/tablize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func Test_Tableize(t *testing.T) {
{"People", "people"},
{"people", "people"},
{"BigPerson", "big_people"},
{"Wild Ox", "wild_oxen"},
}

for _, tt := range table {
Expand Down
6 changes: 3 additions & 3 deletions pluralize.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func Pluralize(s string) string {
// person = people
// datum = data
func (i Ident) Pluralize() Ident {
s := i.Original
s := i.LastPart()
if len(s) == 0 {
return New("")
}
Expand All @@ -33,11 +33,11 @@ func (i Ident) Pluralize() Ident {
return i
}
if p, ok := singleToPlural[ls]; ok {
return New(p)
return i.ReplaceSuffix(s, p)
}
for _, r := range pluralRules {
if strings.HasSuffix(ls, r.suffix) {
return New(r.fn(s))
return i.ReplaceSuffix(s, r.fn(s))
}
}

Expand Down

0 comments on commit 9602526

Please sign in to comment.