diff --git a/lib/.dust-helpers.js.swp b/lib/.dust-helpers.js.swp new file mode 100644 index 0000000..2140641 Binary files /dev/null and b/lib/.dust-helpers.js.swp differ diff --git a/lib/dust-helpers.js b/lib/dust-helpers.js index 52272da..5254ba2 100644 --- a/lib/dust-helpers.js +++ b/lib/dust-helpers.js @@ -50,7 +50,7 @@ function filter(chunk, context, bodies, params, filterOp) { return chunk; } expectedValue = dust.helpers.tap(params.value, chunk, context); - if (filterOp(expectedValue, coerce(actualKey, params.type, context))) { + if (filterOp(coerce(expectedValue, params.type, context), coerce(actualKey, params.type, context))) { if (isSelect(context)) { context.current().isResolved = true; } @@ -74,7 +74,10 @@ function coerce (value, type, context) { switch (type || typeof(value)) { case 'number': return +value; case 'string': return String(value); - case 'boolean': return Boolean(value); + case 'boolean': { + value = (value === 'false' ? false : value); + return Boolean(value); + } case 'date': return new Date(value); case 'context': return context.get(value); } @@ -315,6 +318,24 @@ var helpers = { return filter(chunk, context, bodies, params, function(expected, actual) { return actual === expected; }); }, + /** + ne helper compares the given key is not the same as the expected value + It can be used standalone or in conjunction with select for multiple branching + @param key, The actual key to be compared ( optional when helper used in conjunction with select) + either a string literal value or a dust reference + a string literal value, is enclosed in double quotes, e.g. key="foo" + a dust reference may or may not be enclosed in double quotes, e.g. key="{val}" and key=val are both valid + @param value, The expected value to compare to, when helper is used standalone or in conjunction with select + @param type (optional), supported types are number, boolean, string, date, context, defaults to string + Note : use type="number" when comparing numeric + **/ + "ne": function(chunk, context, bodies, params) { + if(params) { + params.filterOpType = "ne"; + } + return filter(chunk, context, bodies, params, function(expected, actual) { return actual !== expected; }); + }, + /** lt helper compares the given key is less than the expected value It can be used standalone or in conjunction with select for multiple branching diff --git a/test/jasmine-test/spec/helpersTests.js b/test/jasmine-test/spec/helpersTests.js index 7a6a963..d5585e7 100644 --- a/test/jasmine-test/spec/helpersTests.js +++ b/test/jasmine-test/spec/helpersTests.js @@ -201,6 +201,76 @@ var helpersTests = [ context: {}, expected: "", message: "eq helper non matching string case missing else block" + }, + { + name: "eq helper equal boolean case", + source: "{@eq key=\"true\" value=\"true\" type=\"boolean\"}equal{/eq}", + context: {}, + expected: "equal", + message: "eq helper equal boolean case" + }, + { + name: "eq helper non equal boolean case", + source: "{@eq key=\"false\" value=\"true\" type=\"boolean\"}equal{/eq}", + context: {}, + expected: "", + message: "eq helper non equal boolean case" + }, + { + name: "ne helper with no body", + source: "{@ne key=\"foo\" value=\"foo\"/}", + context: {}, + expected: "", + message: "ne helper with no body silently fails with console log" + }, + { + name: "ne helper matching string case", + source: "{@ne key=\"foo\" value=\"foo\"}not equal{/ne}", + context: {}, + expected: "", + message: "ne helper matching string case" + }, + { + name: "ne helper non matching string case", + source: "{@ne key=\"foo\" value=\"bar\"}not equal{:else}bar{/ne}", + context: {}, + expected: "not equal", + message: "ne helper non matching string case" + }, + { + name: "ne helper non matching string case missing else block", + source: "{@ne key=\"foo\" value=\"bar\"}not equal{/ne}", + context: {}, + expected: "not equal", + message: "ne helper non matching string case missing else block" + }, + { + name: "ne helper non equal numbers case", + source: "{@ne key=\"3\" value=\"5\" type=\"number\"}not equal{/ne}", + context: {}, + expected: "not equal", + message: "ne helper non equal numbers case" + }, + { + name: "ne helper equal numbers case", + source: "{@ne key=\"3\" value=\"3\" type=\"number\"}not equal{/ne}", + context: {}, + expected: "", + message: "ne helper equal numbers case" + }, + { + name: "ne helper non equal boolean case", + source: "{@ne key=\"false\" value=\"true\" type=\"boolean\"}not equal{/ne}", + context: {}, + expected: "not equal", + message: "ne helper non equal boolean case" + }, + { + name: "ne helper equal boolean case", + source: "{@ne key=\"true\" value=\"true\" type=\"boolean\"}not equal{/ne}", + context: {}, + expected: "", + message: "ne helper equal boolean case" }, { name: "lt helper with no body",