-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjquery-uritemplate.1.0.js
9 lines (9 loc) · 5.68 KB
/
jquery-uritemplate.1.0.js
1
2
3
4
5
6
7
8
9
(function($) {
if (!window.UriTemplate) {
(function(p){function s(a){return"[object Array]"===Object.prototype.toString.apply(a)}function w(a,b,c){for(var d in a)a.hasOwnProperty(d)&&(c=b(c,a[d],d,a));return c}function x(a,b,c){for(var d=c,c=0;c<a.length;c+=1)d=b(d,a[c],c,a);return d}function m(a){var b;if(null===a||void 0===a)return!1;if(s(a)){for(b=0;b<a.length;b+=1)if(m(a[b]))return!0;return!1}if("string"===typeof a||"number"===typeof a||"boolean"===typeof a)return!0;for(b in a)if(a.hasOwnProperty(b)&&m(a[b]))return!0;return!1}function n(a){return"0"<=a&&"9">=a}function o(a){return n(a)||"a"<=a&&"f">=a||"A"<=a&&"F">=a}function y(a){return"a"<=a&&"z">=a||"A"<=a&&"Z">=a||n(a)||"-"===a||"."===a||"_"===a||"~"===a}function z(a){return":"===a||"/"===a||"?"===a||"#"===a||"["===a||"]"===a||"@"===a||"!"===a||"$"===a||"&"===a||"("===a||")"===a||"*"===a||"+"===a||","===a||";"===a||"="===a||"'"===a}function A(a,b){var c="",d,e;if("number"===typeof a||"boolean"===typeof a)a=a.toString();for(d=0;d<a.length;d+=1)e=a.charAt(d),"%"===e?o(a.charAt(d+1))&&o(a.charAt(d+2))?(c+=a.substr(d,3),d+=2):c+="%25":c+=y(e)||b&&z(e)?e:"%"+e.charCodeAt(0).toString(16).toUpperCase();return c}function C(a){return A(a,!0)}function q(a,b){this.templateText=a;this.experssions=b}function t(a){var b="",c,d;for(c=0;c<a.length;c+=1)if(d=a.charAt(c),"%"===d)if(o(a.charAt(c+1))&&o(a.charAt(c+2)))b+=a.substr(c,3),c+=2;else throw Error("illegal % found at position "+c);else b+=z(d)||y(d)?d:"%"+d.charCodeAt(0).toString(16).toUpperCase();return b}function r(a){this.literal=t(a)}function v(a,b,c){this.templateText=a;this.operator=b;this.varspecs=c}function D(a){function b(){i={varname:d.substring(g,h),exploded:!1,maxLength:null};g=null}function c(){if(k===h)throw Error("after a ':' you have to specify the length. position = "+h);i.maxLength=parseInt(d.substring(k,h),10);k=null}var d,e,j=[],i=null,g=null,k=null,h,f;d=a.substr(1,a.length-2);for(h=0;h<d.length;h+=f.length){f=d[h];if(0===h){e=B.valueOf(f);if(""!==e.symbol){g=1;continue}g=0}if(null!==g){if("%"===f){if(h>d.length-2||!n(d[h+1]||!n(d[h+2])))throw Error('illegal char "%" at position '+h);f+=d[h+1]+d[h+2]}if("."===f){if(g===h)throw Error("a varname MUST NOT start with a dot -- see position "+h);continue}if("a"<=f&&"z">=f||"A"<=f&&"Z">=f||n(f)||"_"===f||3===f.length&&"%"===f.charAt(0)&&o(f.charAt(1)&&o(f.charAt(2))))continue;b()}if(null!==k){if(n(f))continue;c()}if(":"===f){if(null!==i.maxLength)throw Error("only one :maxLength is allowed per varspec at position "+h);k=h+1}else if("*"===f){if(null===i)throw Error("explode exploded at position "+h);if(i.exploded)throw Error("explode exploded twice at position "+h);if(i.maxLength)throw Error("an explode (*) MUST NOT follow to a prefix, see position "+h);i.exploded=!0}else if(","===f)j.push(i),i=null,g=h+1;else throw Error("illegal character '"+f+"' at position "+h);}null!==g&&b();null!==k&&c();j.push(i);return new v(a,e,j)}var B,l=function(a){u[a]={symbol:a,separator:"?"===a?"&":""===a||"+"===a||"#"===a?",":a,named:";"===a||"&"===a||"?"===a,ifEmpty:"&"===a||"?"===a?"=":"",first:"+"===a?"":a,encode:"+"===a||"#"===a?C:A,toString:function(){return this.symbol}}},u={};l("");l("+");l("#");l(".");l("/");l(";");l("?");l("&");B={valueOf:function(a){if(u[a])return u[a];if(0<="=,!@|".indexOf(a))throw Error('Illegal use of reserved operator "'+a+'"');return u[""]}};q.prototype.toString=function(){return this.templateText};q.prototype.expand=function(a){var b,c="";for(b=0;b<this.experssions.length;b+=1)c+=this.experssions[b].expand(a);return c};r.prototype.expand=function(){return this.literal};r.prototype.toString=r.prototype.expand;v.prototype.toString=function(){return this.templateText};v.prototype.expand=function(a){function b(a,b,c){m(b)&&(0<a.length&&(a+=","),k||(a+=f.encode(c)+","),a+=f.encode(b));return a}function c(a,b,c){m(b)&&(0<a.length&&(a+=f.separator),a+=k?t(i.varname):f.encode(c),a+="="+f.encode(b));return a}function d(a,b,c){m(b)&&(0<a.length&&(a+=f.separator),k||(a+=f.encode(c)+"="),a+=f.encode(b));return a}var e="",j,i,g,k,h=!0,f=this.operator;for(j=0;j<this.varspecs.length;j+=1)if(i=this.varspecs[j],g=a[i.varname],m(g))if(h?(e+=this.operator.first,h=!1):e+=this.operator.separator,k=s(g),"string"===typeof g||"number"===typeof g||"boolean"===typeof g){g=g.toString();if(this.operator.named){e+=t(i.varname);if(""===g){e+=this.operator.ifEmpty;continue}e+="="}i.maxLength&&g.length>i.maxLength&&(g=g.substr(0,i.maxLength));e+=this.operator.encode(g)}else{if(i.maxLength)throw Error("Prefix modifiers are not applicable to variables that have composite values. You tried to expand "+this+" with "+JSON.stringify(g));if(i.exploded)e+=s(g)?x(g,f.named?c:d,""):w(g,f.named?c:d,"");else{if(f.named){e+=t(i.varname);if(!m(g)){e+=this.operator.ifEmpty;continue}e+="="}e+=s(g)?x(g,b,""):w(g,b,"")}}return e};q.parse=function(a){var b,c,d=[],e=null,j=0;for(b=0;b<a.length;b+=1)if(c=a.charAt(b),null!==j){if("}"===c)throw Error("brace was closed in position "+b+" but never opened");"{"===c&&(j<b&&d.push(new r(a.substring(j,b))),j=null,e=b)}else if(null!==e){if("{"===c)throw Error("brace was opened in position "+e+" and cannot be reopened in position "+b);if("}"===c){if(e+1===b)throw Error("empty braces on position "+e);d.push(D(a.substring(e,b+1)));e=null;j=b+1}}else throw Error("reached unreachable code");if(null!==e)throw Error("brace was opened on position "+e+", but never closed");j<a.length&&d.push(new r(a.substr(j)));return new q(a,d)};p(q)})(function(p){"undefined"!==typeof module?module.exports=p:"undefined"!==typeof window?window.UriTemplate=p:global.UriTemplate=p});
}
$.ajaxPrefilter(function(options) {
if (options.url)
options.url = UriTemplate.parse(options.url).expand(options.tokens || {});
});
})(jQuery);