From c3cd82ee3111a25d4868a81362f9d9a40fad4aee Mon Sep 17 00:00:00 2001 From: Irae Date: Sat, 2 Apr 2016 21:25:40 -0700 Subject: [PATCH] Only append query if there is something to append This is mostly cosmetic, but it is really strange to have a question mark if you don't have query, and adding checks on any application using routr to not pass a query seems wasteful. i.e. in my application I use: `Object.assing({}, detaultQuery, myFuncQueryParam)` and sometimes I get an empty object. It would be convenient if the router didn't add the question mark. r.makePath('my', {wat:'route'}, {}); Before this patch: /my/route? After this patch: /my/route --- lib/router.js | 6 +++++- tests/unit/lib/router.js | 7 +++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/router.js b/lib/router.js index 03d34b5..5508ffd 100644 --- a/lib/router.js +++ b/lib/router.js @@ -149,6 +149,7 @@ Route.prototype.makePath = function (params, query) { var compiler; var err; var url; + var strQuery; if (Array.isArray(routePath)) { routePath = routePath[0]; @@ -161,7 +162,10 @@ Route.prototype.makePath = function (params, query) { try { url = compiler(params); if (query) { - url += '?' + this._queryLib.stringify(query); + strQuery = this._queryLib.stringify(query); + if (strQuery) { + url += '?' + strQuery; + } } return url; } catch (e) { diff --git a/tests/unit/lib/router.js b/tests/unit/lib/router.js index 7f83873..003b54b 100644 --- a/tests/unit/lib/router.js +++ b/tests/unit/lib/router.js @@ -364,6 +364,13 @@ describe('Router', function () { }); expect(path).to.equal('/foo/bar?baz=foo&foo=bar'); }); + it('adds no question mark with empty query', function () { + var path = router.makePath('unamed_params', { + foo: 'foo', + 0: 'bar' + }, {/* empty object */}); + expect(path).to.equal('/foo/bar'); + }); it('non-existing route', function () { var path = router.makePath('article_does_not_exist', { site: 'SITE',