Created
February 14, 2017 04:46
-
-
Save nemesisqp/d36219620967a3ba52ba87c2841b137f to your computer and use it in GitHub Desktop.
handlebars compareTime helper
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
Handlebars.registerHelper('compareTime', function(lhs, operator, rhs, options) { | |
/*eslint eqeqeq: 0*/ | |
if (arguments.length < 4) { | |
throw new Error('handlebars Helper {{compareTime}} expects 4 arguments'); | |
} | |
var a = rhs, b = rhs; | |
if (!moment.isMoment(a)) a = moment(lhs); | |
if (!moment.isMoment(b)) b = moment(rhs); | |
if (!a.isValid()) throw new Error('helper {{compareTime}}: invalid first input time: `' + lhs + '`'); | |
if (!b.isValid()) throw new Error('helper {{compareTime}}: invalid second input time: `' + rhs + '`'); | |
var result; | |
switch (operator) { | |
case '==': | |
case '===': | |
result = a.isSame(b); | |
break; | |
case '!=': | |
case '!==': | |
result = !(a.isSame(b)); | |
break; | |
case '<': | |
result = a.isBefore(b); | |
break; | |
case '>': | |
result = a.isAfter(b); | |
break; | |
case '<=': | |
result = a.isSameOrBefore(b); | |
break; | |
case '>=': | |
result = a.isSameOrAfter(b); | |
break; | |
default: { | |
throw new Error('helper {{compareTime}}: invalid operator: `' + operator + '`'); | |
} | |
} | |
if (result === false) { | |
return options.inverse(this); | |
} | |
return options.fn(this); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment