Skip to content

Commit 5b39fcc

Browse files
committed
Merge pull request #105 from sethkinast/eq-key-undef
Comparison helpers should still execute when their key parameter is set, but it resolves to undefined.
2 parents 91ec158 + 5f4c276 commit 5b39fcc

File tree

2 files changed

+23
-12
lines changed

2 files changed

+23
-12
lines changed

lib/dust-helpers.js

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,17 @@ function filter(chunk, context, bodies, params, filterOp) {
3333
actualKey,
3434
expectedValue,
3535
filterOpType = params.filterOpType || '';
36+
3637
// when @eq, @lt etc are used as standalone helpers, key is required and hence check for defined
37-
if ( typeof params.key !== "undefined") {
38+
if (params.hasOwnProperty("key")) {
3839
actualKey = dust.helpers.tap(params.key, chunk, context);
39-
}
40-
else if (isSelect(context)) {
40+
} else if (isSelect(context)) {
4141
actualKey = context.current().selectKey;
4242
// supports only one of the blocks in the select to be selected
4343
if (context.current().isResolved) {
4444
filterOp = function() { return false; };
4545
}
46-
}
47-
else {
46+
} else {
4847
_log("No key specified for filter in:" + filterOpType + " helper ");
4948
return chunk;
5049
}
@@ -57,19 +56,17 @@ function filter(chunk, context, bodies, params, filterOp) {
5756
// we want helpers without bodies to fail gracefully so check it first
5857
if(body) {
5958
return chunk.render(body, context);
60-
}
61-
else {
62-
_log("No key specified for filter in:" + filterOpType + " helper ");
59+
} else {
60+
_log("No body specified for " + filterOpType + " helper ");
6361
return chunk;
6462
}
65-
}
66-
else if (bodies['else']) {
63+
} else if (bodies['else']) {
6764
return chunk.render(bodies['else'], context);
6865
}
6966
return chunk;
7067
}
7168

72-
function coerce (value, type, context) {
69+
function coerce(value, type, context) {
7370
if (typeof value !== "undefined") {
7471
switch (type || typeof value) {
7572
case 'number': return +value;

test/jasmine-test/spec/helpersTests.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -497,11 +497,25 @@
497497
},
498498
{
499499
name: "eq helper with no params",
500-
source: "{@eq}Hello{/eq}",
500+
source: "{@eq}Hello{:else}Goodbye{/eq}",
501501
context: {},
502502
expected: "",
503503
message: "eq helper with no params does not execute"
504504
},
505+
{
506+
name: "eq helper with key that resolves to undefined",
507+
source: "{@eq key=foo value=\"0\"}Hello{:else}Goodbye{/eq}",
508+
context: {},
509+
expected: "Goodbye",
510+
message: "eq helper with key that resolves to undefined uses that as comparison"
511+
},
512+
{
513+
name: "eq helper with both key and value undefined",
514+
source: "{@eq key=foo value=bar}Hello{:else}Goodbye{/eq}",
515+
context: {},
516+
expected: "Hello",
517+
message: "eq helper with key and value that both resolve to undefined is true"
518+
},
505519
{
506520
name: "eq helper matching string case",
507521
source: "{@eq key=\"foo\" value=\"foo\"}equal{/eq}",

0 commit comments

Comments
 (0)