Skip to content
Myr

Added preprocessing of route param wildcards using param(). Closes #251

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Async is yet to come

authored by visionmedia · committed by visionmedia · parent 9f23c7b · Browse files · 4 files+37 added−4 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+37 added−4 removed

  • tests:+27 added−2 removed
  • other:+10 added−2 removed

4 files+37 added−4 removed

modified:lib/express/spec/mocks.js+1 added
modified:lib/express/core.js+2 added-2 removed
166 unmodified lines
167 mapParams: function(route) {168 route.keys.each(function(key, i){169 var val = this.request.captures[++i]170 if (key in Express.params)171 if ((val = Express.params[key].call(val, val)) === false)171 if ((val = Express.params[key].call(this.request, val)) === false)172 this.request.passed = true173 this.request.params.path[key] = val173 this.request.params.path[key] = this.request.captures[i] = val174 }, this)175 }176})177
176 unmodified lines
modified:lib/express/dsl.js+8 added
113 unmodified lines
114 conf[1].call(Express)115 })116}117118/**119 * Pre-process param _key_ with _fn_.120 *121 * @param {string} key122 * @param {function} fn123 * @api public124 */125126exports.param = function(key, fn) {127 if (typeof key !== 'string')128 throw new TypeError('param key must be a string')129 if (typeof fn !== 'function')
10 unmodified lines
modified:spec/spec.routing.js+26 added-2 removed
34 unmodified lines
35 val = parseInt(val)36 return isNaN(val) ? false : val37 })38 get('/user/:user_id', function(id){39 require('sys').p(id)39 return String(typeof id === 'number')40 })42 get('/user/12').body.should.eql 'true'41 get('/user/99').body.should.eql 'true'42 end43 44 it 'should pass when false is explictly returned'45 param('user_id', function(val){46 val = parseInt(val)47 return isNaN(val) ? false : val48 })49 get('/user/:user_id', function(id){50 return String(typeof id === 'number')51 })52 get('/user/:name', function(name){53 return name54 })55 get('/user/99').body.should.eql 'true'56 get('/user/tj').body.should.eql 'tj'57 end58 59 it 'should evaluate in context to the request'60 param('user_id', function(val){61 this.pass()62 })63 get('/user/:user_id', function(id){64 return String(typeof id === 'number')65 })66 get('/user/99').status.should.eql 40467 end68 end69 end70 71 describe 'route'
310 unmodified lines