diff --git a/archive/dustjs-helpers-1.5.0.tar.gz b/archive/dustjs-helpers-1.5.0.tar.gz new file mode 100644 index 0000000..2ee9f5e Binary files /dev/null and b/archive/dustjs-helpers-1.5.0.tar.gz differ diff --git a/archive/dustjs-helpers-1.5.0.zip b/archive/dustjs-helpers-1.5.0.zip new file mode 100644 index 0000000..aae0756 Binary files /dev/null and b/archive/dustjs-helpers-1.5.0.zip differ diff --git a/dist/dust-helpers.js b/dist/dust-helpers.js index c6d9e7e..aef2cfe 100644 --- a/dist/dust-helpers.js +++ b/dist/dust-helpers.js @@ -1,10 +1,21 @@ -/*! dustjs-helpers - v1.4.0 +/*! dustjs-helpers - v1.5.0 * https://github.com/linkedin/dustjs-helpers * Copyright (c) 2014 Aleksander Williams; Released under the MIT License */ (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 a future version of dustjs-helpers", "WARN"); + _log("For help and a deprecation timeline, see https://github.com/linkedin/dustjs-helpers/wiki/Deprecated-Features#" + target.replace(/\W+/g, ""), "WARN"); + _deprecatedCache[target] = true; +} function isSelect(context) { var value = context.current(); @@ -36,18 +47,17 @@ function filter(chunk, context, bodies, params, filterOp) { actualKey, expectedValue, filterOpType = params.filterOpType || ''; + // when @eq, @lt etc are used as standalone helpers, key is required and hence check for defined - if ( typeof params.key !== "undefined") { + if (params.hasOwnProperty("key")) { actualKey = dust.helpers.tap(params.key, chunk, context); - } - else if (isSelect(context)) { + } else if (isSelect(context)) { actualKey = context.current().selectKey; // supports only one of the blocks in the select to be selected if (context.current().isResolved) { filterOp = function() { return false; }; } - } - else { + } else { _log("No key specified for filter in:" + filterOpType + " helper "); return chunk; } @@ -60,27 +70,24 @@ function filter(chunk, context, bodies, params, filterOp) { // we want helpers without bodies to fail gracefully so check it first if(body) { return chunk.render(body, context); - } - else { - _log("No key specified for filter in:" + filterOpType + " helper "); + } else { + _log("No body specified for " + filterOpType + " helper "); return chunk; } - } - else if (bodies['else']) { + } else if (bodies['else']) { return chunk.render(bodies['else'], context); } return chunk; } -function coerce (value, type, context) { - if (value) { - switch (type || typeof(value)) { +function coerce(value, type, context) { + if (typeof value !== "undefined") { + 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); } @@ -143,22 +150,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; - } + // Will be removed in 1.6 + _deprecated("{@idx}"); + if(body) { + return body(chunk, context.push(context.stack.index)); + } + else { + return chunk; + } }, /** @@ -208,12 +216,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) { + // Will be removed in 1.6 + _deprecated("{@if}"); + + cond = dust.helpers.tap(params.cond, chunk, context); // eval expressions with given dust references if(eval(cond)){ if(body) { diff --git a/dist/dust-helpers.min.js b/dist/dust-helpers.min.js index 3bc4b70..8fd292a 100644 --- a/dist/dust-helpers.min.js +++ b/dist/dust-helpers.min.js @@ -1,4 +1,4 @@ -/*! dustjs-helpers - v1.4.0 +/*! dustjs-helpers - v1.5.0 * https://github.com/linkedin/dustjs-helpers * Copyright (c) 2014 Aleksander Williams; Released under the MIT License */ -!function(dust){function isSelect(a){var b=a.current();return"object"==typeof b&&b.isSelect===!0}function jsonFilter(a,b){return"function"==typeof b?b.toString().replace(/(^\s+|\s+$)/gm,"").replace(/\n/gm,"").replace(/,\s*/gm,", ").replace(/\)\{/gm,") {"):b}function filter(a,b,c,d,e){d=d||{};var f,g,h=c.block,i=d.filterOpType||"";if("undefined"!=typeof d.key)f=dust.helpers.tap(d.key,a,b);else{if(!isSelect(b))return _log("No key specified for filter in:"+i+" helper "),a;f=b.current().selectKey,b.current().isResolved&&(e=function(){return!1})}return g=dust.helpers.tap(d.value,a,b),e(coerce(g,d.type,b),coerce(f,d.type,b))?(isSelect(b)&&(b.current().isResolved=!0),h?a.render(h,b):(_log("No key specified for filter in:"+i+" helper "),a)):c["else"]?a.render(c["else"],b):a}function coerce(a,b,c){if(a)switch(b||typeof a){case"number":return+a;case"string":return String(a);case"boolean":return a="false"===a?!1:a,Boolean(a);case"date":return new Date(a);case"context":return c.get(a)}return a}var _log=dust.log?function(a){dust.log(a,"INFO")}:function(){},helpers={tap:function(a,b,c){if("function"!=typeof a)return a;var d,e="";return d=b.tap(function(a){return e+=a,""}).render(a,c),b.untap(),d.constructor!==b.constructor?d:""===e?!1:e},sep:function(a,b,c){var d=c.block;return b.stack.index===b.stack.of-1?a:d?c.block(a,b):a},idx:function(a,b,c){var d=c.block;return d?c.block(a,b.push(b.stack.index)):a},contextDump:function(a,b,c,d){var e,f=d||{},g=f.to||"output",h=f.key||"current";return g=dust.helpers.tap(g,a,b),h=dust.helpers.tap(h,a,b),e="full"===h?JSON.stringify(b.stack,jsonFilter,2):JSON.stringify(b.stack.head,jsonFilter,2),"console"===g?(_log(e),a):(e=e.replace(/b})):a},lte:function(a,b,c,d){return d?(d.filterOpType="lte",filter(a,b,c,d,function(a,b){return a>=b})):a},gt:function(a,b,c,d){return d?(d.filterOpType="gt",filter(a,b,c,d,function(a,b){return b>a})):a},gte:function(a,b,c,d){return d?(d.filterOpType="gte",filter(a,b,c,d,function(a,b){return b>=a})):a},"default":function(a,b,c,d){return d&&(d.filterOpType="default"),filter(a,b,c,d,function(){return!0})},size:function(a,b,c,d){var e,f,g,h=0;if(d=d||{},e=d.key,e&&e!==!0)if(dust.isArray(e))h=e.length;else if(!isNaN(parseFloat(e))&&isFinite(e))h=e;else if("object"==typeof e){f=0;for(g in e)Object.hasOwnProperty.call(e,g)&&f++;h=f}else h=(e+"").length;else h=0;return a.write(h)}};for(var key in helpers)dust.helpers[key]=helpers[key];"undefined"!=typeof exports&&(module.exports=dust)}("undefined"!=typeof exports?require("dustjs-linkedin"):dust); \ No newline at end of file +!function(dust){function _deprecated(a){_deprecatedCache[a]||(_log("Deprecation warning: "+a+" is deprecated and will be removed in a future version of dustjs-helpers","WARN"),_log("For help and a deprecation timeline, see https://github.com/linkedin/dustjs-helpers/wiki/Deprecated-Features#"+a.replace(/\W+/g,""),"WARN"),_deprecatedCache[a]=!0)}function isSelect(a){var b=a.current();return"object"==typeof b&&b.isSelect===!0}function jsonFilter(a,b){return"function"==typeof b?b.toString().replace(/(^\s+|\s+$)/gm,"").replace(/\n/gm,"").replace(/,\s*/gm,", ").replace(/\)\{/gm,") {"):b}function filter(a,b,c,d,e){d=d||{};var f,g,h=c.block,i=d.filterOpType||"";if(d.hasOwnProperty("key"))f=dust.helpers.tap(d.key,a,b);else{if(!isSelect(b))return _log("No key specified for filter in:"+i+" helper "),a;f=b.current().selectKey,b.current().isResolved&&(e=function(){return!1})}return g=dust.helpers.tap(d.value,a,b),e(coerce(g,d.type,b),coerce(f,d.type,b))?(isSelect(b)&&(b.current().isResolved=!0),h?a.render(h,b):(_log("No body specified for "+i+" helper "),a)):c["else"]?a.render(c["else"],b):a}function coerce(a,b,c){if("undefined"!=typeof a)switch(b||typeof a){case"number":return+a;case"string":return String(a);case"boolean":return a="false"===a?!1:a,Boolean(a);case"date":return new Date(a);case"context":return c.get(a)}return a}var _log=dust.log?function(a,b){b=b||"INFO",dust.log(a,b)}:function(){},_deprecatedCache={},helpers={tap:function(a,b,c){if("function"!=typeof a)return a;var d,e="";return d=b.tap(function(a){return e+=a,""}).render(a,c),b.untap(),d.constructor!==b.constructor?d:""===e?!1:e},sep:function(a,b,c){var d=c.block;return b.stack.index===b.stack.of-1?a:d?d(a,b):a},idx:function(a,b,c){var d=c.block;return _deprecated("{@idx}"),d?d(a,b.push(b.stack.index)):a},contextDump:function(a,b,c,d){var e,f=d||{},g=f.to||"output",h=f.key||"current";return g=dust.helpers.tap(g,a,b),h=dust.helpers.tap(h,a,b),e="full"===h?JSON.stringify(b.stack,jsonFilter,2):JSON.stringify(b.stack.head,jsonFilter,2),"console"===g?(_log(e),a):(e=e.replace(/b})):a},lte:function(a,b,c,d){return d?(d.filterOpType="lte",filter(a,b,c,d,function(a,b){return a>=b})):a},gt:function(a,b,c,d){return d?(d.filterOpType="gt",filter(a,b,c,d,function(a,b){return b>a})):a},gte:function(a,b,c,d){return d?(d.filterOpType="gte",filter(a,b,c,d,function(a,b){return b>=a})):a},"default":function(a,b,c,d){return d&&(d.filterOpType="default"),filter(a,b,c,d,function(){return!0})},size:function(a,b,c,d){var e,f,g,h=0;if(d=d||{},e=d.key,e&&e!==!0)if(dust.isArray(e))h=e.length;else if(!isNaN(parseFloat(e))&&isFinite(e))h=e;else if("object"==typeof e){f=0;for(g in e)Object.hasOwnProperty.call(e,g)&&f++;h=f}else h=(e+"").length;else h=0;return a.write(h)}};for(var key in helpers)dust.helpers[key]=helpers[key];"undefined"!=typeof exports&&(module.exports=dust)}("undefined"!=typeof exports?require("dustjs-linkedin"):dust); \ No newline at end of file diff --git a/package.json b/package.json index c076592..e141c84 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "dustjs-helpers", - "version": "1.4.0", + "version": "1.5.0", "author": { "name": "Aleksander Williams", "url": "http://akdubya.github.com/dustjs"