-
Notifications
You must be signed in to change notification settings - Fork 0
/
plural.go
55 lines (50 loc) · 1.08 KB
/
plural.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
// plural package to make pluralization of russian words
package plural
type Number interface {
int | int32 | int64 | uint | uint32 | uint64
}
// Noun function to make pluralization for noun
func Noun[n Number](num n, oneWord, twoWord, ThreeWord string) string {
switch getNounPluralForm(num) {
case 1:
return twoWord
case 2:
return ThreeWord
default:
return oneWord
}
}
// Verb function to make pluralization for verb
func Verb[n Number](num n, oneWord, twoWord, ThreeWord string) string {
switch getVerbPluralForm(num) {
case 1:
return twoWord
case 2:
return ThreeWord
default:
return oneWord
}
}
func getNounPluralForm[n Number](a n) int {
if a%10 == 1 && a%100 != 11 {
return 0
} else if a%10 >= 2 && a%10 <= 4 && (a%100 < 10 || a%100 >= 20) {
return 1
} else {
return 2
}
}
func getVerbPluralForm[n Number](a n) int {
if a > 1000000 {
return 2
}
if a > 1000 && a < 1000000 && a%1000 == 0 {
a /= 1000
}
if a%10 == 1 && a%100 != 11 || a%10000 == 1000 {
return 0
} else if a%10 >= 2 && a%10 <= 4 && (a%100 < 10 || a%100 >= 20) {
return 1
}
return 2
}