diff --git a/README.md b/README.md index 7599a99..9c54bb2 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,33 @@ -# Plugin? +# JavaScript Basic Route Matcher +A simple route matching utility. Intended to be included as part of a larger routing library. -JUST SOME STUFF MAYBE FOR BBQ MAYBE NOT +## Sample Usage +```javascript +// You can use routeMatcher to match just once. +routeMatcher("search/:query/p:page", "search/boston/p20"); // {query: "boston", page: "20"} -WORK IN PROGRESS DONT USE YET KTHX +// Or you can use routeMatcher to create a reusable route matching function. +var matchRoute = routeMatcher("search/:query/p:page"); +matchRoute("search/gonna-fail") // null +matchRoute("search/cowboy/p5") // {query: "cowboy", page: "5"} +matchRoute("search/gnarf/p10") // {query: "gnarf", page: "10"} -NOT EVEN USING JQUERY YET MAYBE IT NEVER WILL +// You can pass in a RegExp route as well. +var matchRoute = routeMatcher(/^users?(?:\/(\d+)(?:\.\.(\d+))?)?/); +matchRoute("gonna-fail" // null (no match) +matchRoute("user") // ["user", undefined, undefined] +matchRoute("users") // ["users", undefined, undefined] +matchRoute("user/123"); // ["user/123", "123", undefined] +matchRoute("user/123..456"); // ["user/123..456", "123", "456"] +``` -I LOVE COOKIES +## Documentation +Look at the [unit tests](unit/ba-routematcher.js). +## Release History +Nothing official yet... + +## License +Copyright (c) 2011 "Cowboy" Ben Alman +Dual licensed under the MIT and GPL licenses. + diff --git a/jquery.ba-route.js b/ba-routematcher.js similarity index 97% rename from jquery.ba-route.js rename to ba-routematcher.js index 0b2116a..e386190 100644 --- a/jquery.ba-route.js +++ b/ba-routematcher.js @@ -13,7 +13,7 @@ // Match :xxx or *xxx param placeholders. var reParam = /([:*])(\w+)/g; - global.getRoute = function(route, url) { + global.routeMatcher = function(route, url) { // Param names, in order var names = []; // The route parsing function to be returned (or invoked if a url was diff --git a/unit/jquery.ba-route.js b/unit/ba-routematcher.js similarity index 80% rename from unit/jquery.ba-route.js rename to unit/ba-routematcher.js index b0f9d80..d0b97a0 100644 --- a/unit/jquery.ba-route.js +++ b/unit/ba-routematcher.js @@ -1,18 +1,18 @@ test("called with url", function() { - same(getRoute("users", "foo"), null, "shouldn't match"); - same(getRoute("users", "users"), {}, "should match"); + same(routeMatcher("users", "foo"), null, "shouldn't match"); + same(routeMatcher("users", "users"), {}, "should match"); }); test("called without url", function() { - var r = getRoute("users"); + var r = routeMatcher("users"); same(typeof r, "function", "should return a function"); same(r("foo"), null, "shouldn't match"); same(r("users"), {}, "should match"); }); test("regex route", function() { - var r = getRoute(/^users?(?:\/(\d+)(?:\.\.(\d+))?)?/); + var r = routeMatcher(/^users?(?:\/(\d+)(?:\.\.(\d+))?)?/); same(r("foo"), null, "shouldn't match"); same(r("user"), ["user", undefined, undefined], "should match"); same(r("user/123"), ["user/123", "123", undefined], "should match"); @@ -20,7 +20,7 @@ test("regex route", function() { }); test("string route, basic", function() { - var r = getRoute("users"); + var r = routeMatcher("users"); same(r("fail"), null, "shouldn't match"); same(r("users/"), null, "shouldn't match"); same(r("users/foo"), null, "shouldn't match"); @@ -28,7 +28,7 @@ test("string route, basic", function() { }); test("string route, one variable", function() { - var r = getRoute("users/:id"); + var r = routeMatcher("users/:id"); same(r("users"), null, "shouldn't match"); same(r("users/123/456"), null, "shouldn't match"); same(r("users/"), {id: ""}, "should match"); @@ -36,14 +36,14 @@ test("string route, one variable", function() { }); test("string route, multiple variables", function() { - var r = getRoute("users/:id/:other"); + var r = routeMatcher("users/:id/:other"); same(r("users"), null, "shouldn't match"); same(r("users/123"), null, "shouldn't match"); same(r("users/123/456"), {id: "123", other: "456"}, "should match"); }); test("string route, one splat", function() { - var r = getRoute("users/*stuff"); + var r = routeMatcher("users/*stuff"); same(r("users"), null, "shouldn't match"); same(r("users/"), {stuff: ""}, "should match"); same(r("users/123"), {stuff: "123"}, "should match"); @@ -51,7 +51,7 @@ test("string route, one splat", function() { }); test("string route, multiple splats", function() { - var r = getRoute("users/*stuff/*more"); + var r = routeMatcher("users/*stuff/*more"); same(r("users"), null, "shouldn't match"); same(r("users/123"), null, "shouldn't match"); same(r("users/123/"), {stuff: "123", more: ""}, "should match"); @@ -63,24 +63,24 @@ test("string route, multiple splats", function() { }); test("string route, variables and splats", function() { - var r = getRoute("users/:id/*stuff/:other/*more"); + var r = routeMatcher("users/:id/*stuff/:other/*more"); same(r("users/123/aaa/456/bbb"), {id: "123", other: "456", stuff: "aaa", more: "bbb"}, "this is pushing it"); - r = getRoute("users/:id/:other/*stuff/*more"); + r = routeMatcher("users/:id/:other/*stuff/*more"); same(r("users/123/456/aaa/bbb/ccc"), {id: "123", other: "456", stuff: "aaa/bbb", more: "ccc"}, "this is a little more reasonable"); }); // These were pulled from the backbone.js unit tests. test("a few backbone.js test routes", function() { - r = getRoute("search/:query/p:page"); + var r = routeMatcher("search/:query/p:page"); same(r("search/boston/p20"), {query: "boston", page: "20"}, "should match"); - r = getRoute("*first/complex-:part/*rest"); + r = routeMatcher("*first/complex-:part/*rest"); same(r("one/two/three/complex-part/four/five/six/seven"), {first: "one/two/three", part: "part", rest: "four/five/six/seven"}, "should match"); - r = getRoute(":entity?*args"); + r = routeMatcher(":entity?*args"); same(r("cowboy?a=b&c=d"), {entity: "cowboy", args: "a=b&c=d"}, "should match"); - r = getRoute("*anything"); + r = routeMatcher("*anything"); same(r("doesnt-match-a-route"), {anything: "doesnt-match-a-route"}, "should match"); }); diff --git a/unit/index.html b/unit/index.html index 91160a7..3102671 100644 --- a/unit/index.html +++ b/unit/index.html @@ -1,14 +1,14 @@ - jQuery Route Test Suite + Basic Route Matcher Test Suite - - + + -

jQuery Route Test Suite

+

Basic Route Matcher Test Suite