forked from skyzyx/iana-tlds
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
68 lines (52 loc) · 990 Bytes
/
main.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
package main
import (
"encoding/json"
"fmt"
"log"
"os"
"regexp"
"sort"
"strings"
"golang.org/x/net/idna"
)
const (
filename = "tlds-alpha-by-domain.txt"
)
var (
punycodeRE = regexp.MustCompile(`(?i)xn--[a-z0-9]+`)
puny = idna.New()
)
func main() {
content, err := os.ReadFile(filename)
if err != nil {
log.Fatal(err)
}
tldsRaw := strings.Split(string(content), "\n")
tldsRaw = tldsRaw[1 : len(tldsRaw)-1]
tldsRaw = mapper(tldsRaw, func(s string) string {
s = strings.ToLower(s)
if isPunyCode(s) {
s, err = puny.ToUnicode(s)
if err != nil {
log.Fatal(err)
}
}
return s
})
sort.Strings(tldsRaw)
b, err := json.MarshalIndent(tldsRaw, "", " ")
if err != nil {
log.Fatal(err)
}
fmt.Println(string(b))
}
func mapper(vs []string, f func(string) string) []string {
vsm := make([]string, len(vs))
for i, v := range vs {
vsm[i] = f(v)
}
return vsm
}
func isPunyCode(str string) bool {
return punycodeRE.MatchString(str)
}