Skip to content

Commit

Permalink
Remove deprecated helper {@default} and ensure {@math} works with…
Browse files Browse the repository at this point in the history
… `{@any}` and `{@none}`.
  • Loading branch information
Seth Kinast committed Apr 18, 2015
1 parent b7d7b56 commit 9ab15a2
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 45 deletions.
55 changes: 21 additions & 34 deletions lib/dust-helpers.js
Expand Up @@ -44,7 +44,6 @@ function addSelectState(context, key) {
return newContext
.push({ "__select__": {
isResolved: false,
isDefaulted: false,
isDeferredComplete: false,
deferreds: [],
key: key
Expand All @@ -53,6 +52,16 @@ function addSelectState(context, key) {
.push(head, context.stack.index, context.stack.of);
}

function resolveSelectDeferreds(state) {
var x, len;
if(state.deferreds.length) {
state.isDeferredComplete = true;
for(x=0, len=state.deferreds.length; x<len; x++) {
state.deferreds[x]();
}
}
}

// Utility method : toString() equivalent for functions
function jsonFilter(key, value) {
if (typeof value === "function") {
Expand Down Expand Up @@ -100,9 +109,6 @@ function filter(chunk, context, bodies, params, filterOp) {
// coerce both the actualKey and expectedValue to the same type for equality and non-equality compares
if (filterOp(coerce(expectedValue, params.type, context), coerce(actualKey, params.type, context))) {
if (selectState) {
if(filterOpType === 'default') {
selectState.isDefaulted = true;
}
selectState.isResolved = true;
}
// Helpers without bodies are valid due to the use of {@any} blocks
Expand All @@ -118,8 +124,9 @@ function filter(chunk, context, bodies, params, filterOp) {
}

function coerce(value, type, context) {
if (typeof value !== "undefined") {
switch (type || typeof value) {
if (typeof value !== 'undefined' &&
typeof type !== 'undefined') {
switch (type) {
case 'number': return +value;
case 'string': return String(value);
case 'boolean':
Expand Down Expand Up @@ -273,7 +280,8 @@ var helpers = {
// operand can be null for "abs", ceil and floor
operand = params.operand,
round = params.round,
mathOut = null;
mathOut = null,
state, x, len;

key = parseFloat(dust.helpers.tap(key, chunk, context));
operand = parseFloat(dust.helpers.tap(operand, chunk, context));
Expand Down Expand Up @@ -327,7 +335,9 @@ var helpers = {
// with bodies act like the select helper with mathOut as the key
// like the select helper bodies['else'] is meaningless and is ignored
context = addSelectState(context, mathOut);
return chunk.render(bodies.block, context);
chunk = chunk.render(bodies.block, context);
resolveSelectDeferreds(getSelectState(context));
return chunk;
} else {
// self closing math helper will return the calculated output
return chunk.write(mathOut);
Expand All @@ -352,22 +362,15 @@ var helpers = {
**/
"select": function(chunk, context, bodies, params) {
var body = bodies.block,
state, key, len, x;
key;

if (params.hasOwnProperty("key")) {
key = dust.helpers.tap(params.key, chunk, context);
// bodies['else'] is meaningless and is ignored
if (body) {
context = addSelectState(context, key);
state = getSelectState(context);
chunk = chunk.render(body, context);
// Resolve any deferred blocks (currently just {@any} blocks)
if(state.deferreds.length) {
state.isDeferredComplete = true;
for(x=0, len=state.deferreds.length; x<len; x++) {
state.deferreds[x]();
}
}
resolveSelectDeferreds(getSelectState(context));
} else {
_log("select", "Missing body block", "WARN");
}
Expand Down Expand Up @@ -490,7 +493,7 @@ var helpers = {
} else {
chunk = chunk.map(function(chunk) {
selectState.deferreds.push(function() {
if(selectState.isResolved && !selectState.isDefaulted) {
if(selectState.isResolved) {
chunk = chunk.render(bodies.block, context);
}
chunk.end();
Expand Down Expand Up @@ -529,22 +532,6 @@ var helpers = {
return chunk;
},

/**
* {@default}
* Outputs if no truth test inside a {@select} has passed.
* Must be contained inside a {@select} block.
*/
"default": function(chunk, context, bodies, params) {
params.filterOpType = "default";
// Deprecated for removal in 1.7
_deprecated("default");
if(!isSelect(context)) {
_log("default", "Must be used inside a {@select} block", "ERROR");
return chunk;
}
return filter(chunk, context, bodies, params, function() { return true; });
},

/**
* size helper prints the size of the given key
* Note : size helper is self closing and does not support bodies
Expand Down
29 changes: 18 additions & 11 deletions test/jasmine-test/spec/helpersTests.js
Expand Up @@ -382,7 +382,7 @@
},
{
name: "math helper with body gt default",
source: '<div>{@math key="13" method="add" operand="12"}{@gt value=123}13 + 12 > 123{/gt}{@default}Math is fun{/default}{/math}</div>',
source: '<div>{@math key="13" method="add" operand="12"}{@gt value=123}13 + 12 > 123{/gt}{@none}Math is fun{/none}{/math}</div>',
context: {},
expected: "<div>Math is fun</div>",
message: "testing math with body else helper with add and gt and default"
Expand All @@ -395,12 +395,19 @@
message: "testing math with body ignores the else"
},
{
name: "math helper with body acts like the select helper",
name: "math helper with body acts like the select helper, ignoring else",
source: '<div>{@math key="1" method="subtract" operand="1"}math with body is truthy even if mathout is falsy{:else}else is meaningless{/math}</div>',
context: {},
expected: "<div>math with body is truthy even if mathout is falsy</div>",
message: "testing math with body ignores the else"
},
{
name: "math helper with body acts like the select helper and uses @any and @none",
source: '{@math key=1 method="subtract" operand=2}{@any}Positive!{/any}{@none}Negative!{/none}{@gte value=0/}{/math}',
context: {},
expected: 'Negative!',
message: 'math helper with any and none'
},
{
name: "math helper empty body",
source: '<div>{@math key="1" method="add" operand="2"}{/math}</div>',
Expand Down Expand Up @@ -897,7 +904,7 @@
"{@eq value=\"bar\"}foobar{/eq}",
"{@eq value=\"baz\"}foobaz{/eq}",
"{@eq value=\"foobar\"}foofoobar{/eq}",
"{@default value=\"foo\"}foofoo{/default}",
"{@none value=\"foo\"}foofoo{/none}",
"{/select}"
].join("\n"),
context: { "foo": "foo" },
Expand Down Expand Up @@ -944,7 +951,7 @@
source: ["{#b}{@select}",
" {@eq value=\"{z}\"}<div>FOO</div>{/eq}",
" {@eq value=\"{x}\"}<div>BAR</div>{/eq}",
" {@default}foofoo{/default}",
" {@none}foofoo{/none}",
"{/select}{/b}"].join("\n"),
context: { b : { z: "foo", x: "bar" } },
expected: "",
Expand All @@ -956,7 +963,7 @@
source: ["{#b}{@select key=y}",
" {@eq value=\"{z}\"}<div>FOO</div>{/eq}",
" {@eq value=\"{x}\"}<div>BAR</div>{/eq}",
" {@default}foofoo{/default}",
" {@none}foofoo{/none}",
"{/select}{/b}"].join("\n"),
context: { b : { z: "foo", x: "bar" } },
expected: "foofoo",
Expand All @@ -967,7 +974,7 @@
source: ["{#b}{@select key=\"{x}\"}",
" {@eq value=\"{y}\"}<div>BAR</div>{/eq}",
" {@eq value=\"{z}\"}<div>BAZ</div>{/eq}",
" {@default value=\"foo\"}foofoo{/default}",
" {@none value=\"foo\"}foofoo{/none}",
"{/select}{/b}"].join("\n"),
context: { b : { "x": "foo", "y": "bar", "z": "baz" } },
expected: "foofoo",
Expand All @@ -978,7 +985,7 @@
source: ["{#skills}{@select key=.}",
"{@eq value=\"java\"}JAVA,{/eq}",
"{@eq value=\"js\"}JS,{/eq}",
"{@default value=\"foo\"}UNKNOWN{/default}",
"{@none value=\"foo\"}UNKNOWN{/none}",
"{/select}{/skills}"].join("\n"),
context: { "skills" : [ "java", "js" , "unknown"] },
expected: "JAVA,JS,UNKNOWN",
Expand All @@ -989,7 +996,7 @@
source: ["{#skills}{@select key=\"{.}\"}",
"{@eq value=\"java\"}JAVA,{/eq}",
"{@eq value=\"js\"}JS,{/eq}",
"{@default value=\"foo\"}UNKNOWN{/default}",
"{@none value=\"foo\"}UNKNOWN{/none}",
"{/select}{/skills}"].join("\n"),
context: { "skills" : [ "java", "js" , "unknown"] },
expected: "JAVA,JS,UNKNOWN",
Expand All @@ -1015,7 +1022,7 @@
source: ['{#skills}{@select key="{.}"}',
'{@eq value="java"}JAVA {outside},{/eq}',
'{@eq value="js"}JS {outside},{/eq}',
'{@default value="foo"}UNKNOWN {outside}{/default}',
'{@none value="foo"}UNKNOWN {outside}{/none}',
'{/select}{/skills}'].join("\n"),
context: { "skills" : [ "java", "js" , "unknown"], "outside": 'foo' },
expected: "JAVA foo,JS foo,UNKNOWN foo",
Expand All @@ -1029,10 +1036,10 @@
' done {message} ',
' {outside}',
'{/eq} ',
'{@default}',
'{@none}',
' default {message} ',
' {outside}',
'{/default}',
'{/none}',
'{/select}',
'{/data.messages}'].join("\n"),
context: {
Expand Down

0 comments on commit 9ab15a2

Please sign in to comment.