summaryrefslogtreecommitdiff
path: root/internal
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2018-03-18 20:57:09 +0100
committerDimitri Sokolyuk <demon@dim13.org>2018-03-18 20:57:09 +0100
commitfe62a4b8a2b1137faec4ae38a247ecbb2d499ebb (patch)
tree0ab2238144fad951cb361bc1de59b34850c06418 /internal
parent40a73a96bddee55f7cd9c8d5296616ae43ee6d10 (diff)
...
Diffstat (limited to 'internal')
-rw-r--r--internal/plural/plural.go36
-rw-r--r--internal/plural/plural_test.go34
2 files changed, 70 insertions, 0 deletions
diff --git a/internal/plural/plural.go b/internal/plural/plural.go
new file mode 100644
index 0000000..a0b6d2e
--- /dev/null
+++ b/internal/plural/plural.go
@@ -0,0 +1,36 @@
+package plural
+
+import "strings"
+
+func Plural(s string) string {
+ l := len(s)
+ switch {
+ case strings.HasSuffix(s, "y"):
+ return s[:l-1] + "ies"
+ case strings.HasSuffix(s, "us"):
+ return s[:l-2] + "i"
+ case strings.HasSuffix(s, "ch"),
+ strings.HasSuffix(s, "x"),
+ strings.HasSuffix(s, "s"):
+ return s + "es"
+ case strings.HasSuffix(s, "f"):
+ return s[:l-1] + "ves"
+ case strings.HasSuffix(s, "man"),
+ strings.HasSuffix(s, "Man"):
+ return s[:l-2] + "en"
+ default:
+ return s + "s"
+ }
+}
+
+func Indefinite(s string, n int) string {
+ if strings.IndexByte("AEIOUÜaeiouü", s[0]) > 0 {
+ s = "an " + s
+ } else {
+ s = "a " + s
+ }
+ if n > 1 {
+ s = Plural(s)
+ }
+ return s
+}
diff --git a/internal/plural/plural_test.go b/internal/plural/plural_test.go
new file mode 100644
index 0000000..15939f7
--- /dev/null
+++ b/internal/plural/plural_test.go
@@ -0,0 +1,34 @@
+package plural
+
+import "testing"
+
+func TestPlural(t *testing.T) {
+ testCases := map[string]string{
+ "funny": "funnies",
+ "musmus": "musmi",
+ "much": "muches",
+ "mux": "muxes",
+ "fif": "fives",
+ "heman": "hemen",
+ "Wo Man": "Wo Men",
+ "else": "elses",
+ "usas": "usases",
+ "nismus": "nismi",
+ }
+ for k, v := range testCases {
+ if r := Plural(k); r != v {
+ t.Errorf("got %v, want %v", r, v)
+ }
+ }
+}
+
+func TestIndefinite(t *testing.T) {
+ testCases := map[string]string{
+ "abachus": "an abachi",
+ }
+ for k, v := range testCases {
+ if r := Indefinite(k, 2); r != v {
+ t.Errorf("got %v, want %v", r, v)
+ }
+ }
+}