Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add @ne helper as the complement to the @eq helper #14

Merged
merged 4 commits into from
Sep 19, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added lib/.dust-helpers.js.swp
Binary file not shown.
25 changes: 23 additions & 2 deletions lib/dust-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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);
}
Expand Down Expand Up @@ -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
Expand Down
70 changes: 70 additions & 0 deletions test/jasmine-test/spec/helpersTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is a type parameter ( would suggest adding foe type="number" and few others i dont remember on the top of my head.. test cases for completion )

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good.
On Sep 19, 2012 11:52 AM, "Veena Basavaraj" [email protected]
wrote:

In test/jasmine-test/spec/helpersTests.js:

  • },
  • {
  • 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"
  • },
  • {

there is a type parameter ( would suggest adding foe type="number" and few
others i dont remember on the top of my head.. test cases for completion )


Reply to this email directly or view it on GitHubhttps://github.com//pull/14/files#r1643419.

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",
Expand Down