Skip to content

Instantly share code, notes, and snippets.

@rafde
Last active May 22, 2020 18:34
Show Gist options
  • Save rafde/61ffde650f392d26c91f533115f92ec1 to your computer and use it in GitHub Desktop.
Save rafde/61ffde650f392d26c91f533115f92ec1 to your computer and use it in GitHub Desktop.
Handlebars helper that compare 2 values in handlebars as if using a conditional. Can act ternary like.
define(function(require) {
'use strict';
var Handlebars = require('handlebars');
var cmpOps = {
'&&': function(val1, val2) {
return val1 && val2;
},
'||': function(val1, val2) {
return val1 || val2;
},
'^': function(val1, val2) {
return val1 ^ val2;
},
'==': function(val1, val2) {
return val1 == val2;
},
'===': function(val1, val2) {
return val1 === val2;
},
'!=': function(val1, val2) {
return val1 != val2;
},
'!==': function(val1, val2) {
return val1 !== val2;
},
'<': function(val1, val2) {
return val1 < val2;
},
'<=': function(val1, val2) {
return val1 <= val2;
},
'>': function(val1, val2) {
return val1 > val2;
},
'>=': function(val1, val2) {
return val1 >= val2;
}
};
/**
* <p>
* Acts like "if" with comparison operators. It can also act like a ternary operator
* if hash arguments pass, fail are given.
* </p>
*
* <p>
* If this is used as a block helper, whatever is in the block has highest precedence
* followed by pass or fail hash arguments
* and followed by the actual boolean result given when evaluated.
* </p>
*
* @example
* {{cmp "1" "==" "1" pass="pass"}}
*
* {{cmp "1" "==" "2" fail="fail"}}
*
* {{! acts like ternary}}
* {{cmp "1" "&&" "2" pass="result" fail="wont fail in this case"}}
*
* {{#cmp "1" "==" "1"}} 1 == 1{{/cmp}}
*
* {{#cmp "1" "===" "1"}} 1 === 1{{/cmp}}
*
* {{#cmp "1" "!=" "2"}} 1 != 2{{/cmp}}
*
* {{#cmp "1" "!==" "2"}} 1 !== 2{{/cmp}}
*
* {{#cmp "1" "<" "2"}} 1 < 2{{/cmp}}
*
* {{#cmp "2" ">" "1"}} 1 > 2{{/cmp}}
*
* {{#cmp "2" ">=" "2"}} 1 >= 2{{/cmp}}
*
* {{#cmp "1" "<=" "2"}} 1 <= 2{{/cmp}}
*
* {{#cmp "1" "and" "2"}} 1 && 2{{/cmp}} // Need to use "and" rather than "&&" or else thymeleaf freaks -AF
*
* {{#cmp "1" "||" "2"}} 1 || 2{{/cmp}}
*
* {{#cmp "1" "^" "2"}} 1 ^ 2{{/cmp}}
*
* {{^cmp (cmp "1" "===" "1") "&&" (cmp "2" "===" "1")}}
* !(1 === 1 && 2 === 1)
* {{/cmp}}
*
* {{#cmp (cmp "1" "===" "1") "||" (cmp "2" "===" "1")}}
* 1 === 1 || 2 === 1
* {{/cmp}}
*
* @method cmp
*
* @memberof external:Handlebars.helpers
*
* @this external:Handlebars.helpers.handlebarsBlockContext
*
* @param val1 {*} Value to evaluate
* @param op {String} Can be ||, and (i.e. &&), ^, ==, ===,
* !=, !==, <, <=, >, >=
* @param val2 {*} Value to evaluate against
* @param [options] {external:Handlebars.helpers.handlebarsObj}
* @param [options.hash.pass] {*} Hash argument that acts as the result
* if evaluates to true
* @param [options.hash.fail] {*} Hash argument that acts as the result
* if evaluates to false
* @return {*}
*/
function cmp(val1, op, val2) {
var evaluateExp,
result,
options,
blockResult,
hash;
if (arguments.length < 3 || typeof (evaluateExp = cmpOps[op]) !== 'function') {
return '';
}
options = arguments[arguments.length - 1];
hash = options.hash;
evaluateExp = evaluateExp(val1, val2);
if (evaluateExp) {
result = hash.pass;
blockResult = options.fn;
} else {
result = hash.fail;
blockResult = options.inverse;
}
if (typeof blockResult === 'function') {
return blockResult(this);
}
if (typeof result !== 'undefined') {
return result;
}
return evaluateExp;
};
Handlebars.registerHelper('cmp', cmp);
return cmp;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment