Skip to content

Commit

Permalink
Fixing a bug when a regexp is used as a path #270
Browse files Browse the repository at this point in the history
  • Loading branch information
Krasimir Tsonev committed Feb 1, 2021
1 parent d305d1a commit d3db5f9
Show file tree
Hide file tree
Showing 14 changed files with 70 additions and 17 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 8.8.1

Fixing a bug when a regexp is used as a path. #270

## 8.8.0

Adding a `hashString` to the `Match` object. #269
Expand Down
8 changes: 6 additions & 2 deletions lib/es/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -299,9 +299,13 @@ export default function Navigo(appRoute, resolveOptions) {
currentLocationPath: currentLocation
};
setLocationPath(context, function () {});
path = clean(path);

if (typeof path === "string") {
path = clean(path);
}

var match = matchRoute(context, {
name: path,
name: String(path),
path: path,
handler: function handler() {},
hooks: {}
Expand Down
8 changes: 6 additions & 2 deletions lib/navigo.amd.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/navigo.amd.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/navigo.amd.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/navigo.amd.min.js.map

Large diffs are not rendered by default.

8 changes: 6 additions & 2 deletions lib/navigo.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/navigo.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/navigo.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/navigo.min.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "navigo",
"version": "8.8.0",
"version": "8.8.1",
"description": "A simple vanilla JavaScript router",
"main": "lib/navigo.js",
"browser": "lib/navigo.min.js",
Expand Down
35 changes: 35 additions & 0 deletions src/__tests__/issues.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,4 +316,39 @@ describe("Given the Navigo library", () => {
expect(location.pathname).toBe("/item/abc-1234");
});
});
describe("and the problem described in #270", () => {
it("should not throw an error", () => {
const h1 = jest.fn();
const h2 = jest.fn();
const r: NavigoRouter = new Navigo("/");

r.on(/xyz\/[0-9a-z]{4,8}$/, h1, {
leave: (done, match) => {
done();
},
});

r.on(/xyz\/[0-9a-z]{4,8}\/sub$/, h2, {
leave: (done, match) => {
done();
},
});

r.navigate("/xyz/asdf");
r.navigate("/xyz/asdf/sub");

expect(h1).toBeCalledTimes(1);
expect(h1).toBeCalledWith(
expect.objectContaining({
url: "xyz/asdf",
})
);
expect(h2).toBeCalledTimes(1);
expect(h2).toBeCalledWith(
expect.objectContaining({
url: "xyz/asdf/sub",
})
);
});
});
});
8 changes: 5 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ export default function Navigo(
return context.matches ? context.matches : false;
}
function directMatchWithLocation(
path: string,
path: string | RegExp,
currentLocation?: string
): false | Match {
const context: QContext = {
Expand All @@ -331,9 +331,11 @@ export default function Navigo(
currentLocationPath: currentLocation,
};
setLocationPath(context, () => {});
path = clean(path);
if (typeof path === "string") {
path = clean(path);
}
const match = matchRoute(context, {
name: path,
name: String(path),
path,
handler: () => {},
hooks: {},
Expand Down
2 changes: 1 addition & 1 deletion src/middlewares/checkForLeaveHook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default function checkForLeaveHook(context: QContext, done) {
}
let runHook = false;
const newLocationVSOldMatch = context.instance.matchLocation(
oldMatch.route.path as string,
oldMatch.route.path,
context.currentLocationPath
);
if (oldMatch.route.path !== "*") {
Expand Down

0 comments on commit d3db5f9

Please sign in to comment.