UNPKG

1.51 kBJavaScriptView Raw
1'use strict';
2
3function helper(paper) {
4 paper.handlebars.registerHelper('compare', function (lvalue, rvalue) {
5 const options = arguments[arguments.length - 1];
6 var operator;
7 var operators;
8 var result;
9
10 if (arguments.length < 3) {
11 throw new Error("Handlerbars Helper 'compare' needs 2 parameters");
12 }
13
14 operator = options.hash.operator || "==";
15
16 operators = {
17 '==': function (l, r) { return l == r; },
18 '===': function (l, r) { return l === r; },
19 '!=': function (l, r) { return l != r; },
20 '!==': function (l, r) { return l !== r; },
21 '<': function (l, r) { return l < r; },
22 '>': function (l, r) { return l > r; },
23 '<=': function (l, r) { return l <= r; },
24 '>=': function (l, r) { return l >= r; },
25 'typeof': function (l, r) { return typeof l == r; }
26 };
27
28 // getOwnPropertyNames is used because checking the property
29 // directly (like operators[x]) is insecure
30 // (we could use switch instead)
31 if (Object.getOwnPropertyNames(operators).indexOf(operator) === -1) {
32 throw new Error("Handlerbars Helper 'compare' doesn't know the operator " + operator);
33 }
34
35 result = operators[operator](lvalue, rvalue);
36
37 if (result) {
38 return options.fn(this);
39 } else {
40 return options.inverse(this);
41 }
42 });
43}
44
45module.exports = helper;