Skip to content

Commit

Permalink
fixes #20 with related minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
sio4 committed Sep 3, 2022
1 parent 8096cd0 commit f9ed90b
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 10 deletions.
5 changes: 5 additions & 0 deletions custom_data.go
Expand Up @@ -8,6 +8,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
"strings"
)

func init() {
Expand Down Expand Up @@ -75,6 +76,10 @@ func LoadInflections(r io.Reader) error {
defer singularMoot.Unlock()

for s, p := range m {
if strings.Contains(s, " ") || strings.Contains(p, " ") {
// flect works with parts, so multi-words should not be allowed
return fmt.Errorf("inflection elements should be a single word")
}
singleToPlural[s] = p
pluralToSingle[p] = s
}
Expand Down
38 changes: 36 additions & 2 deletions flect_test.go
Expand Up @@ -16,8 +16,8 @@ type tt struct {
func Test_LoadInflections(t *testing.T) {
r := require.New(t)
m := map[string]string{
"beatle": "the beatles",
"xyz": "zyx",
"baby": "bebe",
"xyz": "zyx",
}

b, err := json.Marshal(m)
Expand All @@ -33,6 +33,30 @@ func Test_LoadInflections(t *testing.T) {
}
}

func Test_LoadInflectionsWrongSingular(t *testing.T) {
r := require.New(t)
m := map[string]string{
"a file": "files",
}

b, err := json.Marshal(m)
r.NoError(err)

r.Error(LoadInflections(bytes.NewReader(b)))
}

func Test_LoadInflectionsWrongPlural(t *testing.T) {
r := require.New(t)
m := map[string]string{
"beatle": "the beatles",
}

b, err := json.Marshal(m)
r.NoError(err)

r.Error(LoadInflections(bytes.NewReader(b)))
}

func Test_LoadAcronyms(t *testing.T) {
r := require.New(t)
m := []string{
Expand All @@ -53,6 +77,16 @@ func Test_LoadAcronyms(t *testing.T) {

var singlePluralAssertions = []tt{
{"", ""},
{"Car", "Cars"},
{"Boy", "Boys"},
{"GoodBoy", "GoodBoys"},
{"Axis", "Axes"},
{"Child", "Children"},
{"GoodChild", "GoodChildren"},
{"SmartPerson", "SmartPeople"},
{"SuperbOx", "SuperbOxen"},
{"WildOx", "WildOxen"},
{"wild_ox", "wild_oxen"},
{"ability", "abilities"},
{"address", "addresses"},
{"agency", "agencies"},
Expand Down
8 changes: 4 additions & 4 deletions plural_rules.go
Expand Up @@ -47,6 +47,7 @@ var singleToPlural = map[string]string{
"concerto": "concertos",
"corpus": "corpora",
"crisis": "crises",
"criterion": "criteria",
"curriculum": "curriculums",
"datum": "data",
"deer": "deer",
Expand All @@ -56,7 +57,6 @@ var singleToPlural = map[string]string{
"ellipsis": "ellipses",
"equipment": "equipment",
"erratum": "errata",
"faux pas": "faux pas",
"fez": "fezzes",
"fish": "fish",
"focus": "foci",
Expand All @@ -82,6 +82,7 @@ var singleToPlural = map[string]string{
"locus": "loci",
"louse": "lice",
"matrix": "matrices",
"medium": "media",
"minutia": "minutiae",
"money": "money",
"moose": "moose",
Expand All @@ -96,6 +97,7 @@ var singleToPlural = map[string]string{
"ovum": "ova",
"ox": "oxen",
"parenthesis": "parentheses",
"person": "people",
"phenomenon": "phenomena",
"photo": "photos",
"phylum": "phyla",
Expand Down Expand Up @@ -157,9 +159,7 @@ type singularToPluralSuffix struct {
}

var singularToPluralSuffixList = []singularToPluralSuffix{
{"iterion", "iteria"},
{"campus", "campuses"},
{"genera", "genus"},
{"person", "people"},
{"phylum", "phyla"},
{"randum", "randa"},
Expand All @@ -169,6 +169,7 @@ var singularToPluralSuffixList = []singularToPluralSuffix{
{"child", "children"},
{"chive", "chives"},
{"focus", "foci"},
{"genus", "genera"},
{"hello", "hellos"},
{"jeans", "jeans"},
{"louse", "lice"},
Expand All @@ -195,7 +196,6 @@ var singularToPluralSuffixList = []singularToPluralSuffix{
{"oose", "eese"},
{"ouse", "ouses"},
{"ovum", "ova"},
{"rion", "ria"},
{"shoe", "shoes"},
{"stis", "stes"},
{"tive", "tives"},
Expand Down
3 changes: 3 additions & 0 deletions pluralize.go
Expand Up @@ -43,6 +43,9 @@ func (i Ident) Pluralize() Ident {
return i
}
if p, ok := singleToPlural[ls]; ok {
if s == Capitalize(s) {
p = Capitalize(p)
}
return i.ReplaceSuffix(s, p)
}
for _, r := range pluralRules {
Expand Down
12 changes: 8 additions & 4 deletions singularize.go
Expand Up @@ -30,28 +30,32 @@ func SingularizeWithSize(s string, i int) string {
// data = datum
// people = person
func (i Ident) Singularize() Ident {
s := i.Original
s := i.LastPart()
if len(s) == 0 {
return i
}

singularMoot.RLock()
defer singularMoot.RUnlock()

ls := strings.ToLower(s)
if p, ok := pluralToSingle[ls]; ok {
return New(p)
if s == Capitalize(s) {
p = Capitalize(p)
}
return i.ReplaceSuffix(s, p)
}
if _, ok := singleToPlural[ls]; ok {
return i
}
for _, r := range singularRules {
if strings.HasSuffix(ls, r.suffix) {
return New(r.fn(s))
return i.ReplaceSuffix(s, r.fn(s))
}
}

if strings.HasSuffix(s, "s") {
return New(s[:len(s)-1])
return i.ReplaceSuffix("s", "")
}
return i
}

0 comments on commit f9ed90b

Please sign in to comment.