Skip to content

Commit

Permalink
Added builtin uuid parser
Browse files Browse the repository at this point in the history
  • Loading branch information
tarruda committed Aug 30, 2012
1 parent 834f657 commit b530aa6
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/router.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 28 additions & 0 deletions test/router.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit b530aa6

Please sign in to comment.