This repository has been archived by the owner on May 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
136 lines (102 loc) · 3.18 KB
/
util.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package ow_stats
import (
"regexp"
"strings"
"strconv"
"log"
"golang.org/x/text/transform"
"golang.org/x/text/unicode/norm"
"golang.org/x/text/runes"
"unicode"
)
var spaceRegex = regexp.MustCompile(`[-\s]`)
var nonAlphaRegex = regexp.MustCompile(`\W`)
var normalizeRegex = regexp.MustCompile(`_{2,}`)
var hourRegex = regexp.MustCompile(`(?P<Val>[0-9]+) hours?`)
var minuteRegex = regexp.MustCompile(`(?P<Val>[0-9]+) minutes?`)
var secondRegex = regexp.MustCompile(`(?P<Val>[0-9]+(?:\.[0-9]+)?) seconds?`)
var percentRegex = regexp.MustCompile(`(?P<Val>[0-9]{1,3})\s?%`)
var pluralizerRegex = regexp.MustCompile(`(?P<Start>_|[^a-z]|^)(?P<Term>blow|boost|kill|assist|barrier|hit|multikill|elimination)(?P<End>$|[^a-z])`)
func SanitizeKey(text string) string {
var t = transform.Chain(norm.NFD, runes.Remove(runes.In(unicode.Mn)), norm.NFC)
text = strings.ToLower(text)
text, _, _ = transform.String(t, text)
text = spaceRegex.ReplaceAllString(text, "_")
text = nonAlphaRegex.ReplaceAllString(text, "")
text = normalizeRegex.ReplaceAllString(text, "_")
text = strings.Replace(text, "soldier_76", "soldier76", 1)
return text
}
func SanitizeValue(text string) float32 {
text = strings.ToLower(text)
fnParseFloat := func(text string) (float32, error) {
longNum := strings.Replace(text, ",", "", -1)
v, err := strconv.ParseFloat(longNum, 32)
if err != nil {
return 0., err
}
return float32(v), nil
}
fnParseSimpleFloat32 := func(t string) float32 {
if v, err := strconv.Atoi(t); err == nil {
return float32(v)
}
return 0
}
if text == "--" {
return .0
}
if v, err := fnParseFloat(text); err == nil {
return float32(v)
}
// value is a percentage
if m := percentRegex.FindStringSubmatch(text); m != nil {
if v, err := fnParseFloat(m[1]); err == nil {
return v / 100.
}
log.Printf("Value is a percentage but failed to decode: %s\n", text)
return 0
}
if strings.ContainsRune(text, ':') {
parts := strings.Split(text, ":")
var h, m, s float32
switch len(parts) {
case 3:
h, m, s = fnParseSimpleFloat32(parts[0]), fnParseSimpleFloat32(parts[1]), fnParseSimpleFloat32(parts[2])
case 2:
m, s = fnParseSimpleFloat32(parts[0]), fnParseSimpleFloat32(parts[1])
case 1:
s = fnParseSimpleFloat32(parts[0])
}
return h + ((m + (s / 60.0)) / 60.0)
}
if m := hourRegex.FindStringSubmatch(text); m != nil {
if v, err := fnParseFloat(m[1]); err == nil {
return v
}
log.Printf("Value is an hour but failed to decode: %s\n", text)
return 0
}
if m := minuteRegex.FindStringSubmatch(text); m != nil {
if v, err := fnParseFloat(m[1]); err == nil {
return v / 60.0
}
log.Printf("Value is a minute but failed to decode: %s\n", text)
return 0
}
if m := secondRegex.FindStringSubmatch(text); m != nil {
if v, err := fnParseFloat(m[1]); err == nil {
return v / 3600.0
}
log.Printf("Value is a second but failed to decode: %s\n", text)
return 0
}
log.Printf("Unable to find anything to parse: %s\n", text)
return 0
}
func Pluralizer(text string) string {
text = strings.ToLower(text)
text = pluralizerRegex.ReplaceAllString(text, `${1}${2}s${3}`)
text = strings.Replace(text, "kills_streak_", "kill_streak_", 1)
return text
}