-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.go
51 lines (45 loc) · 1.05 KB
/
utils.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
package goclash
import (
"net/url"
"regexp"
"strconv"
"strings"
)
var (
regexpTag = regexp.MustCompile("[^A-Z0-9]+")
)
// CorrectTag returns a valid Clash of Clans tag. It will be uppercase, have no special characters, and have a # at the beginning.
//
// Credit to: https://github.com/mathsman5133/coc.py/blob/master/coc/utils.py
func CorrectTag(tag string) string {
return "#" + strings.ReplaceAll(regexpTag.ReplaceAllString(strings.ToUpper(tag), ""), "O", "0")
}
// TagURLSafe encodes a tag to be used in a URL.
func TagURLSafe(tag string) string {
return url.PathEscape(tag)
}
func createQueryParams(params map[string]any) url.Values {
query := url.Values{}
for key, value := range params {
if value == nil {
continue
}
switch v := value.(type) {
case string:
if v != "" {
query.Set(key, v)
}
case int:
query.Set(key, strconv.Itoa(v))
case *int:
query.Set(key, strconv.Itoa(*v))
case bool:
query.Set(key, strconv.FormatBool(v))
case []string:
for _, s := range v {
query.Add(key, s)
}
}
}
return query
}