diff --git a/src/router.js b/src/router.js index b999cce..8026b96 100644 --- a/src/router.js +++ b/src/router.js @@ -65,7 +65,7 @@ export const exec = (url, route, matches) => { if (!m || (!val && flag != '?' && flag != '*')) return; rest = flag == '+' || flag == '*'; // rest (+/*) match: - if (rest) val = url.slice(i).map(decodeURIComponent).join('/'); + if (rest) val = url.slice(i).map(decodeURIComponent).join('/') || undefined; // normal/optional field: else if (val) val = decodeURIComponent(val); matches.params[param] = val; diff --git a/test/node/router-match.test.js b/test/node/router-match.test.js index d3bc09b..100377c 100644 --- a/test/node/router-match.test.js +++ b/test/node/router-match.test.js @@ -58,10 +58,26 @@ test('Optional param route', () => { }); test('Optional rest param route "/:x*"', () => { - const accurateResult = execPath('/user', '/user/:id?'); - assert.equal(accurateResult, { path: '/user', params: { id: undefined }, id: undefined, query: {} }); + const matchedResult = execPath('/user', '/user/:id*'); + assert.equal(matchedResult, { path: '/user', params: { id: undefined }, id: undefined, query: {} }); - const inaccurateResult = execPath('/', '/user/:id?'); + const matchedResultWithSlash = execPath('/user/foo/bar', '/user/:id*'); + assert.equal(matchedResultWithSlash, { + path: '/user/foo/bar', + params: { id: 'foo/bar' }, + id: 'foo/bar', + query: {} + }); + + const emptyResult = execPath('/user', '/user/:id*'); + assert.equal(emptyResult, { + path: '/user', + params: { id: undefined }, + id: undefined, + query: {} + }); + + const inaccurateResult = execPath('/', '/user/:id*'); assert.equal(inaccurateResult, undefined); });