Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for named routes #36

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
29 changes: 27 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -502,12 +502,16 @@ Router.prototype.use = function use(handler) {
* and middleware to routes.
*
* @param {string} path
* @param {string} name (optional)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than making the argument's description "(optional)", can we use the JSDoc features to make it as optional? Probably a better IDE experience.

* @return {Route}
* @public
*/

Router.prototype.route = function route(path) {
var route = new Route(path)
Router.prototype.route = function route(path, name) {
if(arguments.length > 1 && this.findRoute(name) != null) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

simpler to just check if (name && .... IMO that is also more explicit about the intention of this code.

throw new Error('a route with that name already exists')
}
var route = new Route(path, name)

var layer = new Layer(path, {
sensitive: this.caseSensitive,
Expand All @@ -525,6 +529,27 @@ Router.prototype.route = function route(path) {
return route
}


/**
* Find a Route with the given name
*
* @param {string} name
* @return {Route} or null if the route does not exist
* @public
*/

Router.prototype.findRoute = function findRoute(name) {
var index = 0;
while (index < this.stack.length) {
var layer = this.stack[index++]
var route = layer.route
if(route.name != undefined && route.name === name) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should use strict equals wherever possible.

return route
}
}
return null
}

// create Router#VERB functions
methods.concat('all').forEach(function(method){
Router.prototype[method] = function (path) {
Expand Down
6 changes: 4 additions & 2 deletions lib/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,17 @@ var slice = Array.prototype.slice
module.exports = Route

/**
* Initialize `Route` with the given `path`,
* Initialize `Route` with the given `path` and `name`,
*
* @param {String} path
* @param {String} name (optional)
* @api private
*/

function Route(path) {
function Route(path, name) {
debug('new %s', path)
this.path = path
this.name = name
this.stack = []

// route handlers for various http methods
Expand Down
13 changes: 13 additions & 0 deletions test/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,19 @@ describe('Router', function () {
assert.equal(route.path, '/foo')
})

it('should set the route name iff provided', function () {
var router = new Router()
var route = router.route('/abc', 'abcRoute')
assert.equal(route.path, '/abc')
assert.equal(route.name, 'abcRoute')
assert.equal(router.findRoute('abcRoute'), route)
assert.throws(router.route.bind(router, '/xyz', 'abcRoute'), /a route with that name already exists/)

var route2 = router.route('/def')
assert.equal(router.findRoute('abcRoute'), route)
assert.equal(null, router.findRoute(undefined))
})

it('should respond to multiple methods', function (done) {
var cb = after(3, done)
var router = new Router()
Expand Down