stx-router 0.2.148 → 0.2.149

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/dist/matcher.d.ts CHANGED
@@ -1,3 +1,19 @@
1
1
  export declare function filePathToPattern(filePath: string, pagesDir: string): string;
2
+ /**
3
+ * Compile a route pattern into a regex and the parameter names its capture
4
+ * groups correspond to.
5
+ *
6
+ * Single left-to-right pass, which is the whole point. Doing it as three
7
+ * successive `.replace()` sweeps - optional, then catch-all, then required -
8
+ * collects the names in *sweep* order while the capture groups end up in
9
+ * *pattern* order, and the two disagree the moment one pattern mixes kinds.
10
+ *
11
+ * `/:owner/:repository/tree/:ref/:path*` collected as
12
+ * `[path, owner, repository, ref]` against groups `(owner)(repository)(ref)(path)`,
13
+ * so every value was bound to the wrong name: a repository browser given a URL
14
+ * showed the root of a repository named after the branch. Nothing errored -
15
+ * the regex matched and the page rendered - which is why it reads as a routing
16
+ * mystery rather than an off-by-one.
17
+ */
2
18
  export declare function patternToRegex(pattern: string): { regex: RegExp, params: string[] };
3
19
  export declare function matchRoute(pathname: string, routes: { pattern: string, regex: RegExp, params: string[] }[]): { route: typeof routes[0], params: Record<string, string> } | null;
package/dist/matcher.js CHANGED
@@ -13,21 +13,33 @@ export function filePathToPattern(filePath, pagesDir) {
13
13
  }
14
14
  export function patternToRegex(pattern) {
15
15
  const params = [];
16
- let regexStr = pattern.replace(/[.+^${}()|[\]\\]/g, "\\$&");
17
- regexStr = regexStr.replace(/\/:([^/]+)\?/g, (_, name) => {
18
- params.push(name);
19
- return "(?:/([^/]+))?";
20
- });
21
- regexStr = regexStr.replace(/:([^/]+)\*/g, (_, name) => {
22
- params.push(name);
23
- return "(.+)";
24
- });
25
- regexStr = regexStr.replace(/:([^/]+)/g, (_, name) => {
26
- params.push(name);
27
- return "([^/]+)";
28
- });
29
- regexStr = `^${regexStr}$`;
30
- return { regex: new RegExp(regexStr), params };
16
+ let regexStr = "", i = 0;
17
+ while (i < pattern.length) {
18
+ const rest = pattern.slice(i), optional = /^\/:([^/?*]+)\?/.exec(rest);
19
+ if (optional) {
20
+ params.push(optional[1]);
21
+ regexStr += "(?:/([^/]+))?";
22
+ i += optional[0].length;
23
+ continue;
24
+ }
25
+ const catchAll = /^:([^/?*]+)\*/.exec(rest);
26
+ if (catchAll) {
27
+ params.push(catchAll[1]);
28
+ regexStr += "(.+)";
29
+ i += catchAll[0].length;
30
+ continue;
31
+ }
32
+ const required = /^:([^/?*]+)/.exec(rest);
33
+ if (required) {
34
+ params.push(required[1]);
35
+ regexStr += "([^/]+)";
36
+ i += required[0].length;
37
+ continue;
38
+ }
39
+ regexStr += pattern[i].replace(/[.+^${}()|[\]\\*?]/g, "\\$&");
40
+ i += 1;
41
+ }
42
+ return { regex: new RegExp(`^${regexStr}$`), params };
31
43
  }
32
44
  export function matchRoute(pathname, routes) {
33
45
  const normalizedPath = pathname === "" ? "/" : pathname;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "stx-router",
3
3
  "type": "module",
4
- "version": "0.2.148",
4
+ "version": "0.2.149",
5
5
  "description": "File-based router for STX — .stx template discovery, nested layouts, typed route params, middleware, and client-side SPA navigation.",
6
6
  "author": "Chris Breuer <chris@stacksjs.org>",
7
7
  "license": "MIT",