fix for issue #11

get the keys by a simple regexp
pull/12/head
Tobias Peters 10 years ago
parent bc9dff9d4c
commit 3fa59e012a

@ -18,25 +18,26 @@ var optionalParam = /\((.*?)\)/g;
var namedParam = /(\(\?)?:\w+/g;
var splatParam = /\*\w+/g;
var escapeRegExp = /[\-{}\[\]+?.,\\\^$|#\s]/g;
var paramKeys = /[*:]\w+/g;
// Convert a route string into a regular expression,
// with named regular expressions for named arguments.
// http://backbonejs.org/docs/backbone.html#section-165
function routeToRegExp(src) {
var keys = [];
var keys = [], match;
while ( ( match = paramKeys.exec( src ) ) !== null )
{
keys.push( match[0] );
}
var route = src.replace(escapeRegExp, '\\$&')
.replace(optionalParam, '(?:$1)?')
.replace(namedParam, function(match, optional) {
keys.push(match);
return optional ? match : '([^/?]+)';
})
.replace(splatParam, function(match) {
keys.push(match);
return '([^?]*?)';
});
.replace(splatParam, '([^?]*?)');
keys = keys.reduce(function(memo, key) {
var value = '\\' + key;

@ -0,0 +1,12 @@
var test = require('tap').test;
var docuri = require('..');
test('splat and named parameter', function(t) {
docuri.route('*splat/page/:named', 'page');
t.deepEqual(docuri.page('splat/page/named'), { splat: [ 'splat' ], named: 'named' }, 'splat and named parameter parsed correctly');
t.equal(docuri.page({ splat: [ 'splat' ], named: 'named' }), 'splat/page/named', 'stringified result is in right order');
t.end();
});
Loading…
Cancel
Save