Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

update with support for multiple actions and multiple splats

  • Loading branch information...
commit 64f5c68421ea71622ed1cc89df2352fb7ac9bd45 1 parent 97ef002
@haggen authored
Showing with 23 additions and 19 deletions.
  1. +21 −16 way.js
  2. +2 −3 way.min.js
View
37 way.js
@@ -1,5 +1,5 @@
/*
- * WayJS v0.2.0 2012-08-24 18:47:20 -0300
+ * WayJS v0.3.2 2012-09-05 19:35:01 -0300
* by Arthur Corenzan <arthur@corenzan.com>
* licensed under http://creativecommons.org/licenses/by/3.0
* more on http://haggen.github.com/wayjs
@@ -16,12 +16,12 @@
Way.prototype = {
- map: function(pattern, action) {
+ map: function() {
var route = {};
route.params = [];
- route.action = action;
- route.pattern = this.translate(pattern, route);
+ route.pattern = this.translate([].shift.apply(arguments), route);
+ route.actions = [].slice.apply(arguments);
this.routes.push(route);
},
@@ -39,7 +39,6 @@
// Translate named parameters
re = re.replace(/:(\w[\w\d]*)|\(/g, function(m, n) {
- // console.log(m, n, m === '(' ? '_' : n, m === '(' ? '(' : '([^\\/]+?)');
route.params.push(m === '(' ? '_' : n);
return m === '(' ? '(' : '([^\\/]+?)';
});
@@ -54,32 +53,38 @@
},
match: function(path) {
- var route, match, matches, m, i, j;
-
- matches = [];
+ var route, match, param, m, i, j;
for(i = 0; i < this.routes.length; i++) {
route = this.routes[i];
- match = { action: route.action, params: [] };
+ match = { actions: route.actions, params: {} };
m = path.match(route.pattern);
if(m) {
for(j = 1; j < m.length; j++) {
- match.params[route.params[j - 1]] = m[j];
+ param = route.params[j - 1];
+
+ if(param === 'splat') {
+ if('splat' in match.params) {
+ match.params['splat'].push(m[j]);
+ } else {
+ match.params['splat'] = [m[j]];
+ }
+ } else {
+ match.params[param] = m[j];
+ }
}
- matches.push(match);
+ return match;
}
}
-
- return matches;
}
};
- if(typeof window === 'undefined') {
- module.exports = new Way();
- } else {
+ if(typeof window === 'object') {
window.way = new Way();
+ } else if(typeof module === 'object' && 'exports' in module) {
+ module.exports = new Way();
}
})();
View
5 way.min.js
@@ -1,8 +1,7 @@
/*
- * WayJS v0.2.0 2012-08-24 18:47:20 -0300
+ * WayJS v0.3.2 2012-09-05 19:35:01 -0300
* by Arthur Corenzan <arthur@corenzan.com>
* licensed under http://creativecommons.org/licenses/by/3.0
* more on http://haggen.github.com/wayjs
*/
-(function(){var c;c=function(){this.routes=[]};c.prototype={map:function(d,b){var a={params:[]};a.action=b;a.pattern=this.translate(d,a);this.routes.push(a)},translate:function(d,b){var a;a=d.replace(/\//g,"\\/");a=a.replace(/\)/g,function(){return")?"});a=a.replace(/:(\w[\w\d]*)|\(/g,function(a,d){b.params.push("("===a?"_":d);return"("===a?"(":"([^\\/]+?)"});a=a.replace(/\*/g,function(){b.params.push("splat");return"(.+?)"});return RegExp("^"+a+"$")},match:function(d){var b,a,c,g,f,e;c=[];for(f=
-0;f<this.routes.length;f++)if(b=this.routes[f],a={action:b.action,params:[]},g=d.match(b.pattern)){for(e=1;e<g.length;e++)a.params[b.params[e-1]]=g[e];c.push(a)}return c}};"undefined"===typeof window?module.exports=new c:window.way=new c})();
+(function(e){"use strict";var t;t=function(){this.routes=[]},t.prototype={map:function(){var e={};e.params=[],e.pattern=this.translate([].shift.apply(arguments),e),e.actions=[].slice.apply(arguments),this.routes.push(e)},translate:function(e,t){var n;return n=e.replace(/\//g,"\\/"),n=n.replace(/\)/g,function(){return")?"}),n=n.replace(/:(\w[\w\d]*)|\(/g,function(e,n){return t.params.push(e==="("?"_":n),e==="("?"(":"([^\\/]+?)"}),n=n.replace(/\*/g,function(){return t.params.push("splat"),"(.+?)"}),new RegExp("^"+n+"$")},match:function(e){var t,n,r,i,s,o;for(s=0;s<this.routes.length;s++){t=this.routes[s],n={actions:t.actions,params:{}},i=e.match(t.pattern);if(i){for(o=1;o<i.length;o++)r=t.params[o-1],r==="splat"?"splat"in n.params?n.params.splat.push(i[o]):n.params.splat=[i[o]]:n.params[r]=i[o];return n}}}},typeof window=="object"?window.way=new t:typeof module=="object"&&"exports"in module&&(module.exports=new t)})();
Please sign in to comment.
Something went wrong with that request. Please try again.