Passed
Push — v3.0.0 ( 9e91a1...ba885d )
by Serhii
01:19
created

timeago.identifyGrammarRules   A

Complexity

Conditions 4

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 8
nop 2
dl 0
loc 14
rs 10
c 0
b 0
f 0
1
package timeago
2
3
import (
4
	"errors"
5
	"strings"
6
)
7
8
type rule struct {
9
	Zero bool
10
	One  bool
11
	Two  bool
12
	Few  bool
13
	Many bool
14
}
15
16
var grammarRules = func(num int) map[string]*rule {
17
	lastDigit := num % 10
18
19
	return map[string]*rule{
20
		"en,nl,de": {
21
			Zero: num == 0,
22
			One:  num == 1,
23
			Two:  num == 2,
24
			Few:  num > 1,
25
			Many: num > 1,
26
		},
27
		"ru,uk": {
28
			Zero: num == 0,
29
			One:  num == 1 || (num > 20 && lastDigit == 1),
30
			Two:  num == 2,
31
			Few:  lastDigit == 2 || lastDigit == 3 || lastDigit == 4,
32
			Many: (num >= 5 && num <= 20) || lastDigit == 0 || (lastDigit >= 5 && lastDigit <= 9),
33
		},
34
	}
35
}
36
37
func identifyGrammarRules(num int, lang string) (*rule, error) {
38
	rules := grammarRules(num)
39
40
	if v, ok := rules[lang]; ok {
41
		return v, nil
42
	}
43
44
	for langs, v := range rules {
45
		if strings.Contains(langs, lang) {
46
			return v, nil
47
		}
48
	}
49
50
	return nil, errors.New("[Timeago]: Language '" + lang + "' not found")
51
}
52