Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
...
Checking mergeability… Don't worry, you can still create the pull request.
  • 7 commits
  • 4 files changed
  • 0 commit comments
  • 1 contributor
View
2  .gitmodules
@@ -1,3 +1,3 @@
[submodule "deps/nodeunit"]
path = deps/nodeunit
- url = git://github.com/caolan/nodeunit.git
+ url = http://github.com/caolan/nodeunit.git
View
12 README.md
@@ -1,5 +1,13 @@
# Dispatch
+## Fork Changes ##
+
+This fork of dispatch has been changed to pass through the _next_ function as the third parameter, pushing matched groups from the route to starting at the fourth param.
+
+Second and third optional parameters (neither or both required) have been added to dispatch(). _condition_ is a function of the form function(req){}. It is executed if a routing match is found. If it is present and returns true, then the dispatch action is performed. If it is present and returns false then the _fallback_ parameter (of the form function(req,res,next){}) is called.
+
+## End Fork Changes ##
+
A really simple URL dispatcher for
[Connect](http://github.com/senchalabs/connect). Allows arbitrarily nested
regular expressions for matching URLs and calling an associated function.
@@ -18,7 +26,7 @@ regular expressions for matching URLs and calling an associated function.
'/user/posts': function(req, res){
...
},
- '/user/posts/(\\w+)': function(req, res, post){
+ '/user/posts/(\\w+)': function(req, res, next, post){
...
}
})
@@ -37,7 +45,7 @@ you see fit:
'/user': {
'/': function(req, res){ ... },
'/posts': function(req, res){ ... },
- '/posts/(\\w+)': function(req, res, post){ ... }
+ '/posts/(\\w+)': function(req, res, next, post){ ... }
}
})
);
View
10 lib/dispatch.js
@@ -36,12 +36,18 @@ function compileKeys(urls){
* The exported function for use as a Connect provider.
* See test/test-dispatch.js for example usage.
*/
-module.exports = function(urls){
+module.exports = function(urls, condition, fallback){
var compiled = compileKeys(flattenKeys(urls));
return function(req, res, next){
if(!compiled.some(function(x){
var match = x[0].exec(url.parse(req.url).pathname);
- if(match) x[1].apply(null, [req, res].concat(match.slice(1)));
+ if(match) {
+ if (!condition || condition(req)) {
+ x[1].apply(null, [req, res, next].concat(match.slice(1)));
+ } else {
+ fallback(req,res,next);
+ }
+ }
return match;
})) next();
};
View
70 test/test-dispatch.js
@@ -1,12 +1,13 @@
var dispatch = require('dispatch');
exports['simple match'] = function(test){
- test.expect(2);
+ test.expect(3);
var request = {url: '/test'};
dispatch({
- '/test': function(req, res){
+ '/test': function(req, res, next){
test.equals(req, request);
test.equals(res, 'response');
+ test.equals(next, 'next');
test.done();
}
})(request, 'response', 'next');
@@ -27,9 +28,10 @@ exports['no match'] = function(test){
exports['regexp match'] = function(test){
var request = {url: '/abc/test123'};
dispatch({
- '/(\\w+)/test\\d*': function(req, res, group){
+ '/(\\w+)/test\\d*': function(req, res, next, group){
test.equals(req, request);
test.equals(res, 'response');
+ test.equals(next, 'next');
test.equals(group, 'abc');
test.done();
}
@@ -37,12 +39,13 @@ exports['regexp match'] = function(test){
};
exports['multiple matches'] = function(test){
- test.expect(3);
+ test.expect(4);
var request = {url: '/abc'};
dispatch({
- '/(\\w+)/?': function(req, res, group){
+ '/(\\w+)/?': function(req, res, next, group){
test.equals(req, request);
test.equals(res, 'response');
+ test.equals(next, 'next');
test.equals(group, 'abc');
},
'/(\\w+)': function(req, res, group){
@@ -57,9 +60,10 @@ exports['nested urls'] = function(test){
dispatch({
'/folder': {
'/some/other': {
- '/path': function(req, res){
+ '/path': function(req, res, next){
test.equals(req, request);
test.equals(res, 'response');
+ test.equals(next, 'next');
test.done();
}
}
@@ -72,9 +76,10 @@ exports['nested urls with captured groups'] = function(test){
dispatch({
'/(\\w+)': {
'/(\\w+)': {
- '/(\\w+)': function(req, res, group1, group2, group3){
+ '/(\\w+)': function(req, res, next, group1, group2, group3){
test.equals(req, request);
test.equals(res, 'response');
+ test.equals(next, 'next');
test.equals(group1, 'one');
test.equals(group2, 'two');
test.equals(group3, 'three');
@@ -88,3 +93,54 @@ exports['nested urls with captured groups'] = function(test){
}
})(request, 'response', 'next');
};
+
+exports['passed condition'] = function(test){
+ test.expect(1);
+ var request = {url: '/test', name:'pass'};
+ dispatch({
+ '/test': function(req, res, next){
+ test.ok(true);
+ test.done();
+ }
+ },
+ function(req){return req.name == 'pass';},
+ function(req, res, next){
+ test.ok(false);
+ test.done();
+ }
+ )(request, 'response', 'next');
+};
+
+exports['failed condition'] = function(test){
+ test.expect(0);
+ var request = {url: '/test', name:'fail'};
+ dispatch({
+ '/test': function(req, res, next){
+ test.ok(false);
+ test.done();
+ }
+ },
+ function(req){return req.name == 'pass';},
+ function(){test.done()}
+ )(request, 'response');
+};
+
+
+exports['condition but no match'] = function(test){
+ test.expect(0);
+ var request = {url: '/nomatch', name:'fail'};
+ dispatch({
+ '/test': function(req, res, next){
+ test.ok(false);
+ test.done();
+ }
+ },
+ function(req){return req.name == 'pass';},
+ function(){
+ test.ok(false);
+ test.done();
+ }
+ )(request, 'response', function(req,res,next) {
+ test.done();
+ });
+};

No commit comments for this range

Something went wrong with that request. Please try again.