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
......
This diff is collapsed.
/*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