Commit b03daea2 authored by fxa's avatar fxa

some minor code cleanups

parent 50a245c7
...@@ -6,23 +6,25 @@ and can expand templates up to and including Level 4 in that specification. ...@@ -6,23 +6,25 @@ and can expand templates up to and including Level 4 in that specification.
It exposes a constructor function UriTemplate with the two methods: It exposes a constructor function UriTemplate with the two methods:
* (static) parse(uriTemplateText) * (static) parse(uriTemplateText) returning an instance of UriTemplate
* expand(variables) * expand(variables) returning an string
Requirements Requirements
------------ ------------
You can use uritemplate.js in any modern browsers (Tested even with IE8 in IE7-Mode), see file demo.html. You can use uritemplate.js in any even not so modern browser (Tested even with IE8 in IE7-Mode), see file demo.html.
But you can also use it with node: But you can also use it with node:
npm install uritemplate **npm install uritemplate**
and then:
var var
UriTemplate = require('uritemplate'), UriTemplate = require('uritemplate'),
template; template;
template = UriTemplate.parse('{?query*}'; template = UriTemplate.parse('{?query*}';
template.expand({query: {first: 1, second: 2}}); template.expand({query: {first: 1, second: 2}});
--> "?firstParam=firstValue&secondParam=secondValue" --> "?first=1&second=2"
Tests Tests
----- -----
...@@ -36,6 +38,7 @@ Comming soon ...@@ -36,6 +38,7 @@ Comming soon
------------ ------------
* A new method extract(uri), which tries to extract the variables from a given uri * A new method extract(uri), which tries to extract the variables from a given uri
* Support of javascript's Date class
License License
------- -------
......
...@@ -23,5 +23,5 @@ ...@@ -23,5 +23,5 @@
"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.1.1" "version" : "0.1.2"
} }
\ No newline at end of file
/*jshint browser:true, bitwise:true, curly:true, devel: true, eqeqeq:true, forin:true, immed:true, latedef:true, newcap:true, noarg:true, nonew:true, undef:true */
/*global module */
(function (exportCallback) { (function (exportCallback) {
"use strict"; "use strict";
...@@ -36,7 +34,7 @@ ...@@ -36,7 +34,7 @@
} }
function reduce(arrayOrObject, callback, initialValue) { function reduce(arrayOrObject, callback, initialValue) {
return (isArray(arrayOrObject)) ? arrayReduce(arrayOrObject, callback, initialValue) : objectReduce(arrayOrObject, callback, initialValue); return isArray(arrayOrObject) ? arrayReduce(arrayOrObject, callback, initialValue) : objectReduce(arrayOrObject, callback, initialValue);
} }
/** /**
...@@ -68,11 +66,10 @@ ...@@ -68,11 +66,10 @@
// even the empty string is considered as defined // even the empty string is considered as defined
return true; return true;
} }
// else Object
for (propertyName in object) { for (propertyName in object) {
if (object.hasOwnProperty(propertyName)) { if (object.hasOwnProperty(propertyName) && isDefined(object[propertyName])) {
if(isDefined(object[propertyName])) { return true;
return true;
}
} }
} }
return false; return false;
...@@ -83,7 +80,7 @@ ...@@ -83,7 +80,7 @@
} }
function isDigit(chr) { function isDigit(chr) {
return (chr >= '0' && chr <= '9'); return chr >= '0' && chr <= '9';
} }
function isHexDigit(chr) { function isHexDigit(chr) {
...@@ -141,73 +138,59 @@ ...@@ -141,73 +138,59 @@
// pass three chars to the result // pass three chars to the result
result += text.substr(index, 3); result += text.substr(index, 3);
index += 2; index += 2;
}else { } else {
result += '%25'; result += '%25';
} }
} }
else if (isUnreserved(chr) || (passReserved && isReserved(chr))) {
result += chr;
}
else { else {
result += pctEncode(chr); result += isUnreserved(chr) || (passReserved && isReserved(chr)) ? chr : pctEncode(chr);
} }
} }
return result; return result;
} }
function encodePassReserved (text) { function encodePassReserved(text) {
return encode(text, true); return encode(text, true);
} }
function Operator(symbol, encode) {
this.symbol = symbol;
this.encode = encode;
}
Operator.prototype.separator = function () {
return (this.symbol === '?') ? '&' : (this.symbol === '' || this.symbol === '+' || this.symbol === '#') ? ',' : this.symbol;
};
Operator.prototype.named = function () {
return this.symbol === ';' || this.symbol === '&' || this.symbol === '?';
};
Operator.prototype.ifEmpty = function () {
return (this.symbol === '&' || this.symbol === '?') ? '=' : '';
};
Operator.prototype.first = function () {
return (this.symbol === '+' ) ? '' : this.symbol;
};
Operator.bySymbol = {
'': new Operator('', encode),
'+': new Operator('+', encodePassReserved),
'#': new Operator('#', encodePassReserved),
'.': new Operator('.', encode),
'/': new Operator('/', encode),
';': new Operator(';', encode),
'?': new Operator('?', encode),
'&': new Operator('&', encode)
};
Operator.valueOf = function (chr) {
if (Operator.bySymbol[chr]) {
return Operator.bySymbol[chr];
}
if ("=,!@|".indexOf(chr) >= 0) {
throw new Error('Illegal use of reserved operator "' + chr + '"');
}
return Operator.bySymbol[''];
};
var var
UriTemplate; operators = (function () {
var
bySymbol = {};
function create(symbol) {
bySymbol[symbol] = {
symbol: symbol,
separator: (symbol === '?') ? '&' : (symbol === '' || symbol === '+' || symbol === '#') ? ',' : symbol,
named: symbol === ';' || symbol === '&' || symbol === '?',
ifEmpty: (symbol === '&' || symbol === '?') ? '=' : '',
first: (symbol === '+' ) ? '' : symbol,
encode: (symbol === '+' || symbol === '#') ? encodePassReserved : encode,
toString: function () {return this.symbol;}
};
}
create('');
create('+');
create('#');
create('.');
create('/');
create(';');
create('?');
create('&');
return {valueOf: function (chr) {
if (bySymbol[chr]) {
return bySymbol[chr];
}
if ("=,!@|".indexOf(chr) >= 0) {
throw new Error('Illegal use of reserved operator "' + chr + '"');
}
return bySymbol[''];
}}
}());
UriTemplate = function (templateText, expressions) { function UriTemplate(templateText, expressions) {
this.templateText = templateText; this.templateText = templateText;
this.experssions = expressions; this.experssions = expressions;
}; }
UriTemplate.prototype.toString = function () { UriTemplate.prototype.toString = function () {
return this.templateText; return this.templateText;
...@@ -240,12 +223,7 @@ ...@@ -240,12 +223,7 @@
} }
} }
else { else {
if (isReserved(chr) || isUnreserved(chr)) { result += isReserved(chr) || isUnreserved(chr) ? chr : pctEncode(chr);
result += chr;
}
else {
result += pctEncode(chr);
}
} }
} }
return result; return result;
...@@ -298,9 +276,9 @@ ...@@ -298,9 +276,9 @@
function reduceNamedExploded(result, currentValue, currentKey) { function reduceNamedExploded(result, currentValue, currentKey) {
if (isDefined(currentValue)) { if (isDefined(currentValue)) {
if (result.length > 0) { if (result.length > 0) {
result += operator.separator(); result += operator.separator;
} }
result += (valueIsArr) ? operator.encode(varspec.varname) : operator.encode(currentKey); result += (valueIsArr) ? encodeLiteral(varspec.varname) : operator.encode(currentKey);
result += '=' + operator.encode(currentValue); result += '=' + operator.encode(currentValue);
} }
return result; return result;
...@@ -309,7 +287,7 @@ ...@@ -309,7 +287,7 @@
function reduceUnnamedExploded(result, currentValue, currentKey) { function reduceUnnamedExploded(result, currentValue, currentKey) {
if (isDefined(currentValue)) { if (isDefined(currentValue)) {
if (result.length > 0) { if (result.length > 0) {
result += operator.separator(); result += operator.separator;
} }
if (!valueIsArr) { if (!valueIsArr) {
result += operator.encode(currentKey) + '='; result += operator.encode(currentKey) + '=';
...@@ -327,24 +305,22 @@ ...@@ -327,24 +305,22 @@
continue; continue;
} }
if (isFirstVarspec) { if (isFirstVarspec) {
result += this.operator.first(); result += this.operator.first;
isFirstVarspec = false; isFirstVarspec = false;
} }
else { else {
result += this.operator.separator(); result += this.operator.separator;
} }
valueIsArr = isArray(value); valueIsArr = isArray(value);
if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") { if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
value = value.toString(); value = value.toString();
if (this.operator.named()) { if (this.operator.named) {
result += varspec.varname; result += encodeLiteral(varspec.varname);
if (value === '') { if (value === '') {
result += this.operator.ifEmpty(); result += this.operator.ifEmpty;
continue; continue;
} }
else { result += '=';
result += '=';
}
} }
if (varspec.maxLength && value.length > varspec.maxLength) { if (varspec.maxLength && value.length > varspec.maxLength) {
value = value.substr(0, varspec.maxLength); value = value.substr(0, varspec.maxLength);
...@@ -356,21 +332,19 @@ ...@@ -356,21 +332,19 @@
throw new Error('Prefix modifiers are not applicable to variables that have composite values. You tried to expand ' + this + " with " + JSON.stringify(value)); throw new Error('Prefix modifiers are not applicable to variables that have composite values. You tried to expand ' + this + " with " + JSON.stringify(value));
} }
else if (!varspec.exploded) { else if (!varspec.exploded) {
if (operator.named()) { if (operator.named) {
result += varspec.varname; result += encodeLiteral(varspec.varname);
if (!isDefined(value)) { if (!isDefined(value)) {
result += this.operator.ifEmpty(); result += this.operator.ifEmpty;
continue; continue;
} }
else { result += '=';
result += '=';
}
} }
result += reduce(value, reduceUnexploded, ''); result += reduce(value, reduceUnexploded, '');
} }
else { else {
// exploded and not string // exploded and not string
result += reduce(value, operator.named() ? reduceNamedExploded : reduceUnnamedExploded, ''); result += reduce(value, operator.named ? reduceNamedExploded : reduceUnnamedExploded, '');
} }
} }
return result; return result;
...@@ -385,7 +359,7 @@ ...@@ -385,7 +359,7 @@
varnameStart = null, varnameStart = null,
maxLengthStart = null, maxLengthStart = null,
index, index,
chr = ' '; chr;
function closeVarname() { function closeVarname() {
varspec = {varname: text.substring(varnameStart, index), exploded: false, maxLength: null}; varspec = {varname: text.substring(varnameStart, index), exploded: false, maxLength: null};
...@@ -403,7 +377,7 @@ ...@@ -403,7 +377,7 @@
for (index = 0; index < text.length; index += chr.length) { for (index = 0; index < text.length; index += chr.length) {
chr = text[index]; chr = text[index];
if (index === 0) { if (index === 0) {
operator = Operator.valueOf(chr); operator = operators.valueOf(chr);
if (operator.symbol !== '') { if (operator.symbol !== '') {
// first char is operator symbol. so we can continue // first char is operator symbol. so we can continue
varnameStart = 1; varnameStart = 1;
...@@ -486,7 +460,6 @@ ...@@ -486,7 +460,6 @@
expressions = [], expressions = [],
braceOpenIndex = null, braceOpenIndex = null,
literalStart = 0; literalStart = 0;
for (index = 0; index < uriTemplateText.length; index += 1) { for (index = 0; index < uriTemplateText.length; index += 1) {
chr = uriTemplateText.charAt(index); chr = uriTemplateText.charAt(index);
if (literalStart !== null) { if (literalStart !== null) {
...@@ -549,4 +522,3 @@ ...@@ -549,4 +522,3 @@
} }
} }
)); ));
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