Skip to content

Commit

Permalink
[resolves #20] Add support for parsing and constructing urls with que…
Browse files Browse the repository at this point in the history
…ry params
  • Loading branch information
Michael Ridgway committed Mar 16, 2016
1 parent 21a01a6 commit 6bea56e
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 8 deletions.
32 changes: 25 additions & 7 deletions lib/router.js
Expand Up @@ -7,6 +7,7 @@

var debug = require('debug')('Routr:router');
var pathToRegexp = require('path-to-regexp');
var queryString = require('query-string');
var METHODS = {
GET: 'get'
};
Expand Down Expand Up @@ -101,20 +102,31 @@ Route.prototype.match = function (url, options) {
}
}

return routeParams;
// 4. query params
var queryParams = {};
if (-1 !== pos) {
queryParams = queryString.parse(url.substring(pos));
}

return {
route: routeParams,
query: queryParams
};
};

/**
* Generates a path string with this route, using the specified params.
* @method makePath
* @param {Object} params The route parameters to be used to create the path string
* @param {Object} query The query parameters to be used to create the path string
* @return {String} The generated path string.
* @for Route
*/
Route.prototype.makePath = function (params) {
Route.prototype.makePath = function (params, query) {
var routePath = this.config.path;
var compiler;
var err;
var url;

if (Array.isArray(routePath)) {
routePath = routePath[0];
Expand All @@ -125,7 +137,11 @@ Route.prototype.makePath = function (params) {
cachedCompilers[routePath] = compiler;

try {
return compiler(params);
url = compiler(params);
if (query) {
url += '?' + queryString.stringify(query);
}
return url;
} catch (e) {
err = e;
}
Expand Down Expand Up @@ -227,8 +243,9 @@ Router.prototype.getRoute = function (url, options) {
return {
name: keys[i],
url: url,
params: match,
config: route.config
params: match.route,
config: route.config,
query: match.query
};
}
}
Expand All @@ -240,10 +257,11 @@ Router.prototype.getRoute = function (url, options) {
* @method makePath
* @param {String} name The route name
* @param {Object} params The route parameters to be used to create the path string
* @param {Object} query The query parameters to be used to create the path string
* @return {String} The generated path string, null if there is no route with the given name.
*/
Router.prototype.makePath = function (name, params) {
return (name && this._routes[name] && this._routes[name].makePath(params)) || null;
Router.prototype.makePath = function (name, params, query) {
return (name && this._routes[name] && this._routes[name].makePath(params, query)) || null;
};

module.exports = Router;
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -21,7 +21,8 @@
],
"dependencies": {
"debug": "^2.0.0",
"path-to-regexp": "^1.1.1"
"path-to-regexp": "^1.1.1",
"query-string": "^3.0.1"
},
"devDependencies": {
"babel-polyfill": "^6.7.2",
Expand Down
12 changes: 12 additions & 0 deletions tests/unit/lib/router.js
Expand Up @@ -228,6 +228,8 @@ describe('Router', function () {

route = router.getRoute('/new_article?foo=bar', {method: 'post'});
expect(route.name).to.equal('new_article');
expect(route.params).to.deep.equal({});
expect(route.query).to.deep.equal({foo: 'bar'});
});

it('method should be case-insensitive and defaults to get', function () {
Expand Down Expand Up @@ -302,6 +304,16 @@ describe('Router', function () {
});
expect(path).to.equal('/foo/bar');
});
it('handle query params', function () {
var path = router.makePath('unamed_params', {
foo: 'foo',
0: 'bar'
}, {
foo: 'bar',
baz: 'foo'
});
expect(path).to.equal('/foo/bar?baz=foo&foo=bar');
});
it('non-existing route', function () {
var path = router.makePath('article_does_not_exist', {
site: 'SITE',
Expand Down

0 comments on commit 6bea56e

Please sign in to comment.