Last active
August 29, 2015 14:05
-
-
Save simongong/bcde1a8a79b942046e45 to your computer and use it in GitHub Desktop.
JavaScript: handlebarsHelpers
This file contains hidden or 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
// 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