Skip to content

Commit

Permalink
fixed incorrect singularize/pluralize for abbreviations
Browse files Browse the repository at this point in the history
  • Loading branch information
sio4 committed Feb 23, 2023
1 parent ef58751 commit 50f46e4
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
22 changes: 22 additions & 0 deletions flect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,28 @@ var singlePluralAssertions = []dict{
{"waste", "wastes", true, true},
{"psi", "psis", true, true},
{"pepsi", "pepsis", true, true},

// Acronyms
{"widget_uuid", "widget_uuids", true, true},
{"WidgetUUID", "WidgetUUIDs", true, true},
{"widgetUUID", "widgetUUIDs", true, true},
{"widgetUuid", "widgetUuids", true, true},
{"widget_UUID", "widget_UUIDs", true, true},

{"ID", "IDs", true, true},
{"IDS", "IDSes", true, true},
// id to ids (ID), ids to idses (IDS) is not supported
{"api", "apis", true, true},
{"API", "APIs", true, true},
{"html", "htmls", true, true},
{"HTML", "HTMLs", true, true},
{"FYI", "FYIs", true, true},
{"LAN", "LANs", true, true},
{"ssh", "sshs", true, true}, // sh
{"SSH", "SSHs", true, true},
{"eia", "eias", true, true}, // ia
{"EIA", "EIAs", true, true},
{"DNS", "DNSes", true, true},
}

func init() {
Expand Down
6 changes: 6 additions & 0 deletions plural_rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,9 @@ var dictionary = []word{
{singular: "shoe", plural: "shoes"},
{singular: "toe", plural: "toes", exact: true},
{singular: "graffiti", plural: "graffiti"},

// abbreviations
{singular: "ID", plural: "IDs", exact: true},
}

// singleToPlural is the highest priority map for Pluralize().
Expand Down Expand Up @@ -366,6 +369,9 @@ var singularToPluralSuffixList = []singularToPluralSuffix{
{"o", "oes"},
{"x", "xes"},

// for abbreviations
{"S", "Ses"},

// excluded rules: seems rare
// Words from Hebrew that add -im or -ot (eg, cherub becomes cherubim)
// - cherub (cherubs or cherubim), seraph (seraphs or seraphim)
Expand Down
10 changes: 9 additions & 1 deletion pluralize.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ func (i Ident) Pluralize() Ident {
pluralMoot.RLock()
defer pluralMoot.RUnlock()

// check if the Original has an explicit entry in the map
if p, ok := singleToPlural[i.Original]; ok {
return i.ReplaceSuffix(i.Original, p)
}
if _, ok := pluralToSingle[i.Original]; ok {
return i
}

ls := strings.ToLower(s)
if _, ok := pluralToSingle[ls]; ok {
return i
Expand All @@ -51,7 +59,7 @@ func (i Ident) Pluralize() Ident {
}

for _, r := range pluralRules {
if strings.HasSuffix(ls, r.suffix) {
if strings.HasSuffix(s, r.suffix) {
return i.ReplaceSuffix(s, r.fn(s))
}
}
Expand Down
10 changes: 9 additions & 1 deletion singularize.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ func (i Ident) Singularize() Ident {
singularMoot.RLock()
defer singularMoot.RUnlock()

// check if the Original has an explicit entry in the map
if p, ok := pluralToSingle[i.Original]; ok {
return i.ReplaceSuffix(i.Original, p)
}
if _, ok := singleToPlural[i.Original]; ok {
return i
}

ls := strings.ToLower(s)
if p, ok := pluralToSingle[ls]; ok {
if s == Capitalize(s) {
Expand All @@ -48,7 +56,7 @@ func (i Ident) Singularize() Ident {
}

for _, r := range singularRules {
if strings.HasSuffix(ls, r.suffix) {
if strings.HasSuffix(s, r.suffix) {
return i.ReplaceSuffix(s, r.fn(s))
}
}
Expand Down

0 comments on commit 50f46e4

Please sign in to comment.