rules.go   A
last analyzed

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
cc 14
eloc 40
dl 0
loc 59
ccs 1
cts 1
cp 1
crap 14
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A timeago.identifyGrammarRules 0 14 4
1
package timeago
2
3
import (
4
	"strings"
5
6
	"github.com/SerhiiCho/timeago/v3/internal/utils"
7
)
8
9
type Rule struct {
10 1
	Zero  bool
11
	One   bool
12
	Two   bool
13
	Few   bool
14
	Many  bool
15
	Other bool
16
}
17
18
var grammarRules = func(num int) map[string]*Rule {
19
	end := num % 10
20
21
	return map[string]*Rule{
22
		"en,nl,de,es,fr": {
23
			Zero: num == 0,
24
			One:  num == 1,
25
			Two:  num == 2,
26
			Few:  num > 1,
27
			Many: num > 1,
28
		},
29
		"ru,uk,be": {
30
			Zero: num == 0,
31
			One:  num == 1 || (num > 20 && end == 1),
32
			Two:  num == 2,
33
			Few:  (end == 2 || end == 3 || end == 4) && (num < 10 || num > 20),
34
			Many: (num >= 5 && num <= 20) || end == 0 || end >= 5,
35
		},
36
		"zh,ja": {
37
			Zero: true,
38
			One:  true,
39
			Two:  true,
40
			Few:  true,
41
			Many: true,
42
		},
43
	}
44
}
45
46
func identifyGrammarRules(num int, lang string) (*Rule, error) {
47
	rules := grammarRules(num)
48
49
	if v, ok := rules[lang]; ok {
50
		return v, nil
51
	}
52
53
	for langs, v := range rules {
54
		if strings.Contains(langs, lang) {
55
			return v, nil
56
		}
57
	}
58
59
	return nil, utils.Errorf("Language '" + lang + "' not found")
60
}
61