Permalink
Browse files

Added builtin uuid parser

  • Loading branch information...
1 parent 834f657 commit b530aa6a4352f82e16d42b4916300c56e1be302b @tarruda committed Aug 30, 2012
Showing with 33 additions and 0 deletions.
  1. +5 −0 src/router.coffee
  2. +28 −0 test/router.coffee
View
@@ -131,6 +131,11 @@ class Compiler
if i < args.length
return args[i]
return null
+
+ uuid: (str) ->
+ if /^[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}$/i.test(str)
+ return str.toLowerCase()
+ return null
if parsers
for own k, v of parsers
View
@@ -160,6 +160,34 @@ describe 'Builtin float parser', ->
.expect(404, done)
+describe 'Builtin uuid parser', ->
+ router = createRouter()
+ app = connect()
+ app.use(router.route)
+
+ router.get '/users/<uuid:id>', (req, res) ->
+ res.write(req.params.id)
+ res.end()
+
+ it 'should match strings that are uuids', (done) ->
+ app.request()
+ .get('/users/550e8400-e29b-41d4-a716-446655440000')
+ .end (res) ->
+ res.body.should.eql('550e8400-e29b-41d4-a716-446655440000')
+ done()
+
+ it 'should not match strings that are not uuids', (done) ->
+ app.request()
+ .get('/users/550e8400-e29b-41d4-a716-44665544000')
+ .expect(404, done)
+
+ it 'should not care for uppercase letters', (done) ->
+ app.request()
+ .get('/users/550E8400-E29b-41D4-a716-446655440000')
+ .end (res) ->
+ res.body.should.eql('550e8400-e29b-41d4-a716-446655440000')
+ done()
+
describe 'Builtin integer parser', ->
router = createRouter()

0 comments on commit b530aa6

Please sign in to comment.