JavaScript Basic Route Matcher
A simple route matching / url building utility. Intended to be included as part of a larger routing library.
Getting Started
This code should work just fine in Node.js:
var routeMatcher = routeMatcher;var myRoute = ;
Or in the browser:
In the browser, you can attach routeMatcher to any object.
Sample Usage
// Use routeMatcher to create a reusable route matching function.var search = ;search // null (no match)search // {query: "cowboy", page: "5"}search // {query: "gnarf", page: "10"} // But wait, it goes both ways!search // "search/bonus/p6" // You can also pass in a map of per-param validators after the route, each can// be a RegExp to test against, function that accepts a value (and returns true// or false) or value to match against.var user = ;user // null (no match)user // null (no match)user // {id: "123", other: ""}user // {id: "123", other: "foo"} // Note that .stringify doesn't perform any validation. Should it?user // "user/abc/xyz" // You can pass in a RegExp route, which returns an object with a `captures`// property, or null if no match. Note that for RegExp routes, the .stringify// method always returns empty string, because stringification isn't supported.var users = ;users // null (no match)users // {captures: ["user", undefined, undefined]}users // {captures: ["users", undefined, undefined]}users // {captures: ["user", "123", undefined]}users // {captures: ["user", "123", "456"]}
Documentation
For now, look at the unit tests.
Contributing
In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using grunt.
Also, please don't edit files in the "dist" subdirectory as they are generated via grunt. You'll find source code in the "lib" subdirectory!
Release History
Nothing official yet...
License
Copyright (c) 2011 "Cowboy" Ben Alman
Dual licensed under the MIT and GPL licenses.
http://benalman.com/about/license/