Commit 19aa9f17 authored by fxa's avatar fxa

fixed https://github.com/fxa/uritemplate-js/issues/11 (Problems in older IEs. Thanks to anozaki!)

parent 819db320
...@@ -62,6 +62,7 @@ MIT License, see http://mit-license.org/ ...@@ -62,6 +62,7 @@ MIT License, see http://mit-license.org/
Release Notes Release Notes
------------- -------------
* 0.3.2 fixed https://github.com/fxa/uritemplate-js/issues/11 Problems with older IE versions. Thanks to anozaki!
* 0.3.1 fixed https://github.com/fxa/uritemplate-js/issues/10 thank you, Paul-Martin! * 0.3.1 fixed https://github.com/fxa/uritemplate-js/issues/10 thank you, Paul-Martin!
* 0.3.0 introduced UriTemplateError as exception, so the interface changed from string to UriTemplateError (as the rfc suggested) * 0.3.0 introduced UriTemplateError as exception, so the interface changed from string to UriTemplateError (as the rfc suggested)
* 0.2.4 fixed double encoding according [RubenVerborgh] and some Prefix modifiers bugs * 0.2.4 fixed double encoding according [RubenVerborgh] and some Prefix modifiers bugs
......
(function(e){"use strict";function n(e){var t;if(null===e||void 0===e)return!1;if(r.isArray(e))return e.length>0;if("string"==typeof e||"number"==typeof e||"boolean"==typeof e)return!0;for(t in e)if(e.hasOwnProperty(t)&&n(e[t]))return!0;return!1}var t=function(){function e(e){this.options=e}return e.prototype.toString=function(){return JSON&&JSON.stringify?JSON.stringify(this.options):this.options},e}(),r=function(){function e(e){return"[object Array]"===Object.prototype.toString.apply(e)}function n(e,n){var t,r="",i=!0;for(t=0;e.length>t;t+=1)i?i=!1:r+=n,r+=e[t];return r}function t(e,n){for(var t=[],r=0;e.length>r;r+=1)t.push(n(e[r]));return t}function r(e,n){for(var t=[],r=0;e.length>r;r+=1)n(e[r])&&t.push(e[r]);return t}function i(e){if("object"!=typeof e||null===e)return e;Object.freeze(e);var n,t;for(t in e)e.hasOwnProperty(t)&&(n=e[t],"object"==typeof n&&o(n));return e}function o(e){return"function"==typeof Object.freeze?i(e):e}return{isArray:e,join:n,map:t,filter:r,deepFreeze:o}}(),i=function(){function e(e){return e>="a"&&"z">=e||e>="A"&&"Z">=e}function n(e){return e>="0"&&"9">=e}function t(e){return n(e)||e>="a"&&"f">=e||e>="A"&&"F">=e}return{isAlpha:e,isDigit:n,isHexDigit:t}}(),o=function(){function e(e){var n,t,r="",i=s.encode(e);for(t=0;i.length>t;t+=1)n=i.charCodeAt(t),r+="%"+n.toString(16).toUpperCase();return r}function n(e,n){return"%"===e[n]&&i.isHexDigit(e[n+1])&&i.isHexDigit(e[n+2])}function t(e,n){return parseInt(e.substr(n,2),16)}function r(e){if(!n(e,0))return!1;var r=t(e,1),i=s.numBytes(r);if(0===i)return!1;for(var o=1;i>o;o+=1)if(!n(e,3*o)||!s.isValidFollowingCharCode(t(e,3*o+1)))return!1;return!0}function o(e,r){var i=e[r];if(!n(e,r))return i;var o=t(e,r+1),a=s.numBytes(o);if(0===a)return i;for(var u=1;a>u;u+=1)if(!n(e,r+3*u)||!s.isValidFollowingCharCode(t(e,r+3*u+1)))return i;return e.substr(r,3*a)}var s={encode:function(e){return unescape(encodeURIComponent(e))},numBytes:function(e){return 127>=e?1:e>=194&&223>=e?2:e>=224&&239>=e?3:e>=240&&244>=e?4:0},isValidFollowingCharCode:function(e){return e>=128&&191>=e}};return{encodeCharacter:e,isPctEncoded:r,pctCharAt:o}}(),s=function(){function e(e){return i.isAlpha(e)||i.isDigit(e)||"_"===e||o.isPctEncoded(e)}function n(e){return i.isAlpha(e)||i.isDigit(e)||"-"===e||"."===e||"_"===e||"~"===e}function t(e){return":"===e||"/"===e||"?"===e||"#"===e||"["===e||"]"===e||"@"===e||"!"===e||"$"===e||"&"===e||"("===e||")"===e||"*"===e||"+"===e||","===e||";"===e||"="===e||"'"===e}return{isVarchar:e,isUnreserved:n,isReserved:t}}(),a=function(){function e(e,n){var t,r="",i="";for(("number"==typeof e||"boolean"==typeof e)&&(e=""+e),t=0;e.length>t;t+=i.length)i=e.charAt(t),r+=s.isUnreserved(i)||n&&s.isReserved(i)?i:o.encodeCharacter(i);return r}function n(n){return e(n,!0)}function t(e,n){var t=o.pctCharAt(e,n);return t.length>1?t:s.isReserved(t)||s.isUnreserved(t)?t:o.encodeCharacter(t)}function r(e){var n,t="",r="";for(n=0;e.length>n;n+=r.length)r=o.pctCharAt(e,n),t+=r.length>1?r:s.isReserved(r)||s.isUnreserved(r)?r:o.encodeCharacter(r);return t}return{encode:e,encodePassReserved:n,encodeLiteral:r,encodeLiteralCharacter:t}}(),u=function(){function e(e){n[e]={symbol:e,separator:"?"===e?"&":""===e||"+"===e||"#"===e?",":e,named:";"===e||"&"===e||"?"===e,ifEmpty:"&"===e||"?"===e?"=":"",first:"+"===e?"":e,encode:"+"===e||"#"===e?a.encodePassReserved:a.encode,toString:function(){return this.symbol}}}var n={};return e(""),e("+"),e("#"),e("."),e("/"),e(";"),e("?"),e("&"),{valueOf:function(e){return n[e]?n[e]:"=,!@|".indexOf(e)>=0?null:n[""]}}}(),f=function(){function e(e){this.literal=a.encodeLiteral(e)}return e.prototype.expand=function(){return this.literal},e.prototype.toString=e.prototype.expand,e}(),p=function(){function e(e){function n(){var n=e.substring(h,f);if(0===n.length)throw new t({expressionText:e,message:"a varname must be specified",position:f});c={varname:n,exploded:!1,maxLength:null},h=null}function r(){if(d===f)throw new t({expressionText:e,message:"after a ':' you have to specify the length",position:f});c.maxLength=parseInt(e.substring(d,f),10),d=null}var a,f,p=[],c=null,h=null,d=null,m="";for(a=function(n){var r=u.valueOf(n);if(null===r)throw new t({expressionText:e,message:"illegal use of reserved operator",position:f,operator:n});return r}(e.charAt(0)),f=a.symbol.length,h=f;e.length>f;f+=m.length){if(m=o.pctCharAt(e,f),null!==h){if("."===m){if(h===f)throw new t({expressionText:e,message:"a varname MUST NOT start with a dot",position:f});continue}if(s.isVarchar(m))continue;n()}if(null!==d){if(f===d&&"0"===m)throw new t({expressionText:e,message:"A :prefix must not start with digit 0",position:f});if(i.isDigit(m)){if(f-d>=4)throw new t({expressionText:e,message:"A :prefix must have max 4 digits",position:f});continue}r()}if(":"!==m)if("*"!==m){if(","!==m)throw new t({expressionText:e,message:"illegal character",character:m,position:f});p.push(c),c=null,h=f+1}else{if(null===c)throw new t({expressionText:e,message:"exploded without varspec",position:f});if(c.exploded)throw new t({expressionText:e,message:"exploded twice",position:f});if(c.maxLength)throw new t({expressionText:e,message:"an explode (*) MUST NOT follow to a prefix",position:f});c.exploded=!0}else{if(null!==c.maxLength)throw new t({expressionText:e,message:"only one :maxLength is allowed per varspec",position:f});if(c.exploded)throw new t({expressionText:e,message:"an exploeded varspec MUST NOT be varspeced",position:f});d=f+1}}return null!==h&&n(),null!==d&&r(),p.push(c),new l(e,a,p)}function n(n){var r,i,o=[],s=null,a=0;for(r=0;n.length>r;r+=1)if(i=n.charAt(r),null===a){if(null===s)throw Error("reached unreachable code");if("{"===i)throw new t({templateText:n,message:"brace already opened",position:r});if("}"===i){if(s+1===r)throw new t({templateText:n,message:"empty braces",position:s});try{o.push(e(n.substring(s+1,r)))}catch(u){if(u.prototype===t.prototype)throw new t({templateText:n,message:u.options.message,position:s+u.options.position,details:u.options});throw u}s=null,a=r+1}}else{if("}"===i)throw new t({templateText:n,message:"unopened brace closed",position:r});"{"===i&&(r>a&&o.push(new f(n.substring(a,r))),a=null,s=r)}if(null!==s)throw new t({templateText:n,message:"unclosed brace",position:s});return n.length>a&&o.push(new f(n.substr(a))),new c(n,o)}return n}(),l=function(){function e(e){return JSON?JSON.stringify(e):e}function t(e){if(!n(e))return!0;if(""===e)return!0;if(r.isArray(e))return 0===e.length;for(var t in e)if(e.hasOwnProperty(t))return!1;return!0}function i(e){var n,t=[];for(n in e)e.hasOwnProperty(n)&&t.push({name:n,value:e[n]});return t}function o(e,n,t){this.templateText=e,this.operator=n,this.varspecs=t}function s(e,n,t){var r="";if(t=""+t,n.named){if(r+=a.encodeLiteral(e.varname),""===t)return r+=n.ifEmpty;r+="="}return null!==e.maxLength&&(t=t.substr(0,e.maxLength)),r+=n.encode(t)}function u(e){return n(e.value)}function f(e,o,s){var f=[],p="";if(o.named){if(p+=a.encodeLiteral(e.varname),t(s))return p+=o.ifEmpty;p+="="}return r.isArray(s)?(f=s,f=r.filter(f,n),f=r.map(f,o.encode),p+=r.join(f,",")):(f=i(s),f=r.filter(f,u),f=r.map(f,function(e){return o.encode(e.name)+","+o.encode(e.value)}),p+=r.join(f,",")),p}function p(e,o,s){var f=r.isArray(s),p=[];return f?(p=s,p=r.filter(p,n),p=r.map(p,function(n){var r=a.encodeLiteral(e.varname);return r+=t(n)?o.ifEmpty:"="+o.encode(n)})):(p=i(s),p=r.filter(p,u),p=r.map(p,function(e){var n=a.encodeLiteral(e.name);return n+=t(e.value)?o.ifEmpty:"="+o.encode(e.value)})),r.join(p,o.separator)}function l(e,t){var o=[],s="";return r.isArray(t)?(o=t,o=r.filter(o,n),o=r.map(o,e.encode),s+=r.join(o,e.separator)):(o=i(t),o=r.filter(o,function(e){return n(e.value)}),o=r.map(o,function(n){return e.encode(n.name)+"="+e.encode(n.value)}),s+=r.join(o,e.separator)),s}return o.prototype.toString=function(){return this.templateText},o.prototype.expand=function(i){var o,a,u,c,h=[],d=!1,m=this.operator;for(o=0;this.varspecs.length>o;o+=1)if(a=this.varspecs[o],u=i[a.varname],null!==u&&void 0!==u)if(a.exploded&&(d=!0),c=r.isArray(u),"string"==typeof u||"number"==typeof u||"boolean"==typeof u)h.push(s(a,m,u));else{if(a.maxLength&&n(u))throw Error("Prefix modifiers are not applicable to variables that have composite values. You tried to expand "+this+" with "+e(u));a.exploded?n(u)&&(m.named?h.push(p(a,m,u)):h.push(l(m,u))):(m.named||!t(u))&&h.push(f(a,m,u))}return 0===h.length?"":m.first+r.join(h,m.separator)},o}(),c=function(){function e(e,n){this.templateText=e,this.expressions=n,r.deepFreeze(this)}return e.prototype.toString=function(){return this.templateText},e.prototype.expand=function(e){var n,t="";for(n=0;this.expressions.length>n;n+=1)t+=this.expressions[n].expand(e);return t},e.parse=p,e.UriTemplateError=t,e}();e(c)})(function(e){"use strict";"undefined"!=typeof module?module.exports=e:"function"==typeof define?define([],function(){return e}):"undefined"!=typeof window?window.UriTemplate=e:global.UriTemplate=e}); !function(e){"use strict";function t(e){var n;if(null===e||void 0===e)return!1;if(r.isArray(e))return e.length>0;if("string"==typeof e||"number"==typeof e||"boolean"==typeof e)return!0;for(n in e)if(e.hasOwnProperty(n)&&t(e[n]))return!0;return!1}var n=function(){function e(e){this.options=e}return e.prototype.toString=function(){return JSON&&JSON.stringify?JSON.stringify(this.options):this.options},e}(),r=function(){function e(e){return"[object Array]"===Object.prototype.toString.apply(e)}function t(e){return"[object String]"===Object.prototype.toString.apply(e)}function n(e){return"[object Number]"===Object.prototype.toString.apply(e)}function r(e){return"[object Boolean]"===Object.prototype.toString.apply(e)}function i(e,t){var n,r="",i=!0;for(n=0;n<e.length;n+=1)i?i=!1:r+=t,r+=e[n];return r}function o(e,t){for(var n=[],r=0;r<e.length;r+=1)n.push(t(e[r]));return n}function s(e,t){for(var n=[],r=0;r<e.length;r+=1)t(e[r])&&n.push(e[r]);return n}function a(e){if("object"!=typeof e||null===e)return e;Object.freeze(e);var t,n;for(n in e)e.hasOwnProperty(n)&&(t=e[n],"object"==typeof t&&u(t));return e}function u(e){return"function"==typeof Object.freeze?a(e):e}return{isArray:e,isString:t,isNumber:n,isBoolean:r,join:i,map:o,filter:s,deepFreeze:u}}(),i=function(){function e(e){return e>="a"&&"z">=e||e>="A"&&"Z">=e}function t(e){return e>="0"&&"9">=e}function n(e){return t(e)||e>="a"&&"f">=e||e>="A"&&"F">=e}return{isAlpha:e,isDigit:t,isHexDigit:n}}(),o=function(){function e(e){var t,n,r="",i=s.encode(e);for(n=0;n<i.length;n+=1)t=i.charCodeAt(n),r+="%"+t.toString(16).toUpperCase();return r}function t(e,t){return"%"===e.charAt(t)&&i.isHexDigit(e.charAt(t+1))&&i.isHexDigit(e.charAt(t+2))}function n(e,t){return parseInt(e.substr(t,2),16)}function r(e){if(!t(e,0))return!1;var r=n(e,1),i=s.numBytes(r);if(0===i)return!1;for(var o=1;i>o;o+=1)if(!t(e,3*o)||!s.isValidFollowingCharCode(n(e,3*o+1)))return!1;return!0}function o(e,r){var i=e.charAt(r);if(!t(e,r))return i;var o=n(e,r+1),a=s.numBytes(o);if(0===a)return i;for(var u=1;a>u;u+=1)if(!t(e,r+3*u)||!s.isValidFollowingCharCode(n(e,r+3*u+1)))return i;return e.substr(r,3*a)}var s={encode:function(e){return unescape(encodeURIComponent(e))},numBytes:function(e){return 127>=e?1:e>=194&&223>=e?2:e>=224&&239>=e?3:e>=240&&244>=e?4:0},isValidFollowingCharCode:function(e){return e>=128&&191>=e}};return{encodeCharacter:e,isPctEncoded:r,pctCharAt:o}}(),s=function(){function e(e){return i.isAlpha(e)||i.isDigit(e)||"_"===e||o.isPctEncoded(e)}function t(e){return i.isAlpha(e)||i.isDigit(e)||"-"===e||"."===e||"_"===e||"~"===e}function n(e){return":"===e||"/"===e||"?"===e||"#"===e||"["===e||"]"===e||"@"===e||"!"===e||"$"===e||"&"===e||"("===e||")"===e||"*"===e||"+"===e||","===e||";"===e||"="===e||"'"===e}return{isVarchar:e,isUnreserved:t,isReserved:n}}(),a=function(){function e(e,t){var n,r="",i="";for(("number"==typeof e||"boolean"==typeof e)&&(e=e.toString()),n=0;n<e.length;n+=i.length)i=e.charAt(n),r+=s.isUnreserved(i)||t&&s.isReserved(i)?i:o.encodeCharacter(i);return r}function t(t){return e(t,!0)}function n(e,t){var n=o.pctCharAt(e,t);return n.length>1?n:s.isReserved(n)||s.isUnreserved(n)?n:o.encodeCharacter(n)}function r(e){var t,n="",r="";for(t=0;t<e.length;t+=r.length)r=o.pctCharAt(e,t),n+=r.length>1?r:s.isReserved(r)||s.isUnreserved(r)?r:o.encodeCharacter(r);return n}return{encode:e,encodePassReserved:t,encodeLiteral:r,encodeLiteralCharacter:n}}(),u=function(){function e(e){t[e]={symbol:e,separator:"?"===e?"&":""===e||"+"===e||"#"===e?",":e,named:";"===e||"&"===e||"?"===e,ifEmpty:"&"===e||"?"===e?"=":"",first:"+"===e?"":e,encode:"+"===e||"#"===e?a.encodePassReserved:a.encode,toString:function(){return this.symbol}}}var t={};return e(""),e("+"),e("#"),e("."),e("/"),e(";"),e("?"),e("&"),{valueOf:function(e){return t[e]?t[e]:"=,!@|".indexOf(e)>=0?null:t[""]}}}(),f=function(){function e(e){this.literal=a.encodeLiteral(e)}return e.prototype.expand=function(){return this.literal},e.prototype.toString=e.prototype.expand,e}(),p=function(){function e(e){function t(){var t=e.substring(h,f);if(0===t.length)throw new n({expressionText:e,message:"a varname must be specified",position:f});c={varname:t,exploded:!1,maxLength:null},h=null}function r(){if(d===f)throw new n({expressionText:e,message:"after a ':' you have to specify the length",position:f});c.maxLength=parseInt(e.substring(d,f),10),d=null}var a,f,p=[],c=null,h=null,d=null,g="";for(a=function(t){var r=u.valueOf(t);if(null===r)throw new n({expressionText:e,message:"illegal use of reserved operator",position:f,operator:t});return r}(e.charAt(0)),f=a.symbol.length,h=f;f<e.length;f+=g.length){if(g=o.pctCharAt(e,f),null!==h){if("."===g){if(h===f)throw new n({expressionText:e,message:"a varname MUST NOT start with a dot",position:f});continue}if(s.isVarchar(g))continue;t()}if(null!==d){if(f===d&&"0"===g)throw new n({expressionText:e,message:"A :prefix must not start with digit 0",position:f});if(i.isDigit(g)){if(f-d>=4)throw new n({expressionText:e,message:"A :prefix must have max 4 digits",position:f});continue}r()}if(":"!==g)if("*"!==g){if(","!==g)throw new n({expressionText:e,message:"illegal character",character:g,position:f});p.push(c),c=null,h=f+1}else{if(null===c)throw new n({expressionText:e,message:"exploded without varspec",position:f});if(c.exploded)throw new n({expressionText:e,message:"exploded twice",position:f});if(c.maxLength)throw new n({expressionText:e,message:"an explode (*) MUST NOT follow to a prefix",position:f});c.exploded=!0}else{if(null!==c.maxLength)throw new n({expressionText:e,message:"only one :maxLength is allowed per varspec",position:f});if(c.exploded)throw new n({expressionText:e,message:"an exploeded varspec MUST NOT be varspeced",position:f});d=f+1}}return null!==h&&t(),null!==d&&r(),p.push(c),new l(e,a,p)}function t(t){var r,i,o=[],s=null,a=0;for(r=0;r<t.length;r+=1)if(i=t.charAt(r),null===a){if(null===s)throw new Error("reached unreachable code");if("{"===i)throw new n({templateText:t,message:"brace already opened",position:r});if("}"===i){if(s+1===r)throw new n({templateText:t,message:"empty braces",position:s});try{o.push(e(t.substring(s+1,r)))}catch(u){if(u.prototype===n.prototype)throw new n({templateText:t,message:u.options.message,position:s+u.options.position,details:u.options});throw u}s=null,a=r+1}}else{if("}"===i)throw new n({templateText:t,message:"unopened brace closed",position:r});"{"===i&&(r>a&&o.push(new f(t.substring(a,r))),a=null,s=r)}if(null!==s)throw new n({templateText:t,message:"unclosed brace",position:s});return a<t.length&&o.push(new f(t.substr(a))),new c(t,o)}return t}(),l=function(){function e(e){return JSON&&JSON.stringify?JSON.stringify(e):e}function n(e){if(!t(e))return!0;if(r.isString(e))return""===e;if(r.isNumber(e)||r.isBoolean(e))return!1;if(r.isArray(e))return 0===e.length;for(var n in e)if(e.hasOwnProperty(n))return!1;return!0}function i(e){var t,n=[];for(t in e)e.hasOwnProperty(t)&&n.push({name:t,value:e[t]});return n}function o(e,t,n){this.templateText=e,this.operator=t,this.varspecs=n}function s(e,t,n){var r="";if(n=n.toString(),t.named){if(r+=a.encodeLiteral(e.varname),""===n)return r+=t.ifEmpty;r+="="}return null!==e.maxLength&&(n=n.substr(0,e.maxLength)),r+=t.encode(n)}function u(e){return t(e.value)}function f(e,o,s){var f=[],p="";if(o.named){if(p+=a.encodeLiteral(e.varname),n(s))return p+=o.ifEmpty;p+="="}return r.isArray(s)?(f=s,f=r.filter(f,t),f=r.map(f,o.encode),p+=r.join(f,",")):(f=i(s),f=r.filter(f,u),f=r.map(f,function(e){return o.encode(e.name)+","+o.encode(e.value)}),p+=r.join(f,",")),p}function p(e,o,s){var f=r.isArray(s),p=[];return f?(p=s,p=r.filter(p,t),p=r.map(p,function(t){var r=a.encodeLiteral(e.varname);return r+=n(t)?o.ifEmpty:"="+o.encode(t)})):(p=i(s),p=r.filter(p,u),p=r.map(p,function(e){var t=a.encodeLiteral(e.name);return t+=n(e.value)?o.ifEmpty:"="+o.encode(e.value)})),r.join(p,o.separator)}function l(e,n){var o=[],s="";return r.isArray(n)?(o=n,o=r.filter(o,t),o=r.map(o,e.encode),s+=r.join(o,e.separator)):(o=i(n),o=r.filter(o,function(e){return t(e.value)}),o=r.map(o,function(t){return e.encode(t.name)+"="+e.encode(t.value)}),s+=r.join(o,e.separator)),s}return o.prototype.toString=function(){return this.templateText},o.prototype.expand=function(i){var o,a,u,c,h=[],d=!1,g=this.operator;for(o=0;o<this.varspecs.length;o+=1)if(a=this.varspecs[o],u=i[a.varname],null!==u&&void 0!==u)if(a.exploded&&(d=!0),c=r.isArray(u),"string"==typeof u||"number"==typeof u||"boolean"==typeof u)h.push(s(a,g,u));else{if(a.maxLength&&t(u))throw new Error("Prefix modifiers are not applicable to variables that have composite values. You tried to expand "+this+" with "+e(u));a.exploded?t(u)&&(g.named?h.push(p(a,g,u)):h.push(l(g,u))):(g.named||!n(u))&&h.push(f(a,g,u))}return 0===h.length?"":g.first+r.join(h,g.separator)},o}(),c=function(){function e(e,t){this.templateText=e,this.expressions=t,r.deepFreeze(this)}return e.prototype.toString=function(){return this.templateText},e.prototype.expand=function(e){var t,n="";for(t=0;t<this.expressions.length;t+=1)n+=this.expressions[t].expand(e);return n},e.parse=p,e.UriTemplateError=n,e}();e(c)}(function(e){"use strict";"undefined"!=typeof module?module.exports=e:"function"==typeof define?define([],function(){return e}):"undefined"!=typeof window?window.UriTemplate=e:global.UriTemplate=e});
\ No newline at end of file \ No newline at end of file
/*jshint */
/*global unescape, module, define, window, global*/ /*global unescape, module, define, window, global*/
/* /*
...@@ -32,6 +31,18 @@ var objectHelper = (function () { ...@@ -32,6 +31,18 @@ var objectHelper = (function () {
return Object.prototype.toString.apply(value) === '[object Array]'; return Object.prototype.toString.apply(value) === '[object Array]';
} }
function isString (value) {
return Object.prototype.toString.apply(value) === '[object String]';
}
function isNumber (value) {
return Object.prototype.toString.apply(value) === '[object Number]';
}
function isBoolean (value) {
return Object.prototype.toString.apply(value) === '[object Boolean]';
}
function join (arr, separator) { function join (arr, separator) {
var var
result = '', result = '',
...@@ -99,6 +110,9 @@ var objectHelper = (function () { ...@@ -99,6 +110,9 @@ var objectHelper = (function () {
return { return {
isArray: isArray, isArray: isArray,
isString: isString,
isNumber: isNumber,
isBoolean: isBoolean,
join: join, join: join,
map: map, map: map,
filter: filter, filter: filter,
...@@ -179,7 +193,7 @@ var pctEncoder = (function () { ...@@ -179,7 +193,7 @@ var pctEncoder = (function () {
* @return {boolean|*|*} * @return {boolean|*|*}
*/ */
function isPercentDigitDigit (text, start) { function isPercentDigitDigit (text, start) {
return text[start] === '%' && charHelper.isHexDigit(text[start + 1]) && charHelper.isHexDigit(text[start + 2]); return text.charAt(start) === '%' && charHelper.isHexDigit(text.charAt(start + 1)) && charHelper.isHexDigit(text.charAt(start + 2));
} }
/** /**
...@@ -221,7 +235,7 @@ var pctEncoder = (function () { ...@@ -221,7 +235,7 @@ var pctEncoder = (function () {
* @return the character or pct-string of the text at startIndex * @return the character or pct-string of the text at startIndex
*/ */
function pctCharAt(text, startIndex) { function pctCharAt(text, startIndex) {
var chr = text[startIndex]; var chr = text.charAt(startIndex);
if (!isPercentDigitDigit(text, startIndex)) { if (!isPercentDigitDigit(text, startIndex)) {
return chr; return chr;
} }
...@@ -609,15 +623,18 @@ var parse = (function () { ...@@ -609,15 +623,18 @@ var parse = (function () {
var VariableExpression = (function () { var VariableExpression = (function () {
// helper function if JSON is not available // helper function if JSON is not available
function prettyPrint (value) { function prettyPrint (value) {
return JSON ? JSON.stringify(value) : value; return (JSON && JSON.stringify) ? JSON.stringify(value) : value;
} }
function isEmpty (value) { function isEmpty (value) {
if (!isDefined(value)) { if (!isDefined(value)) {
return true; return true;
} }
if (value === '') { if (objectHelper.isString(value)) {
return true; return value === '';
}
if (objectHelper.isNumber(value) || objectHelper.isBoolean(value)) {
return false;
} }
if (objectHelper.isArray(value)) { if (objectHelper.isArray(value)) {
return value.length === 0; return value.length === 0;
......
...@@ -16,12 +16,13 @@ ...@@ -16,12 +16,13 @@
first: "1", first: "1",
second: 2 second: 2
} }
}; },
expanded = UriTemplate.parse(templateText).expand(variables);
document.getElementById('id').innerHTML = document.getElementById('id').innerHTML =
"<p>When you have a template of the form</p><p><code>var templateText = \"" + templateText "<p>When you have a template of the form</p><p><code>var templateText = \"" + templateText
+ "\";</code></p><p>and params of the form </p><p><code>var variables = " + JSON.stringify(variables) + "\";</code></p><p>and params of the form </p><p><code>var variables = {query: {first: \"1\", second: 2}}"
+ ";</code></p><p>, you can use </p><p><code>UriTemplate.parse(templateText).expand(variables); </code></p><p>to produce </p><p><code>" + ";</code></p><p>, you can use </p><p><code>UriTemplate.parse(templateText).expand(variables); </code></p><p>to produce </p><p><code>"
+ UriTemplate.parse(templateText).expand(variables) + expanded
+ "</code></p><p> Look at the source code of this page!</p>"; + "</code></p><p> Look at the source code of this page!</p>";
}()); }());
</script> </script>
......
...@@ -33,7 +33,7 @@ ...@@ -33,7 +33,7 @@
"uritemplate-test/spec-examples-by-sections.json", "uritemplate-test/spec-examples-by-sections.json",
"uritemplate-test/spec-examples.json" "uritemplate-test/spec-examples.json"
], ],
"version": "0.3.1", "version": "0.3.2",
"readmeFilename": "README.md", "readmeFilename": "README.md",
"gitHead": "901b85201a821427dfb4591b56aea3a70d45c67c", "gitHead": "901b85201a821427dfb4591b56aea3a70d45c67c",
"devDependencies": { "devDependencies": {
......
...@@ -4,15 +4,18 @@ var VariableExpression = (function () { ...@@ -4,15 +4,18 @@ var VariableExpression = (function () {
"use strict"; "use strict";
// helper function if JSON is not available // helper function if JSON is not available
function prettyPrint (value) { function prettyPrint (value) {
return JSON ? JSON.stringify(value) : value; return (JSON && JSON.stringify) ? JSON.stringify(value) : value;
} }
function isEmpty (value) { function isEmpty (value) {
if (!isDefined(value)) { if (!isDefined(value)) {
return true; return true;
} }
if (value === '') { if (objectHelper.isString(value)) {
return true; return value === '';
}
if (objectHelper.isNumber(value) || objectHelper.isBoolean(value)) {
return false;
} }
if (objectHelper.isArray(value)) { if (objectHelper.isArray(value)) {
return value.length === 0; return value.length === 0;
......
...@@ -5,6 +5,18 @@ var objectHelper = (function () { ...@@ -5,6 +5,18 @@ var objectHelper = (function () {
return Object.prototype.toString.apply(value) === '[object Array]'; return Object.prototype.toString.apply(value) === '[object Array]';
} }
function isString (value) {
return Object.prototype.toString.apply(value) === '[object String]';
}
function isNumber (value) {
return Object.prototype.toString.apply(value) === '[object Number]';
}
function isBoolean (value) {
return Object.prototype.toString.apply(value) === '[object Boolean]';
}
function join (arr, separator) { function join (arr, separator) {
var var
result = '', result = '',
...@@ -72,6 +84,9 @@ var objectHelper = (function () { ...@@ -72,6 +84,9 @@ var objectHelper = (function () {
return { return {
isArray: isArray, isArray: isArray,
isString: isString,
isNumber: isNumber,
isBoolean: isBoolean,
join: join, join: join,
map: map, map: map,
filter: filter, filter: filter,
......
...@@ -53,7 +53,7 @@ var pctEncoder = (function () { ...@@ -53,7 +53,7 @@ var pctEncoder = (function () {
* @return {boolean|*|*} * @return {boolean|*|*}
*/ */
function isPercentDigitDigit (text, start) { function isPercentDigitDigit (text, start) {
return text[start] === '%' && charHelper.isHexDigit(text[start + 1]) && charHelper.isHexDigit(text[start + 2]); return text.charAt(start) === '%' && charHelper.isHexDigit(text.charAt(start + 1)) && charHelper.isHexDigit(text.charAt(start + 2));
} }
/** /**
...@@ -95,7 +95,7 @@ var pctEncoder = (function () { ...@@ -95,7 +95,7 @@ var pctEncoder = (function () {
* @return the character or pct-string of the text at startIndex * @return the character or pct-string of the text at startIndex
*/ */
function pctCharAt(text, startIndex) { function pctCharAt(text, startIndex) {
var chr = text[startIndex]; var chr = text.charAt(startIndex);
if (!isPercentDigitDigit(text, startIndex)) { if (!isPercentDigitDigit(text, startIndex)) {
return chr; return chr;
} }
......
/*jshint */
/*global unescape, module, define, window, global*/ /*global unescape, module, define, window, global*/
/* /*
......
...@@ -106,7 +106,7 @@ module.exports = (function () { ...@@ -106,7 +106,7 @@ module.exports = (function () {
test.done(); test.done();
} }
var SPEC_HOME = '../uritemplate-test'; var SPEC_HOME = 'uritemplate-test';
return { return {
'spec examples': function (test) { 'spec examples': function (test) {
......
...@@ -58,12 +58,41 @@ module.exports = (function () { ...@@ -58,12 +58,41 @@ module.exports = (function () {
test.equal(ve.expand({keys: {a: 'a', b: 'b', c: 'c'}}), 'a=a,b=b,c=c'); test.equal(ve.expand({keys: {a: 'a', b: 'b', c: 'c'}}), 'a=a,b=b,c=c');
test.done(); test.done();
}, },
'a map works with numbers': function (test) {
var ve = new VariableExpression("{keys*}", operators.valueOf(''), [
{varname: 'keys', exploded: true, maxLength: null}
]);
test.equal(ve.expand({keys: {a: 'a', two: 2}}), 'a=a,two=2');
test.done();
},
'a map works with booleans': function (test) {
var ve = new VariableExpression("{keys*}", operators.valueOf(''), [
{varname: 'keys', exploded: true, maxLength: null}
]);
test.equal(ve.expand({keys: {a: 'a', true: true}}), 'a=a,true=true');
test.done();
},
'a empty map prints no operator': function (test) { 'a empty map prints no operator': function (test) {
var ve = new VariableExpression("{.empty_map*}", operators.valueOf('.'), [ var ve = new VariableExpression("{.empty_map*}", operators.valueOf('.'), [
{varname: 'empty_map', exploded: true, maxLength: null} {varname: 'empty_map', exploded: true, maxLength: null}
]); ]);
test.equal(ve.expand({empty_map: {}}), ''); test.equal(ve.expand({empty_map: {}}), '');
test.done(); test.done();
},
'empty elements have the name and =': function (test) {
var ve = new VariableExpression("{?map*}", operators.valueOf('?'), [
{varname: 'map', exploded: true, maxLength: null}
]);
test.equal(ve.expand({map: {a:'a', empty: ''}}), '?a=a&empty=');
test.done();
},
'empty elements have the name and no =, if the operator has no ifemp ': function (test) {
var ve = new VariableExpression("{;map*}", operators.valueOf(';'), [
{varname: 'map', exploded: true, maxLength: null}
]);
test.equal(ve.expand({map: {a:'a', empty: ''}}), ';a=a;empty');
test.done();
} }
}, },
'named': { 'named': {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment