-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtextual.go
executable file
·134 lines (119 loc) · 3.74 KB
/
textual.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package query
import (
"bytes"
"strings"
)
// Truncate the given string to length using … as ellipsis.
func Truncate(s string, length int) string {
return TruncateWithEllipsis(s, length, "…")
}
// TruncateWithEllipsis truncates the given string to length using provided ellipsis.
func TruncateWithEllipsis(s string, length int, ellipsis string) string {
l := len(s)
el := len(ellipsis)
if l+el > length {
s = string(s[0:length-el]) + ellipsis
}
return s
}
// ToPlural returns the plural version of an English word
// using some simple rules and a table of exceptions.
func ToPlural(text string) (plural string) {
// We only deal with lowercase
word := strings.ToLower(text)
// Check translations first, and return a direct translation if there is one
if translations[word] != "" {
return translations[word]
}
// If we have no translation, just follow some basic rules - avoid new rules if possible
if strings.HasSuffix(word, "s") || strings.HasSuffix(word, "z") || strings.HasSuffix(word, "h") {
plural = word + "es"
} else if strings.HasSuffix(word, "y") {
plural = strings.TrimRight(word, "y") + "ies"
} else if strings.HasSuffix(word, "um") {
plural = strings.TrimRight(word, "um") + "a"
} else {
plural = word + "s"
}
return plural
}
// common transformations from singular to plural
// Which irregulars are important or correct depends on your usage of English
// Some of those below are now considered old-fashioned and many more could be added
// As this is used for database models, it only needs a limited subset of all irregulars
// NB you should not attempt to reverse and singularize, but just use the singular provided
var translations = map[string]string{
"hero": "heroes",
"supernova": "supernovae",
"day": "days",
"monkey": "monkeys",
"money": "monies",
"chassis": "chassis",
"sheep": "sheep",
"aircraft": "aircraft",
"fish": "fish",
"nucleus": "nuclei",
"mouse": "mice",
"buffalo": "buffalo",
"species": "species",
"information": "information",
"wife": "wives",
"shelf": "shelves",
"index": "indices",
"matrix": "matrices",
"formula": "formulae",
"millennium": "millennia",
"ganglion": "ganglia",
"octopus": "octopodes",
"man": "men",
"woman": "women",
"person": "people",
"axis": "axes",
"die": "dice",
// ..etc
}
// ToSingular converts a word to singular.
// NB reversal from plurals may fail
func ToSingular(word string) (singular string) {
if strings.HasSuffix(word, "ses") || strings.HasSuffix(word, "zes") || strings.HasSuffix(word, "hes") {
singular = strings.TrimRight(word, "es")
} else if strings.HasSuffix(word, "ies") {
singular = strings.TrimRight(word, "ies") + "y"
} else if strings.HasSuffix(word, "a") {
singular = strings.TrimRight(word, "a") + "um"
} else {
singular = strings.TrimRight(word, "s")
}
return singular
}
// ToSnake converts a string from struct field names to corresponding database column names (e.g. FieldName to field_name).
func ToSnake(text string) string {
b := bytes.NewBufferString("")
for i, c := range text {
if i > 0 && c >= 'A' && c <= 'Z' {
b.WriteRune('_')
}
b.WriteRune(c)
}
return strings.ToLower(b.String())
}
// ToCamel converts a string from database column names to corresponding struct field names (e.g. field_name to FieldName).
func ToCamel(text string, private ...bool) string {
lowerCamel := false
if private != nil {
lowerCamel = private[0]
}
b := bytes.NewBufferString("")
s := strings.Split(text, "_")
for i, v := range s {
if len(v) > 0 {
s := v[:1]
if i > 0 || lowerCamel == false {
s = strings.ToUpper(s)
}
b.WriteString(s)
b.WriteString(v[1:])
}
}
return b.String()
}