This repository has been archived by the owner on Oct 2, 2022. It is now read-only.
/
pluralize.go
96 lines (77 loc) · 1.53 KB
/
pluralize.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package ovrstat /* import "s32x.com/ovrstat/ovrstat" */
import (
"sort"
"strings"
"unicode"
"github.com/jinzhu/inflection"
)
var keywords = []string{
"kill",
"multikill",
"death",
"generator",
"shield",
"enemy",
"turret",
"hit",
"pad",
"blow",
"assist",
"elimination",
"card",
"dragonblade",
"player",
"bomb",
}
func transformKey(str string) string {
split := splitKeywords(str)
sort.Sort(split)
var lowerKeyPart string
for _, keyPart := range split {
lowerKeyPart = strings.ToLower(keyPart)
for _, keyword := range keywords {
if keyword == lowerKeyPart {
replacement := inflection.Plural(keyword)
if unicode.IsUpper(rune(keyPart[0])) {
replacement = strings.ToUpper(replacement[0:1]) + replacement[1:]
}
str = strings.Replace(str, keyPart, replacement, 1)
return str
}
}
}
if str == "allDamageDone" {
return "damageDone"
}
return str
}
type Keywords []string
func (s Keywords) Len() int {
return len(s)
}
func (s Keywords) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (s Keywords) Less(i, j int) bool {
return sliceIndexOf(keywords, s[i]) < sliceIndexOf(keywords, s[j])
}
func splitKeywords(str string) Keywords {
var words Keywords
l := 0
for s := str; s != ""; s = s[l:] {
l = strings.IndexFunc(s[1:], unicode.IsUpper) + 1
if l <= 0 {
l = len(s)
}
words = append(words, s[:l])
}
return words
}
func sliceIndexOf(s []string, str string) int {
for i, val := range s {
if strings.ToLower(val) == strings.ToLower(str) {
return i
}
}
return len(s)
}