Permalink
Browse files

moved dustjs helpers from the original repo. Math helper and idx chan…

…ge added.
  • Loading branch information...
jairodemorais committed Aug 22, 2012
1 parent 5f59b59 commit 71a4fc989004e3f7d2377bc5edd787eccac22b44
View
@@ -14,7 +14,7 @@ if (typeof exports !== "undefined")
/* make a safe version of console if it is not available
* currently supporting:
- * _console.log
+ * _console.log
* */
var _console = (typeof console !== 'undefined')? console: {
log: function(){
@@ -24,12 +24,12 @@ var _console = (typeof console !== 'undefined')? console: {
function isSelect(context) {
var value = context.current();
- return typeof value === "object" && value.isSelect === true;
+ return typeof value === "object" && value.isSelect === true;
}
function filter(chunk, context, bodies, params, filter) {
var params = params || {},
- actual,
+ actual,
expected;
if (params.key) {
actual = helpers.tap(params.key, chunk, context);
@@ -82,7 +82,7 @@ var helpers = {
},
contextDump: function(chunk, context, bodies) {
- _console.log(JSON.stringify(context.stack));
+ _console.log(JSON.stringify(context.stack.head));
return chunk;
},
@@ -92,20 +92,24 @@ var helpers = {
var output = input;
// dust compiles a string to function, if there are references
if( typeof input === "function"){
- output = '';
- chunk.tap(function(data){
- output += data;
- return '';
- }).render(input, context).untap();
- if( output === '' ){
- output = false;
+ if( ( typeof input.isReference !== "undefined" ) && ( input.isReference === true ) ){ // just a plain function, not a dust `body` function
+ output = input();
+ } else {
+ output = '';
+ chunk.tap(function(data){
+ output += data;
+ return '';
+ }).render(input, context).untap();
+ if( output === '' ){
+ output = false;
+ }
}
- }
+ }
return output;
},
/**
- if helper
+ if helper
@param cond, either a string literal value or a dust reference
a string literal value, is enclosed in double quotes, e.g. cond="2>3"
a dust reference is also enclosed in double quotes, e.g. cond="'{val}'' > 3"
@@ -131,8 +135,72 @@ var helpers = {
return chunk;
},
+ /**
+ * 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 for
+ * operations like mod, add, subtract, etc.
+ */
+ "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;
+ eq = this.tap(eq, chunk, context);
+ 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( 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 key parameter and no method or eq passed
+ else {
+ _console.log( "Key is a required parameter along with eq or method/operand!" );
+ }
+ return chunk;
+ },
/**
- select/eq/lt/lte/gt/gte/default helper
+ select/eq/lt/lte/gt/gte/default helper
@param key, either a string literal value or a dust reference
a string literal value, is enclosed in double quotes, e.g. key="foo"
a dust reference may or may not be enclosed in double quotes, e.g. key="{val}" and key=val are both valid
@@ -174,28 +242,32 @@ var helpers = {
"default": function(chunk, context, bodies, params) {
return filter(chunk, context, bodies, params, function(expected, actual) { return true; });
},
-
"size": function( chunk, context, bodies, params ) {
- var subject = params.subject;
+ var subject = params.subject;
var value = 0;
if (!subject) { //undefined, "", 0
- value = 0;
- } else if(dust.isArray(subject)) { //array
- value = subject.length;
- } else if (!isNaN(subject)) { //numeric values
- value = subject;
- } else if (Object(subject) === subject) { //object test
- var nr = 0;
- for(var k in subject) if(Object.hasOwnProperty.call(subject,k)) nr++;
- value = nr;
- } else {
- value = (subject + '').length; //any other value (strings etc.)
- }
- return chunk.write(value);
+ value = 0;
+ }
+ else if(dust.isArray(subject)) { //array
+ value = subject.length;
+ }
+ else if (!isNaN(subject)) { //numeric values
+ value = subject;
+ }
+ else if (Object(subject) === subject) { //object test
+ var nr = 0;
+ for(var k in subject)
+ if(Object.hasOwnProperty.call(subject,k)) nr++;
+ value = nr;
+ } else {
+ value = (subject + '').length; //any other value (strings etc.)
+ }
+ return chunk.write(value);
}
};
dust.helpers = helpers;
+
if (typeof exports !== "undefined")
{
module.exports = dust;
View
@@ -75,7 +75,7 @@ var helpers = {
},
contextDump: function(chunk, context, bodies) {
- _console.log(JSON.stringify(context.stack));
+ _console.log(JSON.stringify(context.stack.head));
return chunk;
},
@@ -85,7 +85,7 @@ var helpers = {
var output = input;
// dust compiles a string to function, if there are references
if( typeof input === "function"){
- if( input.length == 0 ) { // just a plain function, not a dust `body` function
+ if( ( typeof input.isReference !== "undefined" ) && ( input.isReference === true ) ){ // just a plain function, not a dust `body` function
output = input();
} else {
output = '';
@@ -128,6 +128,70 @@ var helpers = {
return chunk;
},
+ /**
+ * 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 for
+ * operations like mod, add, subtract, etc.
+ */
+ "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;
+ eq = this.tap(eq, chunk, context);
+ 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( 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 key parameter and no method or eq passed
+ else {
+ _console.log( "Key is a required parameter along with eq or method/operand!" );
+ }
+ return chunk;
+ },
/**
select/eq/lt/lte/gt/gte/default helper
@param key, either a string literal value or a dust reference
@@ -171,28 +235,32 @@ var helpers = {
"default": function(chunk, context, bodies, params) {
return filter(chunk, context, bodies, params, function(expected, actual) { return true; });
},
-
"size": function( chunk, context, bodies, params ) {
- var subject = params.subject;
+ var subject = params.subject;
var value = 0;
if (!subject) { //undefined, "", 0
- value = 0;
- } else if(dust.isArray(subject)) { //array
- value = subject.length;
- } else if (!isNaN(subject)) { //numeric values
- value = subject;
- } else if (Object(subject) === subject) { //object test
- var nr = 0;
- for(var k in subject) if(Object.hasOwnProperty.call(subject,k)) nr++;
- value = nr;
- } else {
- value = (subject + '').length; //any other value (strings etc.)
- }
- return chunk.write(value);
+ value = 0;
+ }
+ else if(dust.isArray(subject)) { //array
+ value = subject.length;
+ }
+ else if (!isNaN(subject)) { //numeric values
+ value = subject;
+ }
+ else if (Object(subject) === subject) { //object test
+ var nr = 0;
+ for(var k in subject)
+ if(Object.hasOwnProperty.call(subject,k)) nr++;
+ value = nr;
+ } else {
+ value = (subject + '').length; //any other value (strings etc.)
+ }
+ return chunk.write(value);
}
};
dust.helpers = helpers;
+
if (typeof exports !== "undefined")
{
module.exports = dust;
Binary file not shown.
@@ -20,6 +20,12 @@
<script type="text/javascript">
(function() {
+ //Add the tapper helper to test the Tap helper.
+ dust.helpers.tapper = function(chunk, context, bodies, params) {
+ var result = dust.helpers.tap(params.value,chunk,context);
+ chunk.write(result);
+ return chunk;
+ };
var jasmineEnv = jasmine.getEnv();
jasmineEnv.updateInterval = 1000;
@@ -7,6 +7,13 @@ dust = require('dustjs-linkedin'),
dust.helpers = require("../../../lib/dust-helpers").helpers,
helpersTests = require('../spec/helpersTests');
+//Add the tapper helper to test the Tap helper.
+dust.helpers.tapper = function(chunk, context, bodies, params) {
+ var result = dust.helpers.tap(params.value,chunk,context);
+ chunk.write(result);
+ return chunk;
+};
+
//Add the tapper helper to test the Tap helper.
dust.helpers.tapper = function(chunk, context, bodies, params) {
var result = dust.helpers.tap(params.value,chunk,context);
Oops, something went wrong.

0 comments on commit 71a4fc9

Please sign in to comment.