-
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathpreact_iso_url_pattern.go
More file actions
122 lines (105 loc) · 2.76 KB
/
preact_iso_url_pattern.go
File metadata and controls
122 lines (105 loc) · 2.76 KB
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
// Run program: go run preact-iso-url-pattern.go
package main
import (
// "fmt"
"net/url"
"regexp"
"strings"
)
type Matches struct {
Params map[string]string `json:"params"`
Rest string `json:"rest,omitempty"`
}
func preactIsoUrlPatternMatch(urlStr, route string, matches *Matches) *Matches {
if matches == nil {
matches = &Matches{
Params: make(map[string]string),
}
}
urlParts := filterEmpty(strings.Split(urlStr, "/"))
routeParts := filterEmpty(strings.Split(route, "/"))
for i := 0; i < max(len(urlParts), len(routeParts)); i++ {
var m, param, flag string
if i < len(routeParts) {
re := regexp.MustCompile(`^(:?)(.*?)([+*?]?)$`)
matches := re.FindStringSubmatch(routeParts[i])
if len(matches) > 3 {
m, param, flag = matches[1], matches[2], matches[3]
}
}
var val string
if i < len(urlParts) {
val = urlParts[i]
}
// segment match:
if m == "" && param != "" && param == val {
continue
}
// /foo/* match
if m == "" && val != "" && flag == "*" {
matches.Rest = "/" + strings.Join(urlParts[i:], "/")
break
}
// segment mismatch / missing required field:
if m == "" || (val == "" && flag != "?" && flag != "*") {
return nil
}
rest := flag == "+" || flag == "*"
// rest (+/*) match:
if rest {
decodedParts := make([]string, len(urlParts[i:]))
for j, part := range urlParts[i:] {
decoded, err := url.QueryUnescape(part)
if err != nil {
decoded = part // fallback to original if decode fails
}
decodedParts[j] = decoded
}
val = strings.Join(decodedParts, "/")
} else if val != "" {
// normal/optional field: decode val (like JavaScript does)
decoded, err := url.QueryUnescape(val)
if err != nil {
decoded = urlParts[i]
}
val = decoded
}
matches.Params[param] = val
if rest {
break
}
}
return matches
}
func filterEmpty(s []string) []string {
var result []string
for _, str := range s {
if str != "" {
result = append(result, str)
}
}
return result
}
func max(a, b int) int {
if a > b {
return a
}
return b
}
// Example usage:
// func main() {
// params := &Matches{Params: make(map[string]string)}
// fmt.Println(preactIsoUrlPatternMatch("/foo/bar%20baz", "/foo/:param", params))
//
// params := &Matches{Params: make(map[string]string)}
// fmt.Println(preactIsoUrlPatternMatch("/foo/bar/baz", "/foo/*"))
//
// params := &Matches{Params: make(map[string]string)}
// fmt.Println(preactIsoUrlPatternMatch("/foo", "/foo/:param?"))
//
// params := &Matches{Params: make(map[string]string)}
// fmt.Println(preactIsoUrlPatternMatch("/foo/bar", "/bar/:param"))
//
// params := &Matches{Params: make(map[string]string)}
// fmt.Println(preactIsoUrlPatternMatch("/users/test%40example.com/posts", "/users/:userId/posts"))
// }