Skip to content

Commit

Permalink
semi colon. backwards compatible math helper setup for select style
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmyhchan committed Sep 10, 2012
1 parent 1f54ca0 commit 85b7fff
Showing 1 changed file with 47 additions and 36 deletions.
83 changes: 47 additions & 36 deletions lib/dust-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function jsonFilter(key, value) {
function filter(chunk, context, bodies, params, filterCheck) {
var actual,
expected;
params = params || {},
params = params || {};
if (params.key) {
actual = helpers.tap(params.key, chunk, context);
} else if (isSelect(context)) {
Expand Down Expand Up @@ -173,43 +173,54 @@ var helpers = {
"math": function ( chunk, context, bodies, params ) {
//make sure we have key and method params before continuing
if( params && typeof params.key !== "undefined" && params.method ){
var key = params.key;
var key = params.key,
method = params.method,
operand = params.operand || null,
mathOut = null,
operError = function(){_console.log("operand is required for this math method"); return null;};
key = this.tap(key, chunk, context);
var method = params.method;
var operand = params.operand || null;
var operError = function(){_console.log("operand is required for this math method")};
var returnExpression = function(exp){chunk.write( exp )};
if (operand) {
operand = this.tap(operand, chunk, context);
}
switch(method) {
case "mod":
(operand) ? returnExpression( parseFloat(key) % parseFloat(operand) ) : operError();
break;
case "add":
(operand) ? returnExpression( parseFloat(key) + parseFloat(operand) ) : operError();
break;
case "subtract":
(operand) ? returnExpression( parseFloat(key) - parseFloat(operand) ) : operError();
break;
case "multiply":
(operand) ? returnExpression( parseFloat(key) * parseFloat(operand) ) : operError();
break;
case "divide":
(operand) ? returnExpression( parseFloat(key) / parseFloat(operand) ) : operError();
break;
case "ceil":
returnExpression( Math.ceil(parseFloat(key)) );
break;
case "floor":
returnExpression( Math.floor(parseFloat(key)) );
break;
case "abs":
returnExpression( Math.abs(parseFloat(key)) );
break;
default:
_console.log( "method passed is not supported" );
if (operand) {
operand = this.tap(operand, chunk, context);
}
switch(method) {
case "mod":
mathOut = (operand) ? ( parseFloat(key) % parseFloat(operand) ) : operError();
break;
case "add":
mathOut = (operand) ? ( parseFloat(key) + parseFloat(operand) ) : operError();
break;
case "subtract":
mathOut = (operand) ? ( parseFloat(key) - parseFloat(operand) ) : operError();
break;
case "multiply":
mathOut = (operand) ? ( parseFloat(key) * parseFloat(operand) ) : operError();
break;
case "divide":
mathOut = (operand) ? ( parseFloat(key) / parseFloat(operand) ) : operError();
break;
case "ceil":
mathOut = Math.ceil(parseFloat(key));
break;
case "floor":
mathOut = Math.floor(parseFloat(key));
break;
case "abs":
mathOut = Math.abs(parseFloat(key));
break;
default:
_console.log( "method passed is not supported" );
}

if (bodies && bodies.block) {
// with bodies act like the select helper
if (mathOut){

}
return chunk;
} else if (mathOut !== null) {
// self closing math helper will return the calculated output
return chunk.write(mathOut);
}
}
// no key parameter and no method
else {
Expand Down

0 comments on commit 85b7fff

Please sign in to comment.