Permalink
Browse files

added partial matching

  • Loading branch information...
1 parent cd4b402 commit c51fe8babd6b4f3e1d196ece4fcde7d3e5434202 @joshbuddy committed Feb 3, 2010
Showing with 14 additions and 1 deletion.
  1. +8 −1 lib/sherpa.js
  2. +6 −0 spec/spec_recognize.js
View
@@ -9,6 +9,7 @@ Sherpa = {
this.parent = parent;
this.lookup = {};
this.requestLookup = undefined;
+ this.matchPartially = false;
this.destination = undefined;
this.value = undefined;
this.shortcut = [];
@@ -18,6 +19,7 @@ Sherpa = {
},
RequestNode: function(parent) {
this.parent = parent;
+ this.matchPartially = false;
this.destination = undefined;
this.requestLookup = undefined;
this.requestKey = undefined;
@@ -48,6 +50,11 @@ Sherpa.RouteSet.prototype = {
this.router.routes[name] = this;
return this;
},
+ matchPartially: function(match) {
+ for (var routeIndex = 0; routeIndex != this.routes.length; routeIndex++) {
+ this.routes[routeIndex].finalNode.matchPartially = (match === undefined || match === true);
+ }
+ },
matchingNode: function(params) {
if (this.routes.length == 1) {
return this.routes[0].finalNode;
@@ -238,7 +245,7 @@ Sherpa.Router.prototype = {
var splitUri = uri.split(Sherpa.SplitRegex);
var node = this.root;
- while (uri.length > 0) {
+ while (uri.length > 0 && !node.matchPartially) {
var position = splitUri.shift().length;
var part = uri.substring(0, position);
View
@@ -13,6 +13,12 @@ describe("Sherpa - recognize", function() {
assertEqual('recognized', router.recognize('/test').destination);
});
+ it("should recognize a partial route", function() {
+ var router = new Sherpa.Router();
+ router.add('/test').to('recognized').matchPartially();
+ assertEqual('recognized', router.recognize('/test/testing').destination);
+ });
+
it("should recognize a simple route with optionals", function() {
var router = new Sherpa.Router();
router.add('/(test)').to('recognized');

0 comments on commit c51fe8b

Please sign in to comment.