From 370061172b209ece41f95698209bf32c692d9e3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Janus=20T=C3=B8ndering?= Date: Mon, 23 May 2016 17:02:30 +0200 Subject: [PATCH] Fix decodeURIComponent of undefined bug decodeURIComponent(undefined) returns string 'undefined' which in turn sets all optional parameters that are not present in the url to 'undefined' instead of undefined. --- lib/router.js | 2 +- tests/unit/lib/router.js | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/router.js b/lib/router.js index 5e6ea5a..fc0caee 100644 --- a/lib/router.js +++ b/lib/router.js @@ -118,7 +118,7 @@ Route.prototype.match = function (url, options) { // Don't overwrite a previously populated parameter with `undefined`. // A route may legitimately have multiple instances of a parameter // name if the path was an array. - if (pathMatches[i+1] !== undefined || routeParams[self.keys[i].name] === undefined){ + if (pathMatches[i+1] !== undefined && routeParams[self.keys[i].name] === undefined){ // Because pathToRegexp encodeURIComponent params values, it is necessary // to decode when reading from URL routeParams[self.keys[i].name] = decodeURIComponent(pathMatches[i+1]); diff --git a/tests/unit/lib/router.js b/tests/unit/lib/router.js index 7268c9b..2493743 100644 --- a/tests/unit/lib/router.js +++ b/tests/unit/lib/router.js @@ -474,5 +474,12 @@ describe('Route', function () { var homeRoute = router._routes.home; expect(homeRoute.match()).to.equal(null, 'empty path returns null'); }); + it('should leave unset optional parameters as undefined', function(){ + var router = new Router(routesObject); + var article = router._routes.article; + var result = article.match('/site/alias'); + expect(result.route.category).to.be.undefined; + expect(result.route.subcategory).to.be.undefined; + }); });