Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed incorrect singularize/pluralize for abbreviations #66

Merged
merged 1 commit into from
Feb 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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