Skip to content

Commit

Permalink
making math helper specific and extensible per the discussion, adding…
Browse files Browse the repository at this point in the history
… new unit tests
  • Loading branch information
sclatter committed Aug 13, 2012
1 parent fa93709 commit 479c3ee
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 24 deletions.
69 changes: 56 additions & 13 deletions dustjs-helpers/lib/dust-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,22 +120,65 @@ var helpers = {
},

/**
* modulus helper
* @param dividend is the value to divide
* @param divisor is the how many to divide
* dividend by
* math helper
* @param key is the value to perform math against
* @param eq is the value to test for equality with key
* @param method is the math method we will employ
* in the absence of an equality test
* @param operand is the second value needed fof
* operations like mod, add, subtract, etc.
*/
"mod": function ( chunk, context, bodies, params ) {
if( params && params.dividend && params.divisor ){
var dividend = params.dividend;
var divisor = params.divisor;
dividend = this.tap(dividend, chunk, context);
divisor = this.tap(divisor, chunk, context);
return chunk.write( eval( dividend % divisor ) );
"math": function ( chunk, context, bodies, params ) {
//make sure we have key and eq or method params before continuing
if( params && params.key && (params.eq || params.method) ){
var key = params.key;
key = this.tap(key, chunk, context);
if (params.eq) {
var eq = params.eq;
return chunk.write(key === eq);
}
//we are going to operate with math methods if not equals
else {
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( eval( 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" );
}
}
}
// no dividend or divisor
// no key parameter and no method or eq passed
else {
_console.log( "No dividend or divisor passed to mod helper!" );
_console.log( "Key is a required parameter along with eq or method/operand!" );
}
return chunk;
},
Expand Down
78 changes: 67 additions & 11 deletions dustjs-helpers/test/jasmine-test/spec/helpersTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,25 +42,81 @@ var helpersTests = [
message: "should test the if helper using $idx"
},
{
name: "mod helper numbers",
source: '<div>{@mod dividend="16" divisor="4"/}</div>',
name: "math helper mod numbers",
source: '<div>{@math key="16" method="mod" operand="4"/}</div>',
context: {},
expected: "<div>0</div>",
message: "testing mod helper with two numbers"
message: "testing math/mod helper with two numbers"
},
{
name: "mod helper with idx",
source: '{#list}<div>{@mod dividend="{$idx}" divisor="4"/}</div>{/list}',
context: { list: [ { x: 'foo' }] },
name: "math helper mod using $idx",
source: '{#list}<div>{@math key="{$idx}" method="mod" operand="5"/}</div>{/list}',
context: { list: [ { y: 'foo' } ]},
expected: "<div>0</div>",
message: "testing mod helper with idx"
message: "should test the math/mod helper using $idx"
},
{
name: "mod helper with string",
source: '<div>{@mod dividend="16" divisor="howdy"/}</div>',
name: "math helper subtract numbers",
source: '<div>{@math key="16" method="subtract" operand="4"/}</div>',
context: {},
expected: "<div>12</div>",
message: "testing math/subtract helper with two numbers"
},
{
name: "math helper subtract number and string",
source: '<div>{@math key="16" method="subtract" operand="doh"/}</div>',
context: {},
expected: "<div>NaN</div>",
message: "testing mmod helper with one string and one number"
message: "testing math/subtract helper with a number and a string"
},
{
name: "math helper add numbers",
source: '<div>{@math key="5" method="add" operand="4"/}</div>',
context: {},
expected: "<div>9</div>",
message: "testing math/add helper with two numbers"
},
{
name: "math helper multiply numbers",
source: '<div>{@math key="5" method="multiply" operand="4"/}</div>',
context: {},
expected: "<div>20</div>",
message: "testing math/multiply helper with two numbers"
},
{
name: "math helper divide using variable",
source: '<div>{@math key="16" method="divide" operand="{y}"/}</div>',
context: { y : 4 },
expected: "<div>4</div>",
message: "testing math/divide helper with variable as operand"
},
{
name: "math helper floor numbers",
source: '<div>{@math key="16.5" method="floor"/}</div>',
context: {},
expected: "<div>16</div>",
message: "testing math/floor helper with two numbers"
},
{
name: "math helper ceil numbers",
source: '<div>{@math key="16.5" method="ceil"/}</div>',
context: {},
expected: "<div>17</div>",
message: "testing math/ceil helper with two numbers"
},
{
name: "math helper abs numbers",
source: '<div>{@math key="-16" method="abs"/}</div>',
context: {},
expected: "<div>16</div>",
message: "testing math/abs helper with two numbers"
},
{
name: "math helper eq numbers",
source: '<div>{@math key="15" eq="16"/}</div>',
context: {},
expected: "<div>false</div>",
message: "testing math/eq helper with two numbers"
},
{
name: "select helper with a constant string and condition eq",
Expand Down Expand Up @@ -307,4 +363,4 @@ if (typeof module !== "undefined" && typeof require !== "undefined") {
module.exports = helpersTests; // We're on node.js
} else {
window.helpersTests = helpersTests; // We're on the browser
}
}

0 comments on commit 479c3ee

Please sign in to comment.