tinkiet 0.11.0 → 0.11.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/router/index.js +21 -11
package/package.json
CHANGED
package/router/index.js
CHANGED
|
@@ -30,22 +30,35 @@ function matchPath(pattern, pathname) {
|
|
|
30
30
|
const patternParts = pattern.split('/').filter(Boolean);
|
|
31
31
|
const pathParts = pathname.split('/').filter(Boolean);
|
|
32
32
|
const params = {};
|
|
33
|
+
let pathIndex = 0;
|
|
33
34
|
for (let i = 0; i < patternParts.length; i++) {
|
|
34
35
|
const pat = patternParts[i];
|
|
35
|
-
const
|
|
36
|
-
const
|
|
36
|
+
const segment = pathParts[pathIndex];
|
|
37
|
+
const isRestParam = pat.startsWith(':') && pat.endsWith('*');
|
|
38
|
+
const isOptionalParam = pat.startsWith(':') && pat.endsWith('?');
|
|
39
|
+
const isParam = pat.startsWith(':') && !isRestParam && !isOptionalParam;
|
|
37
40
|
// Wildcard "rest" param: :name*
|
|
38
|
-
if (
|
|
41
|
+
if (isRestParam) {
|
|
39
42
|
const name = pat.slice(1, -1); // remove ':' and '*'
|
|
40
|
-
params[name] = decodeURIComponent(pathParts.slice(
|
|
43
|
+
params[name] = decodeURIComponent(pathParts.slice(pathIndex).join('/'));
|
|
41
44
|
return { params };
|
|
42
45
|
}
|
|
46
|
+
// Optional param :name?
|
|
47
|
+
if (isOptionalParam) {
|
|
48
|
+
if (segment != null) {
|
|
49
|
+
const name = pat.slice(1, -1); // remove ':' and '?'
|
|
50
|
+
params[name] = decodeURIComponent(segment);
|
|
51
|
+
pathIndex += 1;
|
|
52
|
+
}
|
|
53
|
+
continue;
|
|
54
|
+
}
|
|
43
55
|
// Simple param :name
|
|
44
|
-
if (
|
|
56
|
+
if (isParam) {
|
|
45
57
|
if (segment == null)
|
|
46
58
|
return null;
|
|
47
59
|
const name = pat.slice(1);
|
|
48
60
|
params[name] = decodeURIComponent(segment);
|
|
61
|
+
pathIndex += 1;
|
|
49
62
|
continue;
|
|
50
63
|
}
|
|
51
64
|
// Plain wildcard "*"
|
|
@@ -56,13 +69,10 @@ function matchPath(pattern, pathname) {
|
|
|
56
69
|
if (segment !== pat) {
|
|
57
70
|
return null;
|
|
58
71
|
}
|
|
59
|
-
|
|
60
|
-
if (isLastPatternSegment && pathParts.length > patternParts.length) {
|
|
61
|
-
return null;
|
|
62
|
-
}
|
|
72
|
+
pathIndex += 1;
|
|
63
73
|
}
|
|
64
|
-
// Extra segments in path not matched by
|
|
65
|
-
if (
|
|
74
|
+
// Extra segments in path not matched by optional/rest params => no match
|
|
75
|
+
if (pathIndex < pathParts.length) {
|
|
66
76
|
return null;
|
|
67
77
|
}
|
|
68
78
|
return { params };
|