-
Notifications
You must be signed in to change notification settings - Fork 1
/
helpers.js
124 lines (103 loc) · 3.8 KB
/
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
Handlebars.registerHelper('attributes', function(options) {
var root = options.data.root;
if (Handlebars.Utils.isEmpty(root.definitions)) {
return '';
}
else {
var buffer = [];
var copies = Array.filter(root.definitions,
function(item) { return !item.negate; });
var index = -1;
copies.sort(function(a, b) {
return (a.attribute.name > b.attribute.name) ? 1 :
((b.attribute.name > a.attribute.name) ? -1 : 0); });
for (var i = 0, j = copies.length; i < j; i++) {
var copy = copies[i];
if (root.attributes.indent) {
copy = Object.extend(copy);
var delta = root.attributes.longestName -
copy.attribute.name.length;
var indent = '';
if (delta > 0) {
indent = new Array(delta + 1).join(' ');
}
copy.indent = indent;
}
// faster array push
buffer[++index] = options.fn(copy);
}
return buffer.join(root.attributes.separator);
}
});
Handlebars.registerHelper('default', function(value, defaultValue) {
return (value === 0) ? value : (value || defaultValue);
});
Handlebars.registerHelper('equals', function(operand1, operand2, options) {
return (operand1 === operand2) ? options.fn(this) :
options.inverse(this);
});
Handlebars.registerHelper('fill', function(options) {
var root = options.data.root;
if (Handlebars.Utils.isEmpty(root.definitions)) {
return '';
}
else {
var buffer = [];
var copies = Array.filter(root.definitions, function(item) {
return '|checkbox|radio|select|text|'.indexOf('|' +
item.type + '|') > -1 && !item.negate;
});
var index = -1;
copies.sort(function(a, b) {
return (a.sourceIndex > b.sourceIndex) ? 1 :
((b.sourceIndex > a.sourceIndex) ? -1 : 0); });
for (var i = 0, j = copies.length; i < j; i++) {
// faster array push
buffer[++index] = options.fn(copies[i]);
}
return buffer.join(root.fill.separator);
}
});
Handlebars.registerHelper('lower', function(value) {
var response = value;
var type = typeof(value);
if ('|string|undefined|'.indexOf('|' + type + '|') > -1 || value === null) {
response = (value || '').toLowerCase();
}
return response;
});
Handlebars.registerHelper('operations', function(options) {
var root = options.data.root;
if (Handlebars.Utils.isEmpty(root.definitions)) {
return '';
}
else {
var buffer = [];
var copies = Array.filter(root.definitions,
function(item) { return !!item.operation.name; });
var index = -1;
copies.sort(function(a, b) {
return (a.operation.name > b.operation.name) ? 1 :
((b.operation.name > a.operation.name) ? -1 : 0); });
for (var i = 0, j = copies.length; i < j; i++) {
// faster array push
buffer[++index] = options.fn(copies[i]);
}
return buffer.join(root.operations.separator);
}
});
Handlebars.registerHelper('proper', function(value) {
var response = value;
var type = typeof(value);
if ('|string|undefined|'.indexOf('|' + type + '|') > -1 || value === null) {
response = (value || '').replace(/[,.!?-]+/g, ' ').replace(/\s\s+/g, ' ').
replace(/\w\S*/g, function(word) {
return word.charAt(0).toUpperCase() + word.substr(1).toLowerCase();
});
}
return response;
});
Handlebars.registerHelper('unequals', function(operand1, operand2, options) {
return (operand1 !== operand2) ? options.fn(this) :
options.inverse(this);
});