This repository has been archived by the owner on Nov 19, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
routemap.compressed.js
12 lines (12 loc) · 5.98 KB
/
routemap.compressed.js
1
2
3
4
5
6
7
8
9
10
11
12
(function(x,y){(function(a,b){if(!a.every||!a.filter||!a.indexOf||!a.map||!a.reduce||!a.some||!a.forEach)throw Error("See "+b+" for reference versions of Array.prototype methods available in JS 1.8");})([],"https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/array/");var k,f={},l={},i=[],p=0,n=0,u=encodeURIComponent,v=decodeURIComponent,o="#",z=/\*|:|\?/,A=/(^([^\*:\?]+):\*)|(^\*$)/,B=/^:([^\*:\?]+)(\??)$/,C=/^([^\*:\?]+):(\??)$/,D=/([^/])$/,q="undefined"!==typeof window?window:{},
h=function(a){return"string"!==typeof a||!a.length},r=function(){var a=Object.prototype.toString,b=function(c){return"object"!==typeof c||null===c?c:"[object Array]"===a.call(c)?c.map(b):r(c)};return Array.prototype.reduce.call(arguments,function(c,d){if(!d||"object"!==typeof d||"[object Array]"===a.call(d))throw new TypeError("merge: "+a.call(d)+" is not mergeable");for(var e in d)d.hasOwnProperty(e)&&(c[e]=b(d[e]));return c},{})},w=function(a){var b=i.filter(function(b){return 0===a.replace(D,"$1/").indexOf(b)}).filter(function(a,
b){return!b||f[a].some(function(a){return!!a.rules.star})});return!b.length?[]:b.reduce(function(b,d){var e=f[d].map(function(b){var c={},e=b.rules.scalars,m=b.rules.keyvals,f,g=a.replace(d,"").split("/").reduce(function(a,b){var c=b.split("="),d=c[0],c=c.slice(1).join("=");return!b.length||(c?a.keyvals[d]=c:a.scalars.push(b)),a},{keyvals:{},scalars:[]}),j,h=m.reduce(function(a,b){return(a[b.name]=0)||a},{}),i=e.filter(function(a){return a.required}).length,l=m.filter(function(a){return a.required}).every(function(a){return g.keyvals.hasOwnProperty(a.name)});
if(i>g.scalars.length||!l)return 0;if(!b.rules.star){if(g.scalars.length>e.length)return 0;for(j in g.keyvals)if(g.keyvals.hasOwnProperty(j)&&!h.hasOwnProperty(j))return 0}g.scalars.slice(0,e.length).forEach(function(a,b){c[e[b].name]=v(a)});m.forEach(function(a){g.keyvals[a.name]&&(c[a.name]=v(g.keyvals[a.name]));delete g.keyvals[a.name]});if(b.rules.star){m=g.scalars.slice(e.length,g.scalars.length);for(j in g.keyvals)g.keyvals.hasOwnProperty(j)&&m.push([j,g.keyvals[j]].join("="));c[b.rules.star]=
m.join("/")}try{if(f=b.method.split(".").reduce(function(a,b){return a[b]},q),"function"!==typeof f)throw Error();}catch(n){throw new TypeError("parse: "+b.method+" is not a function in current context");}return{page:d,hash:k.hash({route:b.raw},c),method:f,args:c}});return b.concat(e).filter(Boolean)},[]).sort(function(a,b){return b.hash.length-a.hash.length})},t=function(a){return function(b){var c,d={},e="/"===b[0]?b:~(c=b.indexOf("/"))?b.slice(c):0,s=function(a){if(d.hasOwnProperty(a)||(d[a]=0))throw new SyntaxError('compile: "'+
a+'" is repeated in: '+b);};if(!e)throw new SyntaxError("compile: the route "+b+" was not understood");if(a[e])return a[e];c=e.split("/").reduce(function(a,c){var d=a.rules,e=d.scalars,g=d.keyvals;if(d.star)throw new SyntaxError("compile: no rules can follow a * directive in: "+b);if(!~c.search(z)&&!e.length&&!g.length)return a.page.push(c),a;if(c.match(A))return d.star=RegExp.$2||RegExp.$3,s(d.star),a;if(c.match(B)){if(a.has_optional_scalar)throw new SyntaxError('compile: "'+c+'" cannot follow an optional rule in: '+
b);RegExp.$2&&(a.has_optional_scalar=c);return e.push({name:RegExp.$1,required:!RegExp.$2}),s(RegExp.$1),a}if(c.match(C))return g.push({name:RegExp.$1,required:!RegExp.$2}),s(RegExp.$1),a;throw new SyntaxError('compile: the rule "'+c+'" was not understood in: '+b);},{page:[],rules:{scalars:[],keyvals:[],star:!1},has_optional_scalar:""});delete c.has_optional_scalar;c.page=c.page.join("/").replace(/\/$/,"")||"/";return a[e]=c}}({});x[y]=k={add:function(a){var b=a.method,c=a.route,d=[a.method,a.route].join("|");
if([c,b].some(h))throw new TypeError("add: rule.route and rule.method must both be non-empty strings");if(l[d])throw Error("add: "+c+" to "+b+" already exists");a=t(c);l[d]=!0;if(!f[a.page]&&(f[a.page]=[]))i=i.concat(a.page).sort(function(a,b){return b.length-a.length});f[a.page].push(k.post_add({method:b,rules:a.rules,raw:c}))},context:function(a){return q="object"===typeof a?a:q},current:function(){return n?r(n):null},default_handler:function(){},get:function(){if("undefined"===typeof window)return"/";
var a=window.location.hash,b=a.indexOf("/");return~b?a.slice(b):"/"},go:function(a){"undefined"!==typeof window&&(window.location.hash=(0===a.indexOf(o)?"":o)+a)},handler:function(){var a=k.get(),b=w(a),c=Array.prototype.slice.call(arguments);if(!b.length)return k.default_handler.apply(null,[a].concat(c));n=b[0];b=k.pre_dispatch(b);n=b[0];b.forEach(function(a){a.method.apply(null,[a.args].concat(c))});p=b[0]},hash:function(a,b){var c,d,b=b||{};if(h(a.route))throw new TypeError("hash: rule.route must be a non-empty string");
d=t(a.route);c=d.page+("/"===d.page?"":"/")+d.rules.scalars.map(function(c){var d=u(b[c.name]),f=void 0===b[c.name]||h(d);if(c.required&&f)throw new TypeError("hash: params."+c.name+" is undefined, route: "+a.route);return f?0:d}).concat(d.rules.keyvals.map(function(c){var d=u(b[c.name]),f=void 0===b[c.name]||h(d);if(c.required&&f)throw new TypeError("hash: params."+c.name+" is undefined, route: "+a.route);return f?0:c.name+"="+d})).filter(Boolean).join("/");d.rules.star&&b[d.rules.star]&&(c+=("/"===
c[c.length-1]?"":"/")+b[d.rules.star]);return c},last:function(){return p?r(p):null},parse:function(a){var b;b=a.indexOf("/");a=~b?a.slice(b):"";if(h(a))throw new TypeError("parse: hash must be a string with a / character");if(!(b=w(a)).length)throw new SyntaxError("parse: "+a+" cannot be parsed");return{page:b[0].page,args:b[0].args}},post_add:function(a){return a},pre_dispatch:function(a){return a},prefix:function(a){return o="undefined"!==typeof a?a+"":o},remove:function(a){var b=a.method,c=a.route,
d=[a.method,a.route].join("|"),e;if([c,b].some(h))throw new TypeError("remove: rule.route and rule.method must both be non-empty strings");l[d]&&(a=t(c),delete l[d],f[a.page]=f[a.page].filter(function(a){return a.raw!==c||a.method!==b}),!f[a.page].length&&delete f[a.page]&&~(e=i.indexOf(a.page))&&i.splice(e,1))}}})("undefined"===typeof exports?window:exports,"RouteMap");