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 = require('lib/routematcher').routeMatcher;
var myRoute = routeMatcher("user/:id");
Or in the browser:
<script src="dist/ba-routematcher.min.js"></script>
<script>
var myRoute = routeMatcher("user/:id");
</script>
In the browser, you can attach routeMatcher to any object.
<script>
this.exports = Bocoup.utils;
</script>
<script src="dist/ba-routematcher.min.js"></script>
<script>
var myRoute = Bocoup.utils.routeMatcher("user/:id");
</script>
Sample Usage
var search = routeMatcher("search/:query/p:page");
search.parse("search/gonna-fail")
search.parse("search/cowboy/p5")
search.parse("search/gnarf/p10")
search.stringify({query: "bonus", page: "6"})
var user = routeMatcher("user/:id/:other", {
id: /^\d+$/,
other: function(value) { return value === "" || value === "foo"; }
});
user.parse("user/123/abc")
user.parse("user/foo/")
user.parse("user/123/")
user.parse("user/123/foo")
user.stringify({id: "abc", other: "xyz"})
var users = routeMatcher(/^(users?)(?:\/(\d+)(?:\.\.(\d+))?)?/);
users.parse("gonna-fail")
users.parse("user")
users.parse("users")
users.parse("user/123")
users.parse("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/