Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Implemented 'in' parser

  • Loading branch information...
commit 9dae412329682c3ebcb9706be1d0d29849972b7a 1 parent c787880
Thiago de Arruda authored

Showing 2 changed files with 61 additions and 12 deletions. Show diff stats Hide diff stats

  1. +28 12 src/router.coffee
  2. +33 0 test/router.coffee
40 src/router.coffee
@@ -7,7 +7,7 @@ escapeRegex = (s) -> s.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&')
7 7
8 8 absoluteUrl = (req, pathname, search) ->
9 9 protocol = 'http'
10   - if req.headers['X-Forwarded-Protocol'] == 'https'
  10 + if req.protocol == 'https'
11 11 protocol = 'https'
12 12 rv = [protocol, '://', req.headers.host]
13 13 if req.port
@@ -37,7 +37,7 @@ class RegexExtractor
37 37
38 38 extract: (requestPath) ->
39 39 m = @regex.exec(requestPath)
40   - if !m then return null
  40 + if ! m then return null
41 41 return m.slice(1)
42 42
43 43 test: (requestPath) -> @extract(requestPath) != null
@@ -66,7 +66,7 @@ class RuleExtractor extends RegexExtractor
66 66
67 67 extract: (requestPath) ->
68 68 m = @regex.exec(requestPath)
69   - if !m then return null
  69 + if ! m then return null
70 70 params = @params
71 71 parsers = @parsers
72 72 extractedArgs = []
@@ -98,7 +98,7 @@ class Compiler
98 98 if opts?.base
99 99 base = opts.base
100 100 rv = parseInt(str, base)
101   - if !isFinite(rv) || rv.toString(base) != str
  101 + if ! isFinite(rv) || rv.toString(base) != str
102 102 return null
103 103 if opts
104 104 if (isFinite(opts.min) && rv < opts.min) ||
@@ -109,7 +109,7 @@ class Compiler
109 109 float: (str, opts) ->
110 110 str = str.trim()
111 111 rv = parseFloat(str)
112   - if !isFinite(rv) || rv.toString() != str
  112 + if ! isFinite(rv) || rv.toString() != str
113 113 return null
114 114 if opts
115 115 if (isFinite(opts.min) && rv < opts.min) ||
@@ -120,6 +120,15 @@ class Compiler
120 120 str: (str, opts) -> defaultParser(str, opts)
121 121
122 122 path: (str) -> str
  123 +
  124 + in: (str, opts) ->
  125 + args = opts['*args']
  126 + for i in [0...args.length]
  127 + if args[i].toString() == str then break
  128 + if i < args.length
  129 + return args[i]
  130 + return null
  131 +
123 132 if parsers
124 133 for own k, v in parsers
125 134 @parsers[k] = v
@@ -142,8 +151,10 @@ class Compiler
142 151
143 152 parserOptRe:
144 153 ///
145   - ([a-zA-Z_][a-zA-Z0-9_]*) # Capture option name
146   - \s*=\s* # Delimiters
  154 + (?:
  155 + ([a-zA-Z_][a-zA-Z0-9_]*) # Capture option name
  156 + \s*=\s* # Delimiters
  157 + )?
147 158 (?:
148 159 (true|false) # Capture boolean literal
149 160 | # OR
@@ -154,15 +165,20 @@ class Compiler
154 165 ///g
155 166
156 167 parseOpts: (rawOpts) ->
157   - rv = {}
  168 + rv =
  169 + '*args': []
158 170 while match = @parserOptRe.exec(rawOpts)
159   - name = match[1]
  171 + name = null
  172 + if match[1]
  173 + name = match[1]
160 174 if match[2] # boolean
161   - rv[name] = Boolean(match[2])
  175 + value = Boolean(match[2])
162 176 else if match[3] # number
163   - rv[name] = parseFloat(match[3])
  177 + value = parseFloat(match[3])
164 178 else # string
165   - rv[name] = match[4]
  179 + value = match[4]
  180 + if name then rv[name] = value # Named argument
  181 + else rv['*args'].push(value) # Unamed argument
166 182 return rv
167 183
168 184 compile: (pattern) ->
33 test/router.coffee
@@ -180,6 +180,39 @@ describe 'Builtin integer parser', ->
180 180 .expect(404, done)
181 181
182 182
  183 +describe "Builtin 'in' parser", ->
  184 + router = createRouter()
  185 + app = connect()
  186 + app.use(router.route)
  187 +
  188 + router.get '/posts/<in(t1,t2,t3,true,5.2):tag>', (req, res) ->
  189 + res.write(JSON.stringify(req.params.tag))
  190 + res.end()
  191 +
  192 + it 'should match if arg is inside list', (done) ->
  193 + app.request()
  194 + .get('/posts/t1')
  195 + .end (res) ->
  196 + JSON.parse(res.body).should.eql('t1')
  197 + done()
  198 +
  199 + it 'should not match if arg is outside list', (done) ->
  200 + app.request()
  201 + .get('/posts/1')
  202 + .expect(404, done)
  203 +
  204 + it 'should convert value if needed', (done) ->
  205 + app.request()
  206 + .get('/posts/true')
  207 + .end (res) ->
  208 + JSON.parse(res.body).should.eql(true)
  209 + app.request()
  210 + .get('/posts/5.2')
  211 + .end (res) ->
  212 + JSON.parse(res.body).should.eql(5.2)
  213 + done()
  214 +
  215 +
183 216 describe 'Accessing branch urls without trailing slash', ->
184 217 router = createRouter()
185 218 app = connect()

0 comments on commit 9dae412

Please sign in to comment.
Something went wrong with that request. Please try again.