Permalink
Browse files

add global greedy setting. see #46

  • Loading branch information...
1 parent 84d340e commit 138c3c4d13df1c2cd0f9769f018c7273e7f687a0 @millermedeiros committed Apr 16, 2012
Showing with 52 additions and 7 deletions.
  1. +3 −1 dev/src/crossroads.js
  2. +41 −0 dev/tests/spec/parse.spec.js
  3. +5 −3 dist/crossroads.js
  4. +2 −2 dist/crossroads.min.js
  5. +1 −1 package.json
@@ -14,6 +14,8 @@
Crossroads.prototype = {
+ greedy : false,
+
normalizeFn : null,
create : function () {
@@ -96,7 +98,7 @@
route;
//should be decrement loop since higher priorities are added at the end of array
while (route = routes[--n]) {
- if ((!res.length || route.greedy) && route.match(request)) {
+ if ((!res.length || this.greedy || route.greedy) && route.match(request)) {
res.push({
route : route,
params : route._getParamsArray(request)
@@ -660,6 +660,47 @@ describe('crossroads.parse()', function(){
});
+ it('should allow global greedy setting', function () {
+
+ var t1, t2, t3, t4, t5, t6, t7, t8;
+
+ crossroads.greedy = true;
+
+ var r1 = crossroads.addRoute('/{a}/{b}/', function(a,b){
+ t1 = a;
+ t2 = b;
+ });
+
+ var r2 = crossroads.addRoute('/bar/{b}/', function(a,b){
+ t3 = a;
+ t4 = b;
+ });
+
+ var r3 = crossroads.addRoute('/foo/{b}/', function(a,b){
+ t5 = a;
+ t6 = b;
+ });
+
+ var r4 = crossroads.addRoute('/{a}/:b:/', function(a,b){
+ t7 = a;
+ t8 = b;
+ });
+
+ crossroads.parse('/foo/lorem');
+
+ expect( t1 ).toEqual( 'foo' );
+ expect( t2 ).toEqual( 'lorem' );
+ expect( t3 ).toBeUndefined();
+ expect( t4 ).toBeUndefined();
+ expect( t5 ).toEqual( 'lorem' );
+ expect( t6 ).toBeUndefined();
+ expect( t7 ).toEqual( 'foo' );
+ expect( t8 ).toEqual( 'lorem' );
+
+ crossroads.greedy = false;
+
+ });
+
});
describe('default arguments', function () {
View
@@ -2,7 +2,7 @@
* crossroads <http://millermedeiros.github.com/crossroads.js/>
* License: MIT
* Author: Miller Medeiros
- * Version: 0.8.0 (2012/3/5 14:26)
+ * Version: 0.9.0-alpha (2012/4/16 20:38)
*/
(function (define) {
@@ -91,6 +91,8 @@ define(['signals'], function (signals) {
Crossroads.prototype = {
+ greedy : false,
+
normalizeFn : null,
create : function () {
@@ -173,7 +175,7 @@ define(['signals'], function (signals) {
route;
//should be decrement loop since higher priorities are added at the end of array
while (route = routes[--n]) {
- if ((!res.length || route.greedy) && route.match(request)) {
+ if ((!res.length || this.greedy || route.greedy) && route.match(request)) {
res.push({
route : route,
params : route._getParamsArray(request)
@@ -190,7 +192,7 @@ define(['signals'], function (signals) {
//"static" instance
crossroads = new Crossroads();
- crossroads.VERSION = '0.8.0';
+ crossroads.VERSION = '0.9.0-alpha';
crossroads.NORM_AS_ARRAY = function (req, vals) {
return [vals.vals_];
@@ -2,6 +2,6 @@
* crossroads <http://millermedeiros.github.com/crossroads.js/>
* License: MIT
* Author: Miller Medeiros
- * Version: 0.8.0 (2012/3/5 14:26)
+ * Version: 0.9.0-alpha (2012/4/16 20:38)
*/
-(function(a){a(["signals"],function(a){function d(a,b){if(a.indexOf)return a.indexOf(b);var c=a.length;while(c--)if(a[c]===b)return c;return-1}function e(a,b){return"[object "+b+"]"===Object.prototype.toString.call(a)}function f(a){return e(a,"RegExp")}function g(a){return e(a,"Array")}function h(a){return e(a,"Function")}function i(a){var b;return a===null||a==="null"?b=null:a==="true"?b=!0:a==="false"?b=!1:a===c||a==="undefined"?b=c:a===""||isNaN(a)?b=a:b=parseFloat(a),b}function j(a){var b=a.length,c=[];while(b--)c[b]=i(a[b]);return c}function k(){this._routes=[],this._prevRoutes=[],this.bypassed=new a.Signal,this.routed=new a.Signal}function l(c,d,e,g){var h=f(c),i=b.patternLexer;this._router=g,this._pattern=c,this._paramsIds=h?null:i.getParamIds(this._pattern),this._optionalParamsIds=h?null:i.getOptionalParamsIds(this._pattern),this._matchRegexp=h?c:i.compilePattern(c),this.matched=new a.Signal,this.switched=new a.Signal,d&&this.matched.add(d),this._priority=e||0}var b,c;return k.prototype={normalizeFn:null,create:function(){return new k},shouldTypecast:!1,addRoute:function(a,b,c){var d=new l(a,b,c,this);return this._sortedInsert(d),d},removeRoute:function(a){var b=d(this._routes,a);b!==-1&&this._routes.splice(b,1),a._destroy()},removeAllRoutes:function(){var a=this.getNumRoutes();while(a--)this._routes[a]._destroy();this._routes.length=0},parse:function(a,b){a=a||"",b=b||[];var c=this._getMatchedRoutes(a),d=0,e=c.length,f;if(e){this._notifyPrevRoutes(a),this._prevRoutes=c;while(d<e)f=c[d],f.route.matched.dispatch.apply(f.route.matched,b.concat(f.params)),f.isFirst=!d,this.routed.dispatch.apply(this.routed,b.concat([a,f])),d+=1}else this.bypassed.dispatch.apply(this.bypassed,b.concat([a]))},_notifyPrevRoutes:function(a){var b=0,c;while(c=this._prevRoutes[b++])c.route.switched&&c.route.switched.dispatch(a)},getNumRoutes:function(){return this._routes.length},_sortedInsert:function(a){var b=this._routes,c=b.length;do--c;while(b[c]&&a._priority<=b[c]._priority);b.splice(c+1,0,a)},_getMatchedRoutes:function(a){var b=[],c=this._routes,d=c.length,e;while(e=c[--d])(!b.length||e.greedy)&&e.match(a)&&b.push({route:e,params:e._getParamsArray(a)});return b},toString:function(){return"[crossroads numRoutes:"+this.getNumRoutes()+"]"}},b=new k,b.VERSION="0.8.0",b.NORM_AS_ARRAY=function(a,b){return[b.vals_]},b.NORM_AS_OBJECT=function(a,b){return[b]},l.prototype={greedy:!1,rules:void 0,match:function(a){return this._matchRegexp.test(a)&&this._validateParams(a)},_validateParams:function(a){var b=this.rules,c=this._getParamsObject(a),d;for(d in b)if(d!=="normalize_"&&b.hasOwnProperty(d)&&!this._isValidParam(a,d,c))return!1;return!0},_isValidParam:function(a,b,c){var e=this.rules[b],i=c[b],j=!1;return i==null&&this._optionalParamsIds&&d(this._optionalParamsIds,b)!==-1?j=!0:f(e)?j=e.test(i):g(e)?j=d(e,i)!==-1:h(e)&&(j=e(i,a,c)),j},_getParamsObject:function(a){var c=this._router.shouldTypecast,d=b.patternLexer.getParamValues(a,this._matchRegexp,c),e={},f=d.length;while(f--)e[f]=d[f],this._paramsIds&&(e[this._paramsIds[f]]=d[f]);return e.request_=c?i(a):a,e.vals_=d,e},_getParamsArray:function(a){var c=this.rules?this.rules.normalize_:null,d;return c=c||this._router.normalizeFn,c&&h(c)?d=c(a,this._getParamsObject(a)):d=b.patternLexer.getParamValues(a,this._matchRegexp,this._router.shouldTypecast),d},dispose:function(){this._router.removeRoute(this)},_destroy:function(){this.matched.dispose(),this.switched.dispose(),this.matched=this.switched=this._pattern=this._matchRegexp=null},toString:function(){return'[Route pattern:"'+this._pattern+'", numListeners:'+this.matched.getNumListeners()+"]"}},b.patternLexer=function(){function w(a,b){var c=[],d;while(d=a.exec(b))c.push(d[1]);return c}function x(a){return w(g,a)}function y(a){return w(f,a)}function z(c){return c=c||"",c&&(c=c.replace(b,""),c=A(c),c=c.replace(a,"\\$&"),c=B(c)),new RegExp("^"+c+"/?$")}function A(a){return a=a.replace(c,"$1"+p+"$2"),a=a.replace(d,"$1"+o+"$2"),a=a.replace(i,n),a=a.replace(h,m),a=a.replace(f,l),a.replace(e,k)}function B(a){return a=a.replace(u,"\\/?"),a=a.replace(v,"\\/"),a=a.replace(t,"(.*)?"),a=a.replace(s,"(.+)"),a=a.replace(r,"([^\\/]+)?/?"),a.replace(q,"([^\\/]+)")}function C(a,b,c){var d=b.exec(a);return d&&(d.shift(),c&&(d=j(d))),d}var a=/[\\.+*?\^$\[\](){}\/'#]/g,b=/\/$/g,c=/([:}]|\w(?=\/))\/?(:)/g,d=/([:}])\/?(\{)/g,e=/\{([^}]+)\}/g,f=/:([^:]+):/g,g=/(?:\{|:)([^}:]+)(?:\}|:)/g,h=/\{([^}]+)\*\}/g,i=/:([^:]+)\*:/g,k="__CR_RP__",l="__CR_OP__",m="__CR_RR__",n="__CR_OR__",o="__CR_RS__",p="__CR_OS__",q=new RegExp(k,"g"),r=new RegExp(l,"g"),s=new RegExp(m,"g"),t=new RegExp(n,"g"),u=new RegExp(p,"g"),v=new RegExp(o,"g");return{getParamIds:x,getOptionalParamsIds:y,getParamValues:C,compilePattern:z}}(),b})})(typeof define=="function"&&define.amd?define:function(a,b){typeof module!="undefined"&&module.exports?module.exports=b(require(a[0])):window.crossroads=b(window[a[0]])})
+(function(a){a(["signals"],function(a){function d(a,b){if(a.indexOf)return a.indexOf(b);var c=a.length;while(c--)if(a[c]===b)return c;return-1}function e(a,b){return"[object "+b+"]"===Object.prototype.toString.call(a)}function f(a){return e(a,"RegExp")}function g(a){return e(a,"Array")}function h(a){return e(a,"Function")}function i(a){var b;return a===null||a==="null"?b=null:a==="true"?b=!0:a==="false"?b=!1:a===c||a==="undefined"?b=c:a===""||isNaN(a)?b=a:b=parseFloat(a),b}function j(a){var b=a.length,c=[];while(b--)c[b]=i(a[b]);return c}function k(){this._routes=[],this._prevRoutes=[],this.bypassed=new a.Signal,this.routed=new a.Signal}function l(c,d,e,g){var h=f(c),i=b.patternLexer;this._router=g,this._pattern=c,this._paramsIds=h?null:i.getParamIds(this._pattern),this._optionalParamsIds=h?null:i.getOptionalParamsIds(this._pattern),this._matchRegexp=h?c:i.compilePattern(c),this.matched=new a.Signal,this.switched=new a.Signal,d&&this.matched.add(d),this._priority=e||0}var b,c;return k.prototype={greedy:!1,normalizeFn:null,create:function(){return new k},shouldTypecast:!1,addRoute:function(a,b,c){var d=new l(a,b,c,this);return this._sortedInsert(d),d},removeRoute:function(a){var b=d(this._routes,a);b!==-1&&this._routes.splice(b,1),a._destroy()},removeAllRoutes:function(){var a=this.getNumRoutes();while(a--)this._routes[a]._destroy();this._routes.length=0},parse:function(a,b){a=a||"",b=b||[];var c=this._getMatchedRoutes(a),d=0,e=c.length,f;if(e){this._notifyPrevRoutes(a),this._prevRoutes=c;while(d<e)f=c[d],f.route.matched.dispatch.apply(f.route.matched,b.concat(f.params)),f.isFirst=!d,this.routed.dispatch.apply(this.routed,b.concat([a,f])),d+=1}else this.bypassed.dispatch.apply(this.bypassed,b.concat([a]))},_notifyPrevRoutes:function(a){var b=0,c;while(c=this._prevRoutes[b++])c.route.switched&&c.route.switched.dispatch(a)},getNumRoutes:function(){return this._routes.length},_sortedInsert:function(a){var b=this._routes,c=b.length;do--c;while(b[c]&&a._priority<=b[c]._priority);b.splice(c+1,0,a)},_getMatchedRoutes:function(a){var b=[],c=this._routes,d=c.length,e;while(e=c[--d])(!b.length||this.greedy||e.greedy)&&e.match(a)&&b.push({route:e,params:e._getParamsArray(a)});return b},toString:function(){return"[crossroads numRoutes:"+this.getNumRoutes()+"]"}},b=new k,b.VERSION="0.9.0-alpha",b.NORM_AS_ARRAY=function(a,b){return[b.vals_]},b.NORM_AS_OBJECT=function(a,b){return[b]},l.prototype={greedy:!1,rules:void 0,match:function(a){return this._matchRegexp.test(a)&&this._validateParams(a)},_validateParams:function(a){var b=this.rules,c=this._getParamsObject(a),d;for(d in b)if(d!=="normalize_"&&b.hasOwnProperty(d)&&!this._isValidParam(a,d,c))return!1;return!0},_isValidParam:function(a,b,c){var e=this.rules[b],i=c[b],j=!1;return i==null&&this._optionalParamsIds&&d(this._optionalParamsIds,b)!==-1?j=!0:f(e)?j=e.test(i):g(e)?j=d(e,i)!==-1:h(e)&&(j=e(i,a,c)),j},_getParamsObject:function(a){var c=this._router.shouldTypecast,d=b.patternLexer.getParamValues(a,this._matchRegexp,c),e={},f=d.length;while(f--)e[f]=d[f],this._paramsIds&&(e[this._paramsIds[f]]=d[f]);return e.request_=c?i(a):a,e.vals_=d,e},_getParamsArray:function(a){var c=this.rules?this.rules.normalize_:null,d;return c=c||this._router.normalizeFn,c&&h(c)?d=c(a,this._getParamsObject(a)):d=b.patternLexer.getParamValues(a,this._matchRegexp,this._router.shouldTypecast),d},dispose:function(){this._router.removeRoute(this)},_destroy:function(){this.matched.dispose(),this.switched.dispose(),this.matched=this.switched=this._pattern=this._matchRegexp=null},toString:function(){return'[Route pattern:"'+this._pattern+'", numListeners:'+this.matched.getNumListeners()+"]"}},b.patternLexer=function(){function w(a,b){var c=[],d;while(d=a.exec(b))c.push(d[1]);return c}function x(a){return w(g,a)}function y(a){return w(f,a)}function z(c){return c=c||"",c&&(c=c.replace(b,""),c=A(c),c=c.replace(a,"\\$&"),c=B(c)),new RegExp("^"+c+"/?$")}function A(a){return a=a.replace(c,"$1"+p+"$2"),a=a.replace(d,"$1"+o+"$2"),a=a.replace(i,n),a=a.replace(h,m),a=a.replace(f,l),a.replace(e,k)}function B(a){return a=a.replace(u,"\\/?"),a=a.replace(v,"\\/"),a=a.replace(t,"(.*)?"),a=a.replace(s,"(.+)"),a=a.replace(r,"([^\\/]+)?/?"),a.replace(q,"([^\\/]+)")}function C(a,b,c){var d=b.exec(a);return d&&(d.shift(),c&&(d=j(d))),d}var a=/[\\.+*?\^$\[\](){}\/'#]/g,b=/\/$/g,c=/([:}]|\w(?=\/))\/?(:)/g,d=/([:}])\/?(\{)/g,e=/\{([^}]+)\}/g,f=/:([^:]+):/g,g=/(?:\{|:)([^}:]+)(?:\}|:)/g,h=/\{([^}]+)\*\}/g,i=/:([^:]+)\*:/g,k="__CR_RP__",l="__CR_OP__",m="__CR_RR__",n="__CR_OR__",o="__CR_RS__",p="__CR_OS__",q=new RegExp(k,"g"),r=new RegExp(l,"g"),s=new RegExp(m,"g"),t=new RegExp(n,"g"),u=new RegExp(p,"g"),v=new RegExp(o,"g");return{getParamIds:x,getOptionalParamsIds:y,getParamValues:C,compilePattern:z}}(),b})})(typeof define=="function"&&define.amd?define:function(a,b){typeof module!="undefined"&&module.exports?module.exports=b(require(a[0])):window.crossroads=b(window[a[0]])})
View
@@ -3,7 +3,7 @@
"description" : "Flexible router which can be used in multiple environments",
"keywords" : ["routes", "event", "observer", "routing", "router"],
"homepage" : "http://millermedeiros.github.com/crossroads.js/",
- "version" : "0.8.0",
+ "version" : "0.9.0-alpha",
"author" : {
"name" : "Miller Medeiros",
"url" : "http://blog.millermedeiros.com/"

0 comments on commit 138c3c4

Please sign in to comment.