Skip to content

Commit

Permalink
Merge pull request #104 from sethkinast/coerce-false
Browse files Browse the repository at this point in the history
Coerce falsy values when a type is specified by a comparison helper.
  • Loading branch information
prashn64 committed Nov 20, 2014
2 parents 35bf5eb + 856d628 commit 91ec158
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
7 changes: 3 additions & 4 deletions lib/dust-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,13 @@ function filter(chunk, context, bodies, params, filterOp) {
}

function coerce (value, type, context) {
if (value) {
switch (type || typeof(value)) {
if (typeof value !== "undefined") {
switch (type || typeof value) {
case 'number': return +value;
case 'string': return String(value);
case 'boolean': {
case 'boolean':
value = (value === 'false' ? false : value);
return Boolean(value);
}
case 'date': return new Date(value);
case 'context': return context.get(value);
}
Expand Down
7 changes: 7 additions & 0 deletions test/jasmine-test/spec/helpersTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,13 @@
expected: "",
message: "eq helper non equal boolean case"
},
{
name: "eq helper coerce falsy case",
source: "{@eq key=x value=\"0\" type=\"string\"}equal{/eq}",
context: {x:0},
expected: "equal",
message: "eq helper should coerce falsy booleans"
},
{
name: "eq helper without a body",
source: "{@eq key=\"abc\" value=\"java\"/}",
Expand Down

0 comments on commit 91ec158

Please sign in to comment.