From 4e27b6f70f247f45affe96985676429ae962a3aa Mon Sep 17 00:00:00 2001 From: Steven Foote Date: Wed, 19 Sep 2012 11:16:02 -0700 Subject: [PATCH 1/4] Add @ne helper as the complement to the @eq helper --- lib/dust-helpers.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/lib/dust-helpers.js b/lib/dust-helpers.js index 52272da..d38d350 100644 --- a/lib/dust-helpers.js +++ b/lib/dust-helpers.js @@ -315,6 +315,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 + **/ + "eq": function(chunk, context, bodies, params) { + if(params) { + params.filterOpType = "eq"; + } + 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 From e1dd9e996660a7b56c0b5c8900bdd75d862fa028 Mon Sep 17 00:00:00 2001 From: Steven Foote Date: Wed, 19 Sep 2012 11:24:29 -0700 Subject: [PATCH 2/4] Changing eq to ne. --- lib/dust-helpers.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/dust-helpers.js b/lib/dust-helpers.js index d38d350..3c3c689 100644 --- a/lib/dust-helpers.js +++ b/lib/dust-helpers.js @@ -326,7 +326,7 @@ var helpers = { @param type (optional), supported types are number, boolean, string, date, context, defaults to string Note : use type="number" when comparing numeric **/ - "eq": function(chunk, context, bodies, params) { + "ne": function(chunk, context, bodies, params) { if(params) { params.filterOpType = "eq"; } From 264ca97861d6f320c29eba4affddd0eae71080a3 Mon Sep 17 00:00:00 2001 From: Steven Foote Date: Wed, 19 Sep 2012 11:37:58 -0700 Subject: [PATCH 3/4] Add tests for @ne --- test/jasmine-test/spec/helpersTests.js | 28 ++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/test/jasmine-test/spec/helpersTests.js b/test/jasmine-test/spec/helpersTests.js index 7a6a963..576d83e 100644 --- a/test/jasmine-test/spec/helpersTests.js +++ b/test/jasmine-test/spec/helpersTests.js @@ -202,6 +202,34 @@ var helpersTests = [ expected: "", message: "eq helper non matching string case missing else block" }, + { + 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: "lt helper with no body", source: "{@lt key=\"2\" value=\"3\" type=\"number\"/}", From 35580a7ce3cb5fa3c7e78a24a5b47ddf56c1ea7d Mon Sep 17 00:00:00 2001 From: Steven Foote Date: Wed, 19 Sep 2012 14:06:45 -0700 Subject: [PATCH 4/4] Add tests for other types for ne While adding tests, I discovered some problems with the filter and coerce functions. Fixes included here. --- lib/.dust-helpers.js.swp | Bin 0 -> 32768 bytes lib/dust-helpers.js | 9 +++-- test/jasmine-test/spec/helpersTests.js | 44 ++++++++++++++++++++++++- 3 files changed, 49 insertions(+), 4 deletions(-) create mode 100644 lib/.dust-helpers.js.swp diff --git a/lib/.dust-helpers.js.swp b/lib/.dust-helpers.js.swp new file mode 100644 index 0000000000000000000000000000000000000000..21406417cb609b476c0868879bb86e96fe327515 GI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EIShNdD;)&^|itUP$+&ai5r5EF&DprQ^@-f--$8L>%Ma$P~pcPVubWjH^7e3<7or6>fFUnQsMGji#RYI}MTtXm6s+rI_ z05d`B1bqN|VH@FvHxa~TcJ@h69k@jlGxg8=lNW7g9`suW+$c0b@#h%aAAl(S90RMd Zi~0+)XP_6~4tn3|@Y@Cw0g_h-{|D?-%})RT literal 0 HcmV?d00001 diff --git a/lib/dust-helpers.js b/lib/dust-helpers.js index 3c3c689..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); } @@ -328,7 +331,7 @@ var helpers = { **/ "ne": function(chunk, context, bodies, params) { if(params) { - params.filterOpType = "eq"; + params.filterOpType = "ne"; } return filter(chunk, context, bodies, params, function(expected, actual) { return actual !== expected; }); }, diff --git a/test/jasmine-test/spec/helpersTests.js b/test/jasmine-test/spec/helpersTests.js index 576d83e..d5585e7 100644 --- a/test/jasmine-test/spec/helpersTests.js +++ b/test/jasmine-test/spec/helpersTests.js @@ -201,7 +201,21 @@ 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\"/}", @@ -230,6 +244,34 @@ var helpersTests = [ 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", source: "{@lt key=\"2\" value=\"3\" type=\"number\"/}",