Skip to content

Commit

Permalink
(preact-iso) - trim leading and trailing slash (#416)
Browse files Browse the repository at this point in the history
* trim leading and trailing slash

* try truthy split

* Update router.js

* Update router.js

* fix test arguments

* add changeset and update demo

* fix matching

* Update packages/preact-iso/router.js

* minor fix

* Apply suggestions from code review

Co-authored-by: Jason Miller <developit@users.noreply.github.com>

* update indents in demo

* Update router.js

Co-authored-by: Jason Miller <developit@users.noreply.github.com>
  • Loading branch information
JoviDeCroock and developit authored Mar 14, 2021
1 parent b225cc2 commit f320e3e
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/cyan-badgers-grab.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'preact-iso': patch
---

Fix routes with leading/trailing slashes
6 changes: 3 additions & 3 deletions packages/preact-iso/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ const UPDATE = (state, url) => {
};

export const exec = (url, route, matches) => {
url = url.trim('/').split('/');
route = (route || '').trim('/').split('/');
url = url.split('/').filter(Boolean);
route = (route || '').split('/').filter(Boolean);
for (let i = 0, val; i < Math.max(url.length, route.length); i++) {
let [, m, param, flag] = (route[i] || '').match(/^(:?)(.*?)([+*?]?)$/);
val = url[i];
Expand All @@ -32,7 +32,7 @@ export const exec = (url, route, matches) => {
// field match:
matches[param] = val && decodeURIComponent(val);
// normal/optional field:
if (flag >= '?') continue;
if (flag >= '?' || flag === '') continue;
// rest (+/*) match:
matches[param] = url.slice(i).map(decodeURIComponent).join('/');
break;
Expand Down
8 changes: 8 additions & 0 deletions packages/preact-iso/test/match.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,12 @@ describe('match', () => {
const inaccurateResult = exec('/', '/user/:id?', { path: '/' });
expect(inaccurateResult).toEqual(undefined);
});

it('Handles leading/trailing slashes', () => {
const result = exec('/about-late/_SEGMENT1_/_SEGMENT2_/', '/about-late/:seg1/:seg2/', {});
expect(result).toEqual({
seg1: '_SEGMENT1_',
seg2: '_SEGMENT2_'
});
});
});
6 changes: 3 additions & 3 deletions packages/wmr/demo/public/lib/loc.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ const UPDATE = (state, url, push) => {
};

export const exec = (url, route, matches) => {
url = url.trim('/').split('/');
route = (route || '').trim('/').split('/');
url = url.split('/').filter(Boolean);
route = (route || '').split('/').filter(Boolean);
for (let i = 0, val; i < Math.max(url.length, route.length); i++) {
let [, m, param, flag] = (route[i] || '').match(/^(:?)(.*?)([+*?]?)$/);
val = url[i];
Expand All @@ -31,7 +31,7 @@ export const exec = (url, route, matches) => {
// field match:
matches[param] = val && decodeURIComponent(val);
// normal/optional field:
if (flag >= '?') continue;
if (flag >= '?' || flag === '') continue;
// rest (+/*) match:
matches[param] = url.slice(i).map(decodeURIComponent).join('/');
break;
Expand Down

0 comments on commit f320e3e

Please sign in to comment.