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/
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.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
......
This diff is collapsed.
/*jshint */
/*global unescape, module, define, window, global*/
/*
......@@ -32,6 +31,18 @@ var objectHelper = (function () {
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) {
var
result = '',
......@@ -99,6 +110,9 @@ var objectHelper = (function () {
return {
isArray: isArray,
isString: isString,
isNumber: isNumber,
isBoolean: isBoolean,
join: join,
map: map,
filter: filter,
......@@ -179,7 +193,7 @@ var pctEncoder = (function () {
* @return {boolean|*|*}
*/
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 () {
* @return the character or pct-string of the text at startIndex
*/
function pctCharAt(text, startIndex) {
var chr = text[startIndex];
var chr = text.charAt(startIndex);
if (!isPercentDigitDigit(text, startIndex)) {
return chr;
}
......@@ -609,15 +623,18 @@ var parse = (function () {
var VariableExpression = (function () {
// helper function if JSON is not available
function prettyPrint (value) {
return JSON ? JSON.stringify(value) : value;
return (JSON && JSON.stringify) ? JSON.stringify(value) : value;
}
function isEmpty (value) {
if (!isDefined(value)) {
return true;
}
if (value === '') {
return true;
if (objectHelper.isString(value)) {
return value === '';
}
if (objectHelper.isNumber(value) || objectHelper.isBoolean(value)) {
return false;
}
if (objectHelper.isArray(value)) {
return value.length === 0;
......
......@@ -16,12 +16,13 @@
first: "1",
second: 2
}
};
},
expanded = UriTemplate.parse(templateText).expand(variables);
document.getElementById('id').innerHTML =
"<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>"
+ UriTemplate.parse(templateText).expand(variables)
+ expanded
+ "</code></p><p> Look at the source code of this page!</p>";
}());
</script>
......
......@@ -33,7 +33,7 @@
"uritemplate-test/spec-examples-by-sections.json",
"uritemplate-test/spec-examples.json"
],
"version": "0.3.1",
"version": "0.3.2",
"readmeFilename": "README.md",
"gitHead": "901b85201a821427dfb4591b56aea3a70d45c67c",
"devDependencies": {
......
......@@ -4,15 +4,18 @@ var VariableExpression = (function () {
"use strict";
// helper function if JSON is not available
function prettyPrint (value) {
return JSON ? JSON.stringify(value) : value;
return (JSON && JSON.stringify) ? JSON.stringify(value) : value;
}
function isEmpty (value) {
if (!isDefined(value)) {
return true;
}
if (value === '') {
return true;
if (objectHelper.isString(value)) {
return value === '';
}
if (objectHelper.isNumber(value) || objectHelper.isBoolean(value)) {
return false;
}
if (objectHelper.isArray(value)) {
return value.length === 0;
......
......@@ -5,6 +5,18 @@ var objectHelper = (function () {
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) {
var
result = '',
......@@ -72,6 +84,9 @@ var objectHelper = (function () {
return {
isArray: isArray,
isString: isString,
isNumber: isNumber,
isBoolean: isBoolean,
join: join,
map: map,
filter: filter,
......
......@@ -53,7 +53,7 @@ var pctEncoder = (function () {
* @return {boolean|*|*}
*/
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 () {
* @return the character or pct-string of the text at startIndex
*/
function pctCharAt(text, startIndex) {
var chr = text[startIndex];
var chr = text.charAt(startIndex);
if (!isPercentDigitDigit(text, startIndex)) {
return chr;
}
......
/*jshint */
/*global unescape, module, define, window, global*/
/*
......
......@@ -106,7 +106,7 @@ module.exports = (function () {
test.done();
}
var SPEC_HOME = '../uritemplate-test';
var SPEC_HOME = 'uritemplate-test';
return {
'spec examples': function (test) {
......
......@@ -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.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) {
var ve = new VariableExpression("{.empty_map*}", operators.valueOf('.'), [
{varname: 'empty_map', exploded: true, maxLength: null}
]);
test.equal(ve.expand({empty_map: {}}), '');
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': {
......
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