Skip to content

Commit

Permalink
Merge pull request #117 from sethkinast/math-float
Browse files Browse the repository at this point in the history
Handle string 0 in math helper and add toint to the params for @math.
  • Loading branch information
prashn64 committed Mar 7, 2015
2 parents 6a6474a + 7d8501c commit 75da720
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions lib/dust-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,42 +270,45 @@ var helpers = {
return null;
};

key = dust.helpers.tap(key, chunk, context);
operand = dust.helpers.tap(operand, chunk, context);
key = parseFloat(dust.helpers.tap(key, chunk, context));
operand = parseFloat(dust.helpers.tap(operand, chunk, context));
// TODO: handle and tests for negatives and floats in all math operations
switch(method) {
case "mod":
if(operand === 0 || operand === -0) {
_log("operand for divide operation is 0/-0: expect Nan!");
}
mathOut = parseFloat(key) % parseFloat(operand);
mathOut = key % operand;
break;
case "add":
mathOut = parseFloat(key) + parseFloat(operand);
mathOut = key + operand;
break;
case "subtract":
mathOut = parseFloat(key) - parseFloat(operand);
mathOut = key - operand;
break;
case "multiply":
mathOut = parseFloat(key) * parseFloat(operand);
mathOut = key * operand;
break;
case "divide":
if(operand === 0 || operand === -0) {
_log("operand for divide operation is 0/-0: expect Nan/Infinity!");
}
mathOut = parseFloat(key) / parseFloat(operand);
if(operand === 0 || operand === -0) {
_log("operand for divide operation is 0/-0: expect Nan/Infinity!");
}
mathOut = key / operand;
break;
case "ceil":
mathOut = Math.ceil(parseFloat(key));
mathOut = Math.ceil(key);
break;
case "floor":
mathOut = Math.floor(parseFloat(key));
mathOut = Math.floor(key);
break;
case "round":
mathOut = Math.round(parseFloat(key));
mathOut = Math.round(key);
break;
case "abs":
mathOut = Math.abs(parseFloat(key));
mathOut = Math.abs(key);
break;
case "toint":
mathOut = parseInt(key, 10);
break;
default:
_log("method passed is not supported");
Expand Down

0 comments on commit 75da720

Please sign in to comment.