Skip to content

Instantly share code, notes, and snippets.

@simongong
Last active August 29, 2015 14:05
Show Gist options
  • Save simongong/bcde1a8a79b942046e45 to your computer and use it in GitHub Desktop.
Save simongong/bcde1a8a79b942046e45 to your computer and use it in GitHub Desktop.
JavaScript: handlebarsHelpers
// Transform a date string into an object, e.g.
// "2014-04" -> {0:"2014", 1:"04"}
translateDate: function(format, date){
var args = Array.prototype.slice.call(arguments);
var dates = date.split('-');
if(dates.length === 2){
args.splice(1, 1, dates[0], dates[1]);
}else if(dates.length === 3){
args.splice(1, 1, dates[0], dates[1], dates[2]);
}
return translate.apply(this, args);
},
// Enhance #if to support conditional expresstion like
// {{#if a '>' b}}
ifCondition: function(a, operator, b, options){
switch(operator){
case '===':
return (a === b) ? options.fn(this) : options.inverse(this);
case '<':
return (a < b) ? options.fn(this) : options.inverse(this);
case '<=':
return (a <= b) ? options.fn(this) : options.inverse(this);
case '>':
return (a > b) ? options.fn(this) : options.inverse(this);
case '>=':
return (a >= b) ? options.fn(this) : options.inverse(this);
case '&&':
return (a && b) ? options.fn(this) : options.inverse(this);
case '||':
return (a || b) ? options.fn(this) : options.inverse(this);
case 'in':
return (a in b.split(',')) ? options.fn(this) : options.inverse(this);
case 'notin':
return !(a in b.split(',')) ? options.fn(this) : options.inverse(this);
default:
return options.inverse(this);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment