Skip to content

Commit abecf32

Browse files
committed
Fix url matcher test
1 parent 95dce88 commit abecf32

2 files changed

Lines changed: 34 additions & 3 deletions

File tree

src/router/url/url-matcher.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,34 @@ import { joinNeighborsR, splitOnDelim } from "../../shared/strings.js";
2222

2323
/** @typedef {import("./interface.js").UrlMatcherCache} UrlMatcherCache */
2424

25+
/**
26+
* @param {any} str
27+
* @param {any} [param]
28+
*/
29+
function quoteRegExp(str, param) {
30+
let surroundPattern = ["", ""];
31+
32+
let result = str.replace(/[\\[\]^$*+?.()|{}]/g, "\\$&");
33+
34+
if (!param) return result;
35+
switch (param.squash) {
36+
case false:
37+
surroundPattern = ["(", `)${param.isOptional ? "?" : ""}`];
38+
break;
39+
case true:
40+
result = result.replace(/\/$/, "");
41+
surroundPattern = ["(?:/(", ")|/)?"];
42+
break;
43+
default:
44+
surroundPattern = [`(${param.squash}|`, ")?"];
45+
break;
46+
}
47+
48+
return (
49+
result + surroundPattern[0] + param.type.pattern.source + surroundPattern[1]
50+
);
51+
}
52+
2553
const memoizeTo = (
2654
/** @type {{ [x: string]: any; path?: UrlMatcher[]; }} */ obj,
2755
/** @type {string} */ _prop,
@@ -364,6 +392,9 @@ export class UrlMatcher {
364392
}
365393
}
366394
this._segments.push(segment);
395+
this._compiled = patterns
396+
.map((_pattern) => quoteRegExp.apply(null, /** @type {any} */ (_pattern)))
397+
.concat(quoteRegExp(segment));
367398
}
368399

369400
/**

src/router/url/url-service.spec.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,11 +202,11 @@ describe("UrlMatcher", () => {
202202
});
203203
});
204204

205-
xit("should allow embedded capture groups", () => {
205+
it("should allow embedded capture groups", () => {
206206
const shouldPass = {
207-
"/url/{matchedParam:([a-z]+)}/child/{childParam}":
207+
"/url/{matchedParam:(?:[a-z]+)}/child/{childParam}":
208208
"/url/someword/child/childParam",
209-
"/url/{matchedParam:([a-z]+)}/child/{childParam}?foo":
209+
"/url/{matchedParam:(?:[a-z]+)}/child/{childParam}":
210210
"/url/someword/child/childParam",
211211
};
212212

0 commit comments

Comments
 (0)