-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlebars-helpers.js
138 lines (115 loc) · 3.46 KB
/
handlebars-helpers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
var _ = require('lodash');
var Handlebars = require('handlebars');
module.exports = {
attr: function(value) {
return _.kebabCase(value);
},
compare: function (lvalue, operator, rvalue, options) {
var operators, result;
if (arguments.length < 3) {
throw new Error("Handlerbars Helper 'compare' needs 2 parameters");
}
if (options === undefined) {
options = rvalue;
rvalue = operator;
operator = "===";
}
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; },
'typeof': function (l, r) { return typeof l == r; }
};
if (!operators[operator]) {
throw new Error("Handlerbars Helper 'compare' doesn't know the operator " + operator);
}
result = operators[operator](lvalue, rvalue);
if (result) {
return options.fn(this);
} else {
return options.inverse(this);
}
},
default: function (value, defaultValue) {
return value ? value : defaultValue;
},
'default-each': function(context, defaultContext, options) {
if (!options) {
throw new Error('Must pass iterator and default value to #default-each');
}
let fn = options.fn,
inverse = options.inverse,
i = 0,
ret = '',
data;
context = context || defaultContext;
if (_.isFunction(context)) { context = context.call(this); }
if (options.data) {
data = Handlebars.createFrame(options.data);
}
function execIteration(field, index, last) {
if (data) {
data.key = field;
data.index = index;
data.first = index === 0;
data.last = !!last;
}
ret = ret + fn(context[field], {
data: data,
blockParams: [context[field], field]
});
}
if (context && typeof context === 'object') {
if (Array.isArray(context)) {
for (let j = context.length; i < j; i++) {
if (i in context) {
execIteration(i, i, i === context.length - 1);
}
}
} else {
let priorKey;
for (let key in context) {
if (context.hasOwnProperty(key)) {
// We're running the iterations one step out of sync so we can detect
// the last iteration without have to scan the object twice and create
// an itermediate keys array.
if (priorKey !== undefined) {
execIteration(priorKey, i - 1);
}
priorKey = key;
i++;
}
}
if (priorKey !== undefined) {
execIteration(priorKey, i - 1, true);
}
}
}
if (i === 0) {
ret = inverse(this);
}
return ret;
},
'default-with': function(context, defaultContext, options) {
if (!options) {
throw new Error('Must pass iterator and default value to #default-each');
}
context = context || defaultContext;
if (_.isFunction(context)) { context = context.call(this); }
let fn = options.fn;
if (!_.isEmpty(context)) {
let data = options.data;
return fn(context, {
data: data,
blockParams: [context]
});
} else {
return options.inverse(this);
}
},
};