Created
June 13, 2014 18:43
-
-
Save framp/a9cef5f74679a70c9dc8 to your computer and use it in GitHub Desktop.
Fixed some issues with the module common-dustjs-helpers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function (dust) { | |
var _eval_dust_string = function(str, chunk, context) { | |
var buf; | |
if (typeof str === "function") { | |
if (str.length === 0) { | |
str = str(); | |
} else { | |
buf = ''; | |
(chunk.tap(function(data) { | |
buf += data; | |
return ''; | |
})).render(str, context).untap(); | |
str = buf; | |
} | |
} | |
return str; | |
}; | |
var _render_if_else = function(b, chunk, context, bodies, params) { | |
if (b === true) { | |
if (bodies.block != null) { | |
chunk = chunk.render(bodies.block, context); | |
} | |
} else { | |
if (bodies["else"] != null) { | |
chunk = chunk.render(bodies["else"], context); | |
} | |
} | |
return chunk; | |
}; | |
var _inner_if_helper = function(chunk, context, bodies, params) { | |
var above, below, c, countof, execute_body, isntval, isval, matches, re, value, _i, _len, _ref; | |
execute_body = false; | |
if (params != null) { | |
if (params.test != null) { | |
value = this._eval_dust_string(params.test, chunk, context); | |
} | |
_ref = ['count', 'count_of', 'count-of', 'countof']; | |
for (_i = 0, _len = _ref.length; _i < _len; _i++) { | |
c = _ref[_i]; | |
if (params[c] != null) { | |
countof = this._eval_dust_string(params[c], chunk, context); | |
if ((countof != null ? countof.length : void 0) != null) { | |
value = countof.length; | |
} | |
} | |
} | |
if (value == null) { | |
value = this._eval_dust_string(params.value, chunk, context); | |
} | |
if (value != null) { | |
if (("" + value) === ("" + (parseFloat(value)))) { | |
value = parseFloat(value); | |
} | |
if (params.matches != null) { | |
matches = this._eval_dust_string(params.matches, chunk, context); | |
re = new RegExp(matches); | |
execute_body = re.test(value); | |
} else if (params['is'] != null) { | |
isval = this._eval_dust_string(params['is'], chunk, context); | |
if (typeof value === 'number' && (!isNaN(parseFloat(isval)))) { | |
isval = parseFloat(isval); | |
} | |
execute_body = value === isval; | |
} else if (params['isnt'] != null) { | |
isntval = this._eval_dust_string(params['isnt'], chunk, context); | |
if (typeof value === 'number' && (!isNaN(parseFloat(isntval)))) { | |
isntval = parseFloat(isntval); | |
} | |
execute_body = value !== isntval; | |
} else if (params.above != null) { | |
above = this._eval_dust_string(params.above, chunk, context); | |
if (typeof value === 'number' && (!isNaN(parseFloat(above)))) { | |
above = parseFloat(above); | |
} | |
execute_body = value > above; | |
} else if (params.below != null) { | |
below = this._eval_dust_string(params.below, chunk, context); | |
if (typeof value === 'number' && (!isNaN(parseFloat(below)))) { | |
below = parseFloat(below); | |
} | |
execute_body = value < below; | |
} else { | |
execute_body = value === true || value === 'true' || value === 'TRUE' || value === 't' || value === 'T' || value === 1 || value === '1' || value === 'on' || value === 'ON' || value === 'yes' || value === 'YES' || value === 'y' || value === 'Y'; | |
} | |
} | |
} | |
return execute_body; | |
}; | |
var filter_helper = function(chunk, context, bodies, params) { | |
var filter_type; | |
if ((params != null ? params.type : void 0) != null) { | |
filter_type = _eval_dust_string(params.type, chunk, context); | |
} | |
return chunk.capture(bodies.block, context, function(data, chunk) { | |
if (filter_type != null) { | |
data = dust.filters[filter_type](data); | |
} | |
chunk.write(data); | |
return chunk.end(); | |
}); | |
}; | |
var repeat_helper = function(chunk, context, bodies, params) { | |
var i, times, _i, _ref, _ref1, _ref2, _ref3; | |
times = parseInt(_eval_dust_string(params.times, chunk, context)); | |
if ((times != null) && !isNaN(times)) { | |
if ((_ref = context.stack.head) != null) { | |
_ref['$len'] = times; | |
} | |
for (i = _i = 0; 0 <= times ? _i < times : _i > times; i = 0 <= times ? ++_i : --_i) { | |
if ((_ref1 = context.stack.head) != null) { | |
_ref1['$idx'] = i; | |
} | |
chunk = bodies.block(chunk, context.push(i, i, times)); | |
} | |
if ((_ref2 = context.stack.head) != null) { | |
_ref2['$idx'] = void 0; | |
} | |
if ((_ref3 = context.stack.head) != null) { | |
_ref3['$len'] = void 0; | |
} | |
} | |
return chunk; | |
}; | |
var upcase_helper = function(chunk, context, bodies, params) { | |
return chunk.capture(bodies.block, context, function(data, chunk) { | |
chunk.write(data.toUpperCase()); | |
return chunk.end(); | |
}); | |
}; | |
var unless_helper = function(chunk, context, bodies, params) { | |
var execute_body; | |
execute_body = _inner_if_helper(chunk, context, bodies, params); | |
execute_body = !execute_body; | |
return _render_if_else(execute_body, chunk, context, bodies, params); | |
}; | |
var titlecase_helper = function(chunk, context, bodies, params) { | |
return chunk.capture(bodies.block, context, function(data, chunk) { | |
chunk.write(data.replace(/([^\W_]+[^\s-]*) */g, (function(txt) { | |
return txt.charAt(0).toUpperCase() + txt.substr(1); | |
}))); | |
return chunk.end(); | |
}); | |
}; | |
var downcase_helper = function(chunk, context, bodies, params) { | |
return chunk.capture(bodies.block, context, function(data, chunk) { | |
chunk.write(data.toLowerCase()); | |
return chunk.end(); | |
}); | |
}; | |
var first_helper = function(chunk, context, bodies, params) { | |
var c, _ref; | |
if ((context != null ? (_ref = context.stack) != null ? _ref.index : void 0 : void 0) != null) { | |
c = context.stack.index === 0; | |
return _render_if_else(c, chunk, context, bodies, params); | |
} | |
return chunk; | |
}; | |
var last_helper = function(chunk, context, bodies, params) { | |
var c, _ref; | |
if ((context != null ? (_ref = context.stack) != null ? _ref.index : void 0 : void 0) != null) { | |
c = context.stack.index === (context.stack.of - 1); | |
return _render_if_else(c, chunk, context, bodies, params); | |
} | |
return chunk; | |
}; | |
var odd_helper = function(chunk, context, bodies, params) { | |
var c, _ref; | |
if ((context != null ? (_ref = context.stack) != null ? _ref.index : void 0 : void 0) != null) { | |
c = context.stack.index % 2 === 1; | |
return _render_if_else(c, chunk, context, bodies, params); | |
} | |
return chunk; | |
}; | |
var even_helper = function(chunk, context, bodies, params) { | |
var c, _ref; | |
if ((context != null ? (_ref = context.stack) != null ? _ref.index : void 0 : void 0) != null) { | |
c = context.stack.index % 2 === 0; | |
return _render_if_else(c, chunk, context, bodies, params); | |
} | |
return chunk; | |
}; | |
var count_helper = function(chunk, context, bodies, params) { | |
var value; | |
value = _eval_dust_string(params.of, chunk, context); | |
if ((value != null ? value.length : void 0) != null) { | |
chunk.write(value.length); | |
} | |
return chunk; | |
}; | |
dust.helpers['upcase'] = upcase_helper; | |
dust.helpers['unless'] = unless_helper; | |
dust.helpers['downcase'] = downcase_helper; | |
dust.helpers['titlecase'] = titlecase_helper; | |
dust.helpers['filter'] = filter_helper; | |
dust.helpers['count'] = count_helper; | |
dust.helpers['repeat'] = repeat_helper; | |
dust.helpers['first'] = first_helper; | |
dust.helpers['last'] = last_helper; | |
dust.helpers['even'] = even_helper; | |
dust.helpers['odd'] = odd_helper; | |
}(typeof exports !== 'undefined' ? module.exports = require('dustjs-linkedin') : dust)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment