Permalink
Browse files

Add doc to the tap method on value resolution and clean up the place…

…s that unnecessarily used isdefined checks
  • Loading branch information...
vybs committed Sep 22, 2012
1 parent cdbe69b commit 42a381bd186c89022a0cc319a2d970c6312cc01b
Showing with 239 additions and 33 deletions.
  1. +48 −32 lib/dust-helpers.js
  2. +191 −1 test/jasmine-test/spec/helpersTests.js
View
@@ -36,7 +36,7 @@ function filter(chunk, context, bodies, params, filterOp) {
actualKey,
expectedValue,
filterOpType = params.filterOpType || '';
- // when @eq, @lt etc are used as standalone helpers, key is defined
+ // when @eq, @lt etc are used as standalone helpers, key is required and hence check for defined
if ( typeof params.key !== "undefined") {
actualKey = dust.helpers.tap(params.key, chunk, context);
}
@@ -92,13 +92,27 @@ function coerce (value, type, context) {
var helpers = {
// Utility helping to resolve dust references in the given chunk
- // returns "undefined" when unable to resolve a value, since it relies on Chunk.reference method
+ // uses the Chunk.render method to resolve value
+ /*
+ Reference resolution rules:
+ if value exists in JSON:
+ "" or '' will evaluate to false, boolean false, null, or undefined will evaluate to false,
+ numeric 0 evaluates to true, so does, string "0", string "null", string "undefined" and string "false".
+ Also note that empty array -> [] is evaluated to false and empty object -> {} and non-empty object are evaluated to true
+
+ if value does not exist in JSON and the input is a single reference: {x}
+ dust render emits empty string, and we then return false
+
+ if values does not exist in JSON and the input is a chain of references : {x} < {y}
+ dust render emits < and we return the partial output
+
+ */
"tap": function( input, chunk, context ){
// return given input if there is no dust reference to resolve
var output = input;
- // dust compiles a string to function, if there are references
+ // dust compiles a string/reference such as {foo} to function,
if( typeof input === "function"){
- // just a plain function/ lamda in the context, not a dust `body` function created by the dust compiler
+ // just a plain function (a.k.a anonymous functions) in the context, not a dust `body` function created by the dust compiler
if( input.isFunction === true ){
output = input();
} else {
@@ -216,26 +230,22 @@ var helpers = {
* @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 method params before continuing
+ //key and method are required for further processing
if( params && typeof params.key !== "undefined" && params.method ){
var key = params.key,
method = params.method,
- operand = params.operand || null,
+ // operand can be null for "abs", ceil and floor
+ operand = params.operand,
mathOut = null,
operError = function(){_console.log("operand is required for this math method"); return null;};
key = dust.helpers.tap(key, chunk, context);
operand = dust.helpers.tap(operand, chunk, context);
- // cannot resolve/find the operand in the context from the tap method
- if( typeof operand === 'undefined' )
- {
- operError();
- } else {
// TODO: handle and tests for negatives and floats in all math operations
switch(method) {
case "mod":
- if(operand === 0) {
- _console.log("operand for mod operation is 0: expect NaN!");
- }
+ if(operand === 0 || operand === -0) {
+ _console.log("operand for divide operation is 0/-0: expect Nan!");
+ }
mathOut = parseFloat(key) % parseFloat(operand);
break;
case "add":
@@ -248,8 +258,8 @@ var helpers = {
mathOut = parseFloat(key) * parseFloat(operand);
break;
case "divide":
- if(operand === 0) {
- _console.log("operand for divide operation is 0: expect Nan!");
+ if(operand === 0 || operand === -0) {
+ _console.log("operand for divide operation is 0/-0: expect Nan/Infinity!");
}
mathOut = parseFloat(key) / parseFloat(operand);
break;
@@ -264,7 +274,6 @@ var helpers = {
break;
default:
_console.log( "method passed is not supported" );
- }
}
if (mathOut !== null){
@@ -296,6 +305,7 @@ var helpers = {
**/
"select": function(chunk, context, bodies, params) {
var body = bodies.block;
+ // key is required for processing, hence check for defined
if( params && typeof params.key !== "undefined"){
// returns given input as output, if the input is not a dust reference, else does a context lookup
var key = dust.helpers.tap(params.key, chunk, context);
@@ -347,8 +357,9 @@ var helpers = {
"ne": function(chunk, context, bodies, params) {
if(params) {
params.filterOpType = "ne";
+ return filter(chunk, context, bodies, params, function(expected, actual) { return actual !== expected; });
}
- return filter(chunk, context, bodies, params, function(expected, actual) { return actual !== expected; });
+ return chunk;
},
/**
@@ -364,9 +375,9 @@ var helpers = {
**/
"lt": function(chunk, context, bodies, params) {
if(params) {
- params.filterOpType = "lt";
- }
- return filter(chunk, context, bodies, params, function(expected, actual) { return actual < expected; });
+ params.filterOpType = "lt";
+ return filter(chunk, context, bodies, params, function(expected, actual) { return actual < expected; });
+ }
},
/**
@@ -382,9 +393,10 @@ var helpers = {
**/
"lte": function(chunk, context, bodies, params) {
if(params) {
- params.filterOpType = "lte";
- }
- return filter(chunk, context, bodies, params, function(expected, actual) { return actual <= expected; });
+ params.filterOpType = "lte";
+ return filter(chunk, context, bodies, params, function(expected, actual) { return actual <= expected; });
+ }
+ return chunk;
},
@@ -400,10 +412,12 @@ var helpers = {
Note : use type="number" when comparing numeric
**/
"gt": function(chunk, context, bodies, params) {
- if(params) {
- params.filterOpType = "gt";
- }
- return filter(chunk, context, bodies, params, function(expected, actual) { return actual > expected; });
+ // if no params do no go further
+ if(params) {
+ params.filterOpType = "gt";
+ return filter(chunk, context, bodies, params, function(expected, actual) { return actual > expected; });
+ }
+ return chunk;
},
/**
@@ -419,18 +433,20 @@ var helpers = {
**/
"gte": function(chunk, context, bodies, params) {
if(params) {
- params.filterOpType = "gte";
- }
- return filter(chunk, context, bodies, params, function(expected, actual) { return actual >= expected; });
+ params.filterOpType = "gte";
+ return filter(chunk, context, bodies, params, function(expected, actual) { return actual >= expected; });
+ }
+ return chunk;
},
// to be used in conjunction with the select helper
// TODO: fix the helper to do nothing when used standalone
"default": function(chunk, context, bodies, params) {
+ // does not require any params
if(params) {
params.filterOpType = "default";
}
- return filter(chunk, context, bodies, params, function(expected, actual) { return true; });
+ return filter(chunk, context, bodies, params, function(expected, actual) { return true; });
},
/**
Oops, something went wrong.

0 comments on commit 42a381b

Please sign in to comment.