millermedeiros / crossroads.js

JavaScript Routes
http://millermedeiros.github.com/crossroads.js/
1.44k stars 156 forks source link

Multiple optional Parameters #114

Open TobiasOetzel opened 10 years ago

TobiasOetzel commented 10 years ago

Hi,

if you define a pattern with 3 optional parameters and you supply the 1st and the 3rd the order is mixed up. eg : /:lorem:/:ipsum:/:dolor: matched against /lorem//dolor will map lorem to lorem, ipsum to dolor and dolor to undefined.

i would expect ipsum to be undefined and dolor to be dolor. Although using a :?parameter: would make more sense if you have 3 optional ones.

i wrote a test in the lexer.spec.js that fails:

    it('should return optional pattern params', function(){
        var pattern = '/:lorem:/:ipsum:/:dolor:',
            regex = crossroads.patternLexer.compilePattern(pattern),
            params = crossroads.patternLexer.getParamValues('/lorem//dolor', regex);

        expect( params[0] ).toEqual( 'lorem' );
        expect( params[1] ).toEqual( undefined );
        expect( params[2] ).toEqual( 'dolor' );
    });

but i did not come up with a fix yet.

Best regards, Tobias

corintho commented 9 years ago

Not sure how this is going to be handled here, but on every other routing framework I've seen out there, any optional path parameters were only allowed in the end of the URL.