Skip to content

Commit

Permalink
moved dustjs helpers from the original repo. Math helper and idx chan…
Browse files Browse the repository at this point in the history
…ge added.
  • Loading branch information
jairodemorais committed Aug 22, 2012
1 parent 5f59b59 commit 71a4fc9
Show file tree
Hide file tree
Showing 7 changed files with 339 additions and 46 deletions.
130 changes: 101 additions & 29 deletions dist/dust-helpers-1.0.0.js
Expand Up @@ -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(){
Expand All @@ -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);
Expand Down Expand Up @@ -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;
},

Expand All @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand Down
102 changes: 85 additions & 17 deletions lib/dust-helpers.js
Expand Up @@ -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;
},

Expand All @@ -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 = '';
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down
Binary file removed test/jasmine-test/client/lib/.DS_Store
Binary file not shown.
6 changes: 6 additions & 0 deletions test/jasmine-test/client/specRunner.html
Expand Up @@ -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;

Expand Down
7 changes: 7 additions & 0 deletions test/jasmine-test/server/specRunner.js
Expand Up @@ -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);
Expand Down

0 comments on commit 71a4fc9

Please sign in to comment.