diff --git a/compileRoute.js b/compileRoute.js index 1e11324..6e0b474 100644 --- a/compileRoute.js +++ b/compileRoute.js @@ -34,8 +34,8 @@ function compileRoute (route, options) { for (var i = 1; i < match.length; ++i) { var key = keys[i - 1] - var value = decodeURIComponent(match[i]) - if (value[0] === ':') { + var value = match[i] && decodeURIComponent(match[i]) + if (value && value[0] === ':') { result[key] = URLON.parse(value) } else { result[key] = value diff --git a/tests/test.js b/tests/test.js index 3af7f78..a526184 100644 --- a/tests/test.js +++ b/tests/test.js @@ -388,6 +388,17 @@ module.exports = { test.equal(mapper.stringify('/:foo', { foo: 'bar', bar: 'baz' }), '/bar#@bar=baz') test.deepEqual(mapper.parse('/:foo', '/bar#@bar=baz'), { foo: 'bar', bar: 'baz' }) + test.done() + }, + + 'should parse optional paramters as undefined': function (test) { + var mapper = urlMapper() + + test.deepEqual(mapper.parse('/:foo/:bar?', '/bar/'), { + foo: 'bar', + bar: undefined + }) + test.done() } }