Passed
Pull Request — main (#38)
by Serhii
02:26 queued 50s
created

timeago.overwriteOutput   B

Complexity

Conditions 7

Size

Total Lines 28
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

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