Skip to content

Commit

Permalink
Remove app.param(fn) signature
Browse files Browse the repository at this point in the history
  • Loading branch information
dougwilson committed Jul 6, 2015
1 parent 3a1f27f commit 1e2951a
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 60 deletions.
2 changes: 2 additions & 0 deletions History.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

This incorporates all changes after 4.10.1 up to 4.13.1.

* remove:
- `app.param(fn)`
* change:
- The leading `:` character in `name` for `app.param(name, fn)` is no longer removed

Expand Down
30 changes: 10 additions & 20 deletions lib/router/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ var proto = module.exports = function(options) {
router.__proto__ = proto;

router.params = {};
router._params = [];
router.caseSensitive = opts.caseSensitive;
router.mergeParams = opts.mergeParams;
router.strict = opts.strict;
Expand Down Expand Up @@ -94,31 +93,22 @@ var proto = module.exports = function(options) {
*/

proto.param = function param(name, fn) {
// param logic
if (typeof name === 'function') {
deprecate('router.param(fn): Refactor to use path params');
this._params.push(name);
return;
if (!fn) {
throw new TypeError('argument fn is required');
}

// apply param functions
var params = this._params;
var len = params.length;
var ret;

for (var i = 0; i < len; ++i) {
if (ret = params[i](name, fn)) {
fn = ret;
}
if (typeof fn !== 'function') {
throw new TypeError('argument fn must be a function');
}

// ensure we end up with a
// middleware function
if ('function' != typeof fn) {
throw new Error('invalid param() call for ' + name + ', got ' + fn);
var params = this.params[name];

if (!params) {
params = this.params[name] = [];
}

(this.params[name] = this.params[name] || []).push(fn);
params.push(fn);

return this;
};

Expand Down
10 changes: 10 additions & 0 deletions test/Router.js
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,16 @@ describe('Router', function(){
})

describe('.param', function() {
it('should require function', function () {
var router = new Router();
assert.throws(router.param.bind(router, 'id'), /argument fn is required/);
});

it('should reject non-function', function () {
var router = new Router();
assert.throws(router.param.bind(router, 'id', 42), /argument fn must be a function/);
});

it('should call param function when routing VERBS', function(done) {
var router = new Router();

Expand Down
40 changes: 0 additions & 40 deletions test/app.param.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,6 @@ var express = require('../')
, request = require('supertest');

describe('app', function(){
describe('.param(fn)', function(){
it('should map app.param(name, ...) logic', function(done){
var app = express();

app.param(function(name, regexp){
if (Object.prototype.toString.call(regexp) == '[object RegExp]') { // See #1557
return function(req, res, next, val){
var captures;
if (captures = regexp.exec(String(val))) {
req.params[name] = captures[1];
next();
} else {
next('route');
}
}
}
})

app.param('name', /^([a-zA-Z]+)$/);

app.get('/user/:name', function(req, res){
res.send(req.params.name);
});

request(app)
.get('/user/tj')
.expect(200, 'tj', function (err) {
if (err) return done(err);
request(app)
.get('/user/123')
.expect(404, done);
});
})

it('should fail if not given fn', function(){
var app = express();
app.param.bind(app, 'name', 'bob').should.throw();
})
})

describe('.param(names, fn)', function(){
it('should map the array', function(done){
var app = express();
Expand Down

0 comments on commit 1e2951a

Please sign in to comment.