Commit 1663b469 authored by fxa's avatar fxa

fixed bug with {?empty}

parent ead0b6f0
...@@ -60,6 +60,7 @@ MIT License, see http://mit-license.org/ ...@@ -60,6 +60,7 @@ MIT License, see http://mit-license.org/
Release Notes Release Notes
------------- -------------
* 0.2.3 fixed bug with empty objects ('{?empty}' with '{empty:{}}' shall expand to '?empty=')
* 0.2.2 fixed pct encoding bug with multibyte utf8 chars * 0.2.2 fixed pct encoding bug with multibyte utf8 chars
* 0.2.1 fixed a bug in package.json * 0.2.1 fixed a bug in package.json
* 0.2.0 heavy project refactoring, splitting source files, introducing jshint (preparation of next steps) * 0.2.0 heavy project refactoring, splitting source files, introducing jshint (preparation of next steps)
......
This diff is collapsed.
...@@ -584,22 +584,22 @@ var VariableExpression = (function () { ...@@ -584,22 +584,22 @@ var VariableExpression = (function () {
varspec = this.varspecs[index]; varspec = this.varspecs[index];
value = variables[varspec.varname]; value = variables[varspec.varname];
if (!isDefined(value)) { if (!isDefined(value)) {
continue; continue;
} }
if (isFirstVarspec) { if (isFirstVarspec) {
result += this.operator.first; result += operator.first;
isFirstVarspec = false; isFirstVarspec = false;
} }
else { else {
result += this.operator.separator; result += operator.separator;
} }
valueIsArr = objectHelper.isArray(value); valueIsArr = objectHelper.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 (operator.named) {
result += LiteralExpression.encodeLiteral(varspec.varname); result += LiteralExpression.encodeLiteral(varspec.varname);
if (value === '') { if (value === '') {
result += this.operator.ifEmpty; result += operator.ifEmpty;
continue; continue;
} }
result += '='; result += '=';
...@@ -607,7 +607,7 @@ var VariableExpression = (function () { ...@@ -607,7 +607,7 @@ var VariableExpression = (function () {
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);
} }
result += this.operator.encode(value); result += operator.encode(value);
} }
else if (varspec.maxLength) { else if (varspec.maxLength) {
// 2.4.1 of the spec says: "Prefix modifiers are not applicable to variables that have composite values." // 2.4.1 of the spec says: "Prefix modifiers are not applicable to variables that have composite values."
...@@ -617,7 +617,7 @@ var VariableExpression = (function () { ...@@ -617,7 +617,7 @@ var VariableExpression = (function () {
if (operator.named) { if (operator.named) {
result += LiteralExpression.encodeLiteral(varspec.varname); result += LiteralExpression.encodeLiteral(varspec.varname);
if (!isDefined(value)) { if (!isDefined(value)) {
result += this.operator.ifEmpty; result += operator.ifEmpty;
continue; continue;
} }
result += '='; result += '=';
...@@ -629,6 +629,22 @@ var VariableExpression = (function () { ...@@ -629,6 +629,22 @@ var VariableExpression = (function () {
result += objectHelper.reduce(value, operator.named ? reduceNamedExploded : reduceUnnamedExploded, ''); result += objectHelper.reduce(value, operator.named ? reduceNamedExploded : reduceUnnamedExploded, '');
} }
} }
if (isFirstVarspec) {
// so no varspecs produced output.
var oneExploded = false;
for (index = 0; index < this.varspecs.length; index += 1) {
if (this.varspecs[index].exploded) {
oneExploded = true;
break;
}
}
if (operator.named && !oneExploded) {
result += operator.symbol;
result += varspec.varname + operator.ifEmpty;
}
}
return result; return result;
}; };
......
...@@ -74,19 +74,19 @@ var VariableExpression = (function () { ...@@ -74,19 +74,19 @@ var VariableExpression = (function () {
continue; continue;
} }
if (isFirstVarspec) { if (isFirstVarspec) {
result += this.operator.first; result += operator.first;
isFirstVarspec = false; isFirstVarspec = false;
} }
else { else {
result += this.operator.separator; result += operator.separator;
} }
valueIsArr = objectHelper.isArray(value); valueIsArr = objectHelper.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 (operator.named) {
result += LiteralExpression.encodeLiteral(varspec.varname); result += LiteralExpression.encodeLiteral(varspec.varname);
if (value === '') { if (value === '') {
result += this.operator.ifEmpty; result += operator.ifEmpty;
continue; continue;
} }
result += '='; result += '=';
...@@ -94,7 +94,7 @@ var VariableExpression = (function () { ...@@ -94,7 +94,7 @@ var VariableExpression = (function () {
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);
} }
result += this.operator.encode(value); result += operator.encode(value);
} }
else if (varspec.maxLength) { else if (varspec.maxLength) {
// 2.4.1 of the spec says: "Prefix modifiers are not applicable to variables that have composite values." // 2.4.1 of the spec says: "Prefix modifiers are not applicable to variables that have composite values."
...@@ -104,7 +104,7 @@ var VariableExpression = (function () { ...@@ -104,7 +104,7 @@ var VariableExpression = (function () {
if (operator.named) { if (operator.named) {
result += LiteralExpression.encodeLiteral(varspec.varname); result += LiteralExpression.encodeLiteral(varspec.varname);
if (!isDefined(value)) { if (!isDefined(value)) {
result += this.operator.ifEmpty; result += operator.ifEmpty;
continue; continue;
} }
result += '='; result += '=';
...@@ -116,6 +116,22 @@ var VariableExpression = (function () { ...@@ -116,6 +116,22 @@ var VariableExpression = (function () {
result += objectHelper.reduce(value, operator.named ? reduceNamedExploded : reduceUnnamedExploded, ''); result += objectHelper.reduce(value, operator.named ? reduceNamedExploded : reduceUnnamedExploded, '');
} }
} }
if (isFirstVarspec) {
// so no varspecs produced output.
var oneExploded = false;
for (index = 0; index < this.varspecs.length; index += 1) {
if (this.varspecs[index].exploded) {
oneExploded = true;
break;
}
}
if (operator.named && !oneExploded) {
result += operator.symbol;
result += varspec.varname + operator.ifEmpty;
}
}
return result; return result;
}; };
......
...@@ -58,6 +58,12 @@ module.exports = (function () { ...@@ -58,6 +58,12 @@ module.exports = (function () {
test.fail('chapter ' + chapterName + ', template ' + template + ' threw error: ' + JSON.stringify(exception, null, 4)); test.fail('chapter ' + chapterName + ', template ' + template + ' threw error: ' + JSON.stringify(exception, null, 4));
return; return;
} }
if (expected.constructor === Array) {
// simplyfy arrays, when only one element is in
if (expected.length === 1) {
expected = expected[0];
}
}
if (expected.constructor === Array) { if (expected.constructor === Array) {
// actual must match at least one of the listed elements // actual must match at least one of the listed elements
for (index = 0; index < expected.length; index += 1) { for (index = 0; index < expected.length; index += 1) {
......
...@@ -25,16 +25,22 @@ module.exports = (function () { ...@@ -25,16 +25,22 @@ module.exports = (function () {
test.equal(ve.expand({x: 1, y: 2}), '1,2'); test.equal(ve.expand({x: 1, y: 2}), '1,2');
test.equal(ve.expand({x: 1, y: null}), '1'); test.equal(ve.expand({x: 1, y: null}), '1');
test.done(); test.done();
} },
/* TODO this test fails "empty lists with ? must show the name": function (test) {
"exploded empty lists with ? must show the name": function (test) { console.log(JSON.stringify(test, null, 4));
var ve = new VariableExpression("{?empty}", operators.valueOf('?'), [
{varname: 'empty', exploded: false, maxLength: null}
]);
test.equal(ve.expand({empty: {}}), '?empty=');
test.done();
},
"exploded empty lists with ? must not show the name": function (test) {
console.log(JSON.stringify(test, null, 4)); console.log(JSON.stringify(test, null, 4));
var ve = new VariableExpression("{?empty*}", operators.valueOf('?'), [ var ve = new VariableExpression("{?empty*}", operators.valueOf('?'), [
{varname: 'empty', exploded: true, maxLength: null} {varname: 'empty', exploded: true, maxLength: null}
]); ]);
test.equal(ve.expand({empty: {}}), '?empty='); test.equal(ve.expand({empty: {}}), '');
test.done(); test.done();
} }
*/
}; };
}()); }());
\ No newline at end of file
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