Skip to content

Commit

Permalink
Merge pull request LinkedInAttic#78 from jairodemorais/selectHelper
Browse files Browse the repository at this point in the history
Use the tap method in the select helper
  • Loading branch information
vybs committed Jul 4, 2012
2 parents 6b5b9b1 + 4441062 commit 5e34211
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 6 deletions.
18 changes: 12 additions & 6 deletions lib/dust-helpers.js
Expand Up @@ -18,7 +18,6 @@ function isSelect(context) {
function filter(chunk, context, bodies, params, filter) {
var params = params || {},
actual, expected;

if (params.key) {
actual = context.get(params.key);
} else if (isSelect(context)) {
Expand All @@ -29,8 +28,7 @@ function filter(chunk, context, bodies, params, filter) {
} else {
throw "No key specified for filter and no key found in context from select statement";
}

expected = params.value;
expected = helpers.tap(params.value, chunk, context);
if (filter(expected, coerce(actual, params.type, context))) {
if (isSelect(context)) {
context.current().isResolved = true;
Expand Down Expand Up @@ -106,13 +104,21 @@ var helpers = {
}
// no condition
else {
_console.log( "No expression given!" );
_console.log( "NO condition given in the if helper!" );
}
return chunk;
},

select: function(chunk, context, bodies, params) {
return chunk.render(bodies.block, context.push({ isSelect: true, isResolved: false, value: context.get(params.key) }));
if( params && params.key){
var key = params.key;
key = this.tap(key, chunk, context);
return chunk.render(bodies.block, context.push({ isSelect: true, isResolved: false, value: context.get(key) }));
}
// no key
else {
_console.log( "No key given for the select tag!" );
}
return chunk;
},

eq: function(chunk, context, bodies, params) {
Expand Down
88 changes: 88 additions & 0 deletions test/jasmine-test/spec/examples.js
Expand Up @@ -665,6 +665,94 @@ var dustExamples = [
expected: "",
message: "Select helper works correctly with no matching conditions"
},
{
name: "select helper: using tap - eq",
source: ['{@select key="y"}',
'{@eq value="{y}"}<div>FOO</div>{/eq}',
'{@eq value="{x}"}<div>BAR</div>{/eq}',
'{/select}'].join("\n"),
context: { y: 'foo', x: 'bar' },
expected: "<div>FOO</div>",
message: "Select helper works correctly using tap for eq"
},
{
name: "select helper: using tap - gte",
source: "{@select key=\"foo\"}{@gte value=\"{x}\"}foobar{/gte}{/select}",
context: {foo : 10 , x : 10},
expected: "foobar",
message: "Select helper works correctly using tap for gte"
},
{
name: "select helper: using tap - lte",
source: "{@select key=\"foo\"}{@lte value=\"{x}\"}foobar{/lte}{/select}",
context: {foo : 10 , x : 10},
expected: "foobar",
message: "Select helper works correctly using tap for lte"
},
{
name: "select helper: using tap - gt",
source: "{@select key=\"foo\"}{@gt value=\"{x}\"}foobar{/gt}{/select}",
context: {foo : 10 , x : 5},
expected: "foobar",
message: "Select helper works correctly using tap for gt"
},
{
name: "select helper: using tap - lt",
source: "{@select key=\"foo\"}{@lt value=\"{x}\"}foobar{/lt}{/select}",
context: {foo : 10 , x : 15},
expected: "foobar",
message: "Select helper works correctly using tap for lt"
},
{
name: "select helper: using tap in the key",
source: "{@select key=\"{x}\"}{@eq value=10}foobar{/eq}{/select}",
context: {foo : 10 , x : "foo"},
expected: "foobar",
message: "Select helper works correctly using tap in the key"
},
{
name: "select helper: using tap in the key with nested objects",
source: "{@select key=\"{x.key}\"}{@eq value=10}foobar{/eq}{/select}",
context: {foo : 10 , x : {key : "foo"}},
expected: "foobar",
message: "Select helper works correctly using tap in the key with nested objects"
},
{
name: "select helper: using tap in the key with nested objects",
source: "{@select key=\"{x.b.foo}\"}{@eq value=10}foobar{/eq}{/select}",
context: { a : 10 , x : {b : { "foo" : "a"}}},
expected: "foobar",
message: "Select helper works correctly using tap in the key with nested objects"
},
{
name: "select helper inside an object: usign tap in the key",
source: ["{#b}{@select key=\"y\"}{@eq value=\"{y}\"}<div>FOO</div>{/eq}",
"{@eq value=\"{x}\"}<div>BAR</div>{/eq}",
"{/select}{/b}"].join("\n"),
context: { b : { y: "foo", x: "bar" } },
expected: "<div>FOO</div>",
message: "Select helper works correctly inside an object using tap in the key"
},
{
name: "select helper inside an object: usign else tag",
source: ["{#b}{@select key=\"y\"}{@eq value=\"{y}\"}<div>FOO</div>{/eq}",
"{@eq value=\"{x}\"}<div>BAR</div>{/eq}",
"{@else value=\"foo\"}foofoo{/else}",
"{/select}{/b}"].join("\n"),
context: { b : { z: "foo", x: "bar" } },
expected: "foofoo",
message: "Select helper works correctly inside an object using else tag"
},
{
name: "select helper inside an object: usign else tag without value",
source: ["{#b}{@select key=\"y\"}{@eq value=\"{y}\"}<div>FOO</div>{/eq}",
"{@eq value=\"{x}\"}<div>BAR</div>{/eq}",
"{@else value=\"foo\"}foofoo{/else}",
"{/select}{/b}"].join("\n"),
context: { b : { z: "foo", x: "bar" } },
expected: "foofoo",
message: "Select helper works correctly inside an object using else tag without value"
},
{
name: "ws updated to allow eol",
source: ['{#authors ',
Expand Down

0 comments on commit 5e34211

Please sign in to comment.