Skip to content

Commit

Permalink
Added app.remove.VERB()
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Apr 26, 2011
1 parent 251175c commit b9596d7
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 5 deletions.
7 changes: 7 additions & 0 deletions lib/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ app.init = function(middleware){
var self = this;
this.cache = {};
this.match = {};
this.remove = {};
this.lookup = {};
this.settings = {};
this.redirects = {};
Expand Down Expand Up @@ -123,6 +124,12 @@ app.init = function(middleware){
: method);
};

self.remove[method] = function(url){
return self.router.remove(url, 'all' == method
? null
: method);
};

self.lookup[method] = function(path){
return self.router.lookup(path, 'all' == method
? null
Expand Down
31 changes: 26 additions & 5 deletions lib/router/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,32 @@ function router(fn){
})();
};

router.remove = function(path, method){
var fns = router.lookup(path, method);
fns.forEach(function(fn){
routes[fn.method].splice(fn.index, 1);
});
router.remove = function(path, method, ret){
var ret = ret || []
, route;

// method specific remove
if (method) {
method = method.toUpperCase();
if (routes[method]) {
for (var i = 0; i < routes[method].length; ++i) {
route = routes[method][i];
if (path == route.path) {
route.index = i;
routes[method].splice(i, 1);
ret.push(route);
--i;
}
}
}
// global remove
} else {
_methods.forEach(function(method){
router.remove(path, method, ret);
});
}

return ret;
};

router.lookup = function(path, method, ret){
Expand Down
21 changes: 21 additions & 0 deletions test/router.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,27 @@ module.exports = {
app.lookup.get('/').should.have.be.empty;
app.lookup.all('/user/:id').should.have.length(2);
},

'test app.remove': function(){
var app = express.createServer();
app.get('/user', function(){});
app.get('/user', function(){});
app.put('/user', function(){});
app.get('/user/:id', function(){});
app.put('/user/:id', function(){});
app.del('/user/:id', function(){});

app.get('/user').should.have.length(2);
var removed = app.remove.get('/user');
removed.should.have.length(2);

var removed = app.remove.get('/user');
removed.should.have.length(0);
app.get('/user').should.have.length(0);

app.remove.all('/user/:id').should.have.length(3);
app.remove.all('/user/:id').should.have.length(0);
},

'test app.match': function(){
var app = express.createServer();
Expand Down

0 comments on commit b9596d7

Please sign in to comment.