-
-
Notifications
You must be signed in to change notification settings - Fork 104
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
sjanuary
wants to merge
10
commits into
pillarjs:master
Choose a base branch
from
sjanuary:named_routes
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
6cce8fc
Add support for named routes and accompanying test
sjanuary 79a34fd
Make style/strict equals changes
sjanuary 24c893c
Ongoing work on named routes
sjanuary 711f0db
Comment out unwritten test
sjanuary fee1d39
More tests for named routes and finding a path
sjanuary c3b3e31
Fix incorrect tests and one bug
sjanuary d59097d
Improved error messages, formatting, doc and more tests
sjanuary 1d8f28d
Implement suggested changes to error messages, not allowing boxed typ…
sjanuary 1b4922c
Change order of arguments for Router.use and add info to README.md
sjanuary 78b0670
Add example with named routes
sjanuary File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -502,12 +502,16 @@ Router.prototype.use = function use(handler) { | |
* and middleware to routes. | ||
* | ||
* @param {string} path | ||
* @param {string} name (optional) | ||
* @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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. simpler to just check |
||
throw new Error('a route with that name already exists') | ||
} | ||
var route = new Route(path, name) | ||
|
||
var layer = new Layer(path, { | ||
sensitive: this.caseSensitive, | ||
|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.