Skip to content

Commit

Permalink
Comparison helpers should still execute when their key parameter is s…
Browse files Browse the repository at this point in the history
…et but resolves to undefined.

Previously, if a helper's key parameter was set, but the Dust variable to which it pointed did not exist, the helper would immediately abort. However, this breaks cases especially for {@ne} in which it would be completely valid if the key did not exist.

The helper will still fail to execute if a key parameter is not passed. However, if the parameter resolves to an undefined variable, it will now execute.

This is a non-backwards-compatible change. Closes #59.
  • Loading branch information
Seth Kinast committed Nov 18, 2014
1 parent 35bf5eb commit e22984a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
19 changes: 8 additions & 11 deletions lib/dust-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,17 @@ function filter(chunk, context, bodies, params, filterOp) {
actualKey,
expectedValue,
filterOpType = params.filterOpType || '';

// when @eq, @lt etc are used as standalone helpers, key is required and hence check for defined
if ( typeof params.key !== "undefined") {
if (params.hasOwnProperty("key")) {
actualKey = dust.helpers.tap(params.key, chunk, context);
}
else if (isSelect(context)) {
} else if (isSelect(context)) {
actualKey = context.current().selectKey;
// supports only one of the blocks in the select to be selected
if (context.current().isResolved) {
filterOp = function() { return false; };
}
}
else {
} else {
_log("No key specified for filter in:" + filterOpType + " helper ");
return chunk;
}
Expand All @@ -57,19 +56,17 @@ function filter(chunk, context, bodies, params, filterOp) {
// we want helpers without bodies to fail gracefully so check it first
if(body) {
return chunk.render(body, context);
}
else {
_log("No key specified for filter in:" + filterOpType + " helper ");
} else {
_log("No body specified for " + filterOpType + " helper ");
return chunk;
}
}
else if (bodies['else']) {
} else if (bodies['else']) {
return chunk.render(bodies['else'], context);
}
return chunk;
}

function coerce (value, type, context) {
function coerce(value, type, context) {
if (value) {
switch (type || typeof(value)) {
case 'number': return +value;
Expand Down
9 changes: 8 additions & 1 deletion test/jasmine-test/spec/helpersTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -497,11 +497,18 @@
},
{
name: "eq helper with no params",
source: "{@eq}Hello{/eq}",
source: "{@eq}Hello{:else}Goodbye{/eq}",
context: {},
expected: "",
message: "eq helper with no params does not execute"
},
{
name: "eq helper with key that resolves to undefined",
source: "{@eq key=foo value=\"0\"}Hello{:else}Goodbye{/eq}",
context: {},
expected: "Goodbye",
message: "eq helper with key that resolves to undefined executes {:else}"
},
{
name: "eq helper matching string case",
source: "{@eq key=\"foo\" value=\"foo\"}equal{/eq}",
Expand Down

0 comments on commit e22984a

Please sign in to comment.