diff --git a/dist/dust-helpers-1.0.0.js b/dist/dust-helpers-1.0.0.js index c170b62..913b45e 100644 --- a/dist/dust-helpers-1.0.0.js +++ b/dist/dust-helpers-1.0.0.js @@ -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; diff --git a/lib/dust-helpers.js b/lib/dust-helpers.js index d36d544..1b7bbb9 100644 --- a/lib/dust-helpers.js +++ b/lib/dust-helpers.js @@ -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; diff --git a/test/jasmine-test/client/lib/.DS_Store b/test/jasmine-test/client/lib/.DS_Store deleted file mode 100644 index 58e8be0..0000000 Binary files a/test/jasmine-test/client/lib/.DS_Store and /dev/null differ diff --git a/test/jasmine-test/client/specRunner.html b/test/jasmine-test/client/specRunner.html index 98b5ea4..ca0e3ce 100644 --- a/test/jasmine-test/client/specRunner.html +++ b/test/jasmine-test/client/specRunner.html @@ -20,6 +20,12 @@