diff --git a/lib/dust-helpers.js b/lib/dust-helpers.js index 6f99aad..05aafcc 100644 --- a/lib/dust-helpers.js +++ b/lib/dust-helpers.js @@ -1,7 +1,17 @@ (function(dust){ -//using the built in logging method of dust when accessible -var _log = dust.log ? function(mssg) { dust.log(mssg, "INFO"); } : function() {}; +// Use dust's built-in logging when available +var _log = dust.log ? function(msg, level) { + level = level || "INFO"; + dust.log(msg, level); +} : function() {}; + +var _deprecatedCache = {}; +function _deprecated(target) { + if(_deprecatedCache[target]) { return; } + _log("Deprecation warning: " + target + " is deprecated and will be removed in the next minor version of dustjs-helpers", "WARN"); + _deprecatedCache[target] = true; +} function isSelect(context) { var value = context.current(); @@ -74,10 +84,9 @@ function coerce (value, type, context) { switch (type || typeof(value)) { case 'number': return +value; case 'string': return String(value); - case 'boolean': { + case 'boolean': value = (value === 'false' ? false : value); return Boolean(value); - } case 'date': return new Date(value); case 'context': return context.get(value); } @@ -140,22 +149,23 @@ var helpers = { if (context.stack.index === context.stack.of - 1) { return chunk; } - if(body) { - return bodies.block(chunk, context); - } - else { - return chunk; + if (body) { + return body(chunk, context); + } else { + return chunk; } }, "idx": function(chunk, context, bodies) { var body = bodies.block; - if(body) { - return bodies.block(chunk, context.push(context.stack.index)); - } - else { - return chunk; - } + if(body) { + // Deprecated in 1.4. Will be removed in 1.5 + _deprecated("{@idx}"); + return body(chunk, context.push(context.stack.index)); + } + else { + return chunk; + } }, /** @@ -205,12 +215,16 @@ var helpers = { cond argument should evaluate to a valid javascript expression **/ - "if": function( chunk, context, bodies, params ){ + "if": function( chunk, context, bodies, params ) { var body = bodies.block, - skip = bodies['else']; - if( params && params.cond){ - var cond = params.cond; - cond = dust.helpers.tap(cond, chunk, context); + skip = bodies['else'], + cond; + + if(params && params.cond) { + // Deprecated in 1.4. Will be removed in 1.5 + _deprecated("{@if}"); + + cond = dust.helpers.tap(params.cond, chunk, context); // eval expressions with given dust references if(eval(cond)){ if(body) {