Passed
Push — master ( 58c7cb...3aad29 )
by Serhii
03:52 queued 02:07
created

timeago.overwriteOutput   A

Complexity

Conditions 5

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 11
dl 0
loc 20
rs 9.3333
c 0
b 0
f 0
ccs 10
cts 10
cp 1
crap 5
nop 1
1
package timeago
2
3
import "strings"
4
5
// overwriteOutput takes the final result, for example "4 days",
6
// and modifies the output if user added custom translations
7
// via config.Translations configuration.
8
func overwriteOutput(result string) string {
9 1
	if !translationsAreSet() {
10 1
		return result + " " + trans().Ago
11
	}
12
13 1
	suffix := trans().Ago
14
15 1
	for _, translationObj := range config.Translations {
16 1
		if translationObj.Language != config.Language {
17 1
			continue
18
		}
19
20 1
		result, suffix = modifyResult(result, translationObj.Translations)
21
	}
22
23 1
	if suffix == "" {
24 1
		return result
25
	}
26
27 1
	return result + " " + suffix
28
}
29
30
func modifyResult(result string, translations map[string]string) (string, string) {
31 1
	suffix := trans().Ago
32
33 1
	for key, translation := range translations {
34 1
		parts := strings.Split(result, " ")
35
36 1
		if key == trans().Ago {
37 1
			suffix = translation
38
		}
39
40 1
		if parts[1] == key {
41 1
			result = strings.Replace(result, key, translation, 1)
42
		}
43
	}
44
45 1
	return result, suffix
46
}
47