millermedeiros/crossroads.js — pull request #74 by nathanboktae (2012-12-09 01:14:25+00:00)
Title: Named Optional Parameters

### The Problem

When there are a series of multiple optional parameters in a route, like these:

/used-cars/:year:/:make:/:model:
/recipes/:search-term:/:with-ingredients:

Crossroads cannot treat these optional parameters as fully independent, otherwise routes would be ambiguous. For the first route, constraints on what year, make and model could be possible, but in the 2nd it is not as the domains of their values overlap

### The Solution

Create optional parameters that also match an identifier so that it can disambiguate exactly what the value is intended to map to. So the previous routes can be written like

/used-cars/|year|/|make|/|model|
/recipes/|search-term|/|with-ingredients|

and will match routes like:

/used-cars/make/ford
/used-cars/year/2005
/used-cars/make/toyota/model/tundra
/recipes/with-ingredients/cheddar
/recipes/search/pasta/with-ingredients/cheddar

This scenario is quite common in when building a filter upon a resource.

I also cleaned up `parse.spec.js` a bit as some of the tests could have resulted in false positives if the matching signal callback was called twice, and the first was wrong but the 2nd call was correct.