-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
pluralize.go
72 lines (60 loc) · 1.39 KB
/
pluralize.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package flect
import (
"strings"
"sync"
)
var pluralMoot = &sync.RWMutex{}
// Pluralize returns a plural version of the string
// user = users
// person = people
// datum = data
func Pluralize(s string) string {
return New(s).Pluralize().String()
}
// PluralizeWithSize will pluralize a string taking a number number into account.
// PluralizeWithSize("user", 1) = user
// PluralizeWithSize("user", 2) = users
func PluralizeWithSize(s string, i int) string {
if i == 1 || i == -1 {
return New(s).Singularize().String()
}
return New(s).Pluralize().String()
}
// Pluralize returns a plural version of the string
// user = users
// person = people
// datum = data
func (i Ident) Pluralize() Ident {
s := i.LastPart()
if len(s) == 0 {
return New("")
}
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
}
if p, ok := singleToPlural[ls]; ok {
if s == Capitalize(s) {
p = Capitalize(p)
}
return i.ReplaceSuffix(s, p)
}
for _, r := range pluralRules {
if strings.HasSuffix(s, r.suffix) {
return i.ReplaceSuffix(s, r.fn(s))
}
}
if strings.HasSuffix(ls, "s") {
return i
}
return New(i.String() + "s")
}