Skip to content

Commit

Permalink
Support of regexp named groups. #270
Browse files Browse the repository at this point in the history
  • Loading branch information
Krasimir Tsonev committed Feb 2, 2021
1 parent 2478e63 commit ea14b64
Show file tree
Hide file tree
Showing 15 changed files with 68 additions and 13 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.4

Support of regexp named groups. #270

## 8.8.3

Fixing a bug with notFound handler hooks. #271
Expand Down
7 changes: 7 additions & 0 deletions examples/270/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "navigo-example-basic-spa",
"private": true,
"scripts": {
"start": "node ./server.js"
}
}
30 changes: 30 additions & 0 deletions examples/270/page.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Navigo</title>
<link href="/styles.css" rel="stylesheet" />
</head>
<body>
<div>
<a href="/xyz/foobar" data-navigo>Click me</a>
</div>
<hr />
<div id="content"></div>
<script src="/navigo.js"></script>
<script>
window.addEventListener("load", () => {
const router = new Navigo("/");
const render = (content) =>
(document.querySelector("#content").innerHTML = content);

router
.on(/xyz\/(?<myName>[0-9a-z]{4,8})$/, (match) => {
render(`<p>${match.data.myName}</p>`);
})
.resolve();
});
</script>
</body>
</html>
11 changes: 11 additions & 0 deletions examples/270/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const express = require("express");
const app = express();
const port = 3000;
const { staticAssets, serveFile } = require("../utils/server");

staticAssets(app);
app.get("*", serveFile(__dirname + "/page.html"));

app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`);
});
1 change: 1 addition & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Examples done to investigate issues:
* How to add a confirm prompt before leave? [#250](https://github.com/krasimir/navigo/issues/250) - [source](./250)
* notFound is not working on new page (refresh) [#229](https://github.com/krasimir/navigo/issues/229) - [source](./229)
* Is it possible to replace/redirect a route request? [#228](https://github.com/krasimir/navigo/issues/228) - [source](./228)
* Named groups in RegExp [#270](https://github.com/krasimir/navigo/issues/191) - [source](./270)
* Incorrect path detection [#191](https://github.com/krasimir/navigo/issues/191) - [source](./191)
* Hook not firing on first page unload [#179](https://github.com/krasimir/navigo/issues/179) - [source](./179)
* Global before hook except for 1 page [#163](https://github.com/krasimir/navigo/issues/163) - [source](./163)
Expand Down
4 changes: 2 additions & 2 deletions lib/es/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,10 @@ export function matchRoute(context, route) {
}

var regexp = new RegExp(pattern, MATCH_REGEXP_FLAGS);
var match = current.match(regexp); // console.log(current, regexp);
var match = current.match(regexp);

if (match) {
var data = isString(route.path) ? regExpResultToParams(match, paramNames) : match.slice(1);
var data = isString(route.path) ? regExpResultToParams(match, paramNames) : match.groups ? match.groups : match.slice(1);
return {
url: current,
queryString: GETParams,
Expand Down
4 changes: 2 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.

4 changes: 2 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.

4 changes: 3 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,12 @@ export function matchRoute(context: QContext, route: Route): false | Match {
}
const regexp = new RegExp(pattern, MATCH_REGEXP_FLAGS);
const match = current.match(regexp);
// console.log(current, regexp);

if (match) {
const data = isString(route.path)
? regExpResultToParams(match, paramNames)
: match.groups
? match.groups
: match.slice(1);
return {
url: current,
Expand Down

0 comments on commit ea14b64

Please # to comment.