Created
July 16, 2013 14:03
-
-
Save ahomu/6008980 to your computer and use it in GitHub Desktop.
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
'use strict'; | |
(function() { | |
/*jshint eqeqeq:false, maxparams:4 */ | |
var COND_OPERATORS = { | |
'==' : function (l, r) { return l == r; }, | |
'===': function (l, r) { return l === r; }, | |
'!=' : function (l, r) { return l != r; }, | |
'!==': function (l, r) { return l !== r; }, | |
'<' : function (l, r) { return l < r; }, | |
'>' : function (l, r) { return l > r; }, | |
'<=' : function (l, r) { return l <= r; }, | |
'>=' : function (l, r) { return l >= r; }, | |
'%' : function (l, r) { return l % r; }, | |
'in' : function (l, r) { | |
if (l.match(/^[0-9]+$/)) { | |
l = parseInt(l, 10); | |
} | |
return r.indexOf(l) !== -1; }, | |
'typeof': function (l, r) { | |
return typeof l == r; | |
} | |
}; | |
Handlebars.registerHelper('ifCond', function(lvalue, operator, rvalue, options) { | |
var result; | |
if (options === undefined) { | |
options = rvalue; | |
rvalue = operator; | |
operator = '==='; | |
} | |
if (arguments.length === 1) { | |
if (lvalue) { | |
return options.fn(this); | |
} else { | |
return options && options.inverse ? options.inverse(this) : ''; | |
} | |
} | |
if (!COND_OPERATORS[operator]) { | |
throw new Error('Handlerbars Helper "ifCond" doesn\'t know the operator ' + operator); | |
} | |
result = COND_OPERATORS[operator](lvalue, rvalue); | |
if (result) { | |
return options.fn(this); | |
} else { | |
// TODO which case, why inverse is not defined? | |
return options && options.inverse ? options.inverse(this) : ''; | |
} | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment