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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/router/index.js +21 -11
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tinkiet",
3
- "version": "0.11.0",
3
+ "version": "0.11.2",
4
4
  "description": "Pragmatic UI Web Components",
5
5
  "type": "module",
6
6
  "main": "index.js",
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 isLastPatternSegment = i === patternParts.length - 1;
36
- const segment = pathParts[i];
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 (pat.startsWith(':') && pat.endsWith('*')) {
41
+ if (isRestParam) {
39
42
  const name = pat.slice(1, -1); // remove ':' and '*'
40
- params[name] = decodeURIComponent(pathParts.slice(i).join('/'));
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 (pat.startsWith(':')) {
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
- // If we are at end of pattern but still have more path segments
60
- if (isLastPatternSegment && pathParts.length > patternParts.length) {
61
- return null;
62
- }
72
+ pathIndex += 1;
63
73
  }
64
- // Extra segments in path not matched by a rest param => no match
65
- if (pathParts.length > patternParts.length) {
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 };