Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Renamed some stuff, like the name of the project. And the function. A…
…nd wrote some very rudimentary docs. Etc.
  • Loading branch information
cowboy committed Sep 16, 2011
1 parent 24194ef commit 7f52e2a
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 25 deletions.
33 changes: 28 additions & 5 deletions 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.
<http://benalman.com/about/license/>
2 changes: 1 addition & 1 deletion jquery.ba-route.js → ba-routematcher.js
Expand Up @@ -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
Expand Down
30 changes: 15 additions & 15 deletions unit/jquery.ba-route.js → unit/ba-routematcher.js
@@ -1,57 +1,57 @@

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");
same(r("user/123..456"), ["user/123..456", "123", "456"], "should match");
});

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");
same(r("users"), {}, "Should match");
});

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");
same(r("users/123"), {id: "123"}, "should match");
});

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");
same(r("users/123/456"), {stuff: "123/456"}, "should match");
});

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");
Expand All @@ -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");
});
8 changes: 4 additions & 4 deletions unit/index.html
@@ -1,14 +1,14 @@
<!DOCTYPE html>
<html>
<head>
<title>jQuery Route Test Suite</title>
<title>Basic Route Matcher Test Suite</title>
<link rel="stylesheet" href="qunit.css" type="text/css">
<script src="qunit.js"></script>
<script src="../jquery.ba-route.js"></script>
<script src="jquery.ba-route.js"></script>
<script src="../ba-routematcher.js"></script>
<script src="ba-routematcher.js"></script>
</head>
<body>
<h1 id="qunit-header">jQuery Route Test Suite</h1>
<h1 id="qunit-header">Basic Route Matcher Test Suite</h1>
<h2 id="qunit-banner"></h2>
<div id="qunit-testrunner-toolbar"></div>
<h2 id="qunit-userAgent"></h2>
Expand Down

0 comments on commit 7f52e2a

Please sign in to comment.