From 138396d842ac287b4c879bc1bd60b21f6c74299e Mon Sep 17 00:00:00 2001 From: webcarrot Date: Fri, 2 Sep 2016 13:41:51 +0200 Subject: [PATCH 1/5] Add support for paths that are arrays of paths --- lib/router.js | 53 +++++++++++++++++++++++++++++++-------------------- 1 file changed, 32 insertions(+), 21 deletions(-) diff --git a/lib/router.js b/lib/router.js index eb51758..d6e19d7 100644 --- a/lib/router.js +++ b/lib/router.js @@ -146,39 +146,50 @@ Route.prototype.match = function (url, options) { */ Route.prototype.makePath = function (params, query) { var routePath = this.config.path; - var compiler; var err; - var url; - var strQuery; + var i; + var len; if (Array.isArray(routePath)) { - routePath = routePath[0]; - } - - if (typeof routePath === 'string') { - compiler = cachedCompilers[routePath] || pathToRegexp.compile(routePath); - cachedCompilers[routePath] = compiler; - - try { - url = compiler(params); - if (query) { - strQuery = this._queryLib.stringify(query); - if (strQuery) { - url += '?' + strQuery; - } + for (i = 0, len = routePath.length; i < len; i++) { + try { + return this._makePath(routePath[i], params, query); + } catch (pathErr) { + err = pathErr; } - return url; - } catch (e) { - err = e; } } else { - err = new TypeError('route path must be a string:' + routePath); + try { + return this._makePath(routePath, params, query) + } catch (pathErr) { + err = pathErr; + } } debug('Route.makePath failed, e = ', err); return null; }; +Route.prototype._makePath = function(routePath, params, query) { + var compiler; + var url; + var strQuery; + if (typeof routePath === "string") { + compiler = cachedCompilers[routePath] || pathToRegexp.compile(routePath); + cachedCompilers[routePath] = compiler; + url = compiler(params); + if (query) { + strQuery = this._queryLib.stringify(query); + if (strQuery) { + url += "?" + strQuery; + } + } + return url; + } else { + throw new TypeError("route path must be a string:" + routePath); + } +} + /** * A Router class that provides route matching and route generation functionalities. * @class Router From 21fbb851e710e7a62fe9e73e5910b6d963fb3533 Mon Sep 17 00:00:00 2001 From: webcarrot Date: Fri, 2 Sep 2016 14:14:47 +0200 Subject: [PATCH 2/5] Add test --- tests/unit/lib/router.js | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/tests/unit/lib/router.js b/tests/unit/lib/router.js index 2de0892..c9e22e8 100644 --- a/tests/unit/lib/router.js +++ b/tests/unit/lib/router.js @@ -69,6 +69,12 @@ var routesObject = { method: 'GET', page: 'arrayPathNameCollision' }, + array_path_with_different_props: { + path: [ + '/array/path/with/different/props/foo/:foo', + '/array/path/with/different/props/bar/:bar' + ] + }, invalid_path: { path: 123 }, @@ -82,6 +88,8 @@ var routesArray = Object.keys(routesObject).map(function (routeName) { }); }); var encodingConsistencyPath = '/path/with/some/json_value/%7B%22keyword%22%3A%22foo%22%7D'; +var arrayPathWithDifferentPropsFoo = '/array/path/with/different/props/foo/foo'; +var arrayPathWithDifferentPropsBar = '/array/path/with/different/props/foo/bar'; describe('Router', function () { [routesObject, routesArray].forEach(function (routes, key) { @@ -292,6 +300,12 @@ describe('Router', function () { var route = router.getRoute(encodingConsistencyPath); expect(route.params.json).to.equal('{"keyword":"foo"}'); }); + it('route with array path with different props', function () { + var routeFoo = router.getRoute(arrayPathWithDifferentPropsFoo); + expect(routeFoo.params.foo).to.equal('foo'); + var routeBar = router.getRoute(arrayPathWithDifferentPropsBar); + expect(routeBar.params.foo).to.equal('bar'); + }); it('should handle a hash fragment with a question-mark', function () { var route = router.getRoute('/finance/news/test.html#?', {method: 'get'}); expect(route.name).to.equal('article'); @@ -409,6 +423,18 @@ describe('Router', function () { }); expect(path).to.equal(encodingConsistencyPath); }); + it('array path with different props', function () { + var pathFoo = router.makePath('array_path_with_different_props', { + foo: "foo" + }); + expect(pathFoo).to.equal(arrayPathWithDifferentPropsFoo); + var pathBar = router.makePath('array_path_with_different_props', { + bar: "bar" + }); + expect(pathBar).to.equal(arrayPathWithDifferentPropsBar); + var pathInvalid = router.makePath('array_path_with_different_props', {}); + expect(pathInvalid).to.equal(null); + }); }); it('should throw if route name is not defined', function () { From 59228973a28ab52226efe77b54716998ae2cb62f Mon Sep 17 00:00:00 2001 From: webcarrot Date: Fri, 2 Sep 2016 14:16:47 +0200 Subject: [PATCH 3/5] Fix typo in test --- tests/unit/lib/router.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/lib/router.js b/tests/unit/lib/router.js index c9e22e8..b663eb5 100644 --- a/tests/unit/lib/router.js +++ b/tests/unit/lib/router.js @@ -89,7 +89,7 @@ var routesArray = Object.keys(routesObject).map(function (routeName) { }); var encodingConsistencyPath = '/path/with/some/json_value/%7B%22keyword%22%3A%22foo%22%7D'; var arrayPathWithDifferentPropsFoo = '/array/path/with/different/props/foo/foo'; -var arrayPathWithDifferentPropsBar = '/array/path/with/different/props/foo/bar'; +var arrayPathWithDifferentPropsBar = '/array/path/with/different/props/bar/bar'; describe('Router', function () { [routesObject, routesArray].forEach(function (routes, key) { From 5564f462631605d4d0851c82c7c50ed06a3ea5c1 Mon Sep 17 00:00:00 2001 From: webcarrot Date: Fri, 2 Sep 2016 14:30:58 +0200 Subject: [PATCH 4/5] Last typo fix in tests? --- tests/unit/lib/router.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/unit/lib/router.js b/tests/unit/lib/router.js index b663eb5..154c2f9 100644 --- a/tests/unit/lib/router.js +++ b/tests/unit/lib/router.js @@ -304,7 +304,7 @@ describe('Router', function () { var routeFoo = router.getRoute(arrayPathWithDifferentPropsFoo); expect(routeFoo.params.foo).to.equal('foo'); var routeBar = router.getRoute(arrayPathWithDifferentPropsBar); - expect(routeBar.params.foo).to.equal('bar'); + expect(routeBar.params.bar).to.equal('bar'); }); it('should handle a hash fragment with a question-mark', function () { var route = router.getRoute('/finance/news/test.html#?', {method: 'get'}); @@ -425,11 +425,11 @@ describe('Router', function () { }); it('array path with different props', function () { var pathFoo = router.makePath('array_path_with_different_props', { - foo: "foo" + foo: 'foo' }); expect(pathFoo).to.equal(arrayPathWithDifferentPropsFoo); var pathBar = router.makePath('array_path_with_different_props', { - bar: "bar" + bar: 'bar' }); expect(pathBar).to.equal(arrayPathWithDifferentPropsBar); var pathInvalid = router.makePath('array_path_with_different_props', {}); From 5157c412d38efbf123ad74c00aa636c7374526f4 Mon Sep 17 00:00:00 2001 From: webcarrot Date: Fri, 2 Sep 2016 14:38:50 +0200 Subject: [PATCH 5/5] Use single quote --- lib/router.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/router.js b/lib/router.js index d6e19d7..9e21686 100644 --- a/lib/router.js +++ b/lib/router.js @@ -174,19 +174,19 @@ Route.prototype._makePath = function(routePath, params, query) { var compiler; var url; var strQuery; - if (typeof routePath === "string") { + if (typeof routePath === 'string') { compiler = cachedCompilers[routePath] || pathToRegexp.compile(routePath); cachedCompilers[routePath] = compiler; url = compiler(params); if (query) { strQuery = this._queryLib.stringify(query); if (strQuery) { - url += "?" + strQuery; + url += '?' + strQuery; } } return url; } else { - throw new TypeError("route path must be a string:" + routePath); + throw new TypeError('route path must be a string:' + routePath); } }