Passed
Pull Request — master (#9)
by Serhii
01:51
created

timeago.getLanguageForm   B

Complexity

Conditions 6

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 6.1666

Importance

Changes 0
Metric Value
cc 6
eloc 15
dl 0
loc 23
ccs 10
cts 12
cp 0.8333
crap 6.1666
rs 8.6666
c 0
b 0
f 0
nop 1
1
package timeago
2
3
import (
4
	"fmt"
5
)
6
7
// getTimeTranslations returns array of translations for different
8
// cases. For example `1 second` must not have `s` at the end
9
// but `2 seconds` requires `s`. So this method keeps all
10
// possible options for the translated word.
11
func getTimeTranslations() map[string]map[string]string {
12 1
	return map[string]map[string]string{
13
		"seconds": {
14
			"single":  trans().Second,
15
			"plural":  trans().Seconds,
16
			"special": trans().SecondsSpecial,
17
		},
18
		"minutes": {
19
			"single":  trans().Minute,
20
			"plural":  trans().Minutes,
21
			"special": trans().MinutesSpecial,
22
		},
23
		"hours": {
24
			"single":  trans().Hour,
25
			"plural":  trans().Hours,
26
			"special": trans().HoursSpecial,
27
		},
28
		"days": {
29
			"single":  trans().Day,
30
			"plural":  trans().Days,
31
			"special": trans().DaysSpecial,
32
		},
33
		"weeks": {
34
			"single":  trans().Week,
35
			"plural":  trans().Weeks,
36
			"special": trans().WeeksSpecial,
37
		},
38
		"months": {
39
			"single":  trans().Month,
40
			"plural":  trans().Months,
41
			"special": trans().MonthsSpecial,
42
		},
43
		"years": {
44
			"single":  trans().Year,
45
			"plural":  trans().Years,
46
			"special": trans().YearsSpecial,
47
		},
48
	}
49
}
50
51
func getLanguageForm(num int) string {
52 1
	var form string
53 1
	lastDigit := getLastNumber(num)
54 1
	rule := getRules(num, lastDigit)[language]
55
56 1
	if rule.Special != nil {
57 1
		for _, isPassing := range rule.Special {
58 1
			if isPassing {
59 1
				return "special"
60
			}
61
		}
62
	}
63
64 1
	switch {
65
	case rule.Single:
66 1
		return "single"
67
	case rule.Plural:
68 1
		return "plural"
69
	}
70
71
	fmt.Printf("Provided rules don't apply to a number %d", num)
72
73
	return form
74
}
75