-
Notifications
You must be signed in to change notification settings - Fork 229
Expand file tree
/
Copy pathhandlebars.js
More file actions
154 lines (130 loc) · 3.44 KB
/
handlebars.js
File metadata and controls
154 lines (130 loc) · 3.44 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
const crypto = require('crypto');
const _ = require('lodash');
const Handlebars = require('handlebars');
const moment = require('moment');
const replacer = (key, value) => {
if (_.isObject(value)) {
return _.transform(value, (result, v, k) => {
result[Handlebars.Utils.escapeExpression(k)] = v;
});
} else if (_.isString(value)) {
return Handlebars.Utils.escapeExpression(value);
} else {
return value;
}
};
// For jobs that don't have a valid ID, produce a random ID we can use in its place.
const idMapping = new WeakMap();
const getTimestamp = (job) => {
// Bull
if (job.timestamp) {
return job.timestamp;
}
// Bee
if (job.options && job.options.timestamp) {
return job.options.timestamp;
}
};
const helpers = {
json(obj, pretty = false) {
const args = [obj, replacer];
if (pretty) {
args.push(2);
}
return new Handlebars.SafeString(JSON.stringify(...args));
},
isNumber(operand) {
return parseInt(operand, 10).toString() === String(operand);
},
adjustedPage(currentPage, pageSize, newPageSize) {
const firstId = (currentPage - 1) * pageSize;
return _.ceil(firstId / newPageSize) + 1;
},
block(name) {
const blocks = this._blocks;
const content = blocks && blocks[name];
return content ? content.join('\n') : null;
},
contentFor(name, options) {
const blocks = this._blocks || (this._blocks = {});
const block = blocks[name] || (blocks[name] = []);
block.push(options.fn(this));
},
hashIdAttr(obj) {
const {id} = obj;
if (typeof id === 'string') {
return crypto.createHash('sha256').update(id).digest('hex');
}
let mapping = idMapping.get(obj);
if (!mapping) {
mapping = crypto.randomBytes(32).toString('hex');
idMapping.set(obj, mapping);
}
return mapping;
},
getDelayedExecutionAt(job) {
// Bull
if (job.delay) {
if (job.processedOn) {
return job.processedOn + job.delay;
} else {
return job.timestamp + job.delay;
}
}
// Bee
if (job.options && job.options.delay) {
return job.options.delay;
}
},
getTimestamp,
encodeURI(url) {
if (typeof url !== 'string') {
return '';
}
return encodeURIComponent(url);
},
capitalize(value) {
if (typeof value !== 'string') {
return '';
}
return value.charAt(0).toUpperCase() + value.slice(1);
},
add(a, b) {
if (Handlebars.helpers.isNumber(a) && Handlebars.helpers.isNumber(b)) {
return parseInt(a, 10) + parseInt(b, 10);
}
if (typeof a === 'string' && typeof b === 'string') {
return a + b;
}
return '';
},
subtract(a, b) {
if (!Handlebars.helpers.isNumber(a)) {
throw new TypeError('expected the first argument to be a number');
}
if (!Handlebars.helpers.isNumber(b)) {
throw new TypeError('expected the second argument to be a number');
}
return parseInt(a, 10) - parseInt(b, 10);
},
length(value) {
if (typeof value === 'string' || Array.isArray(value)) {
return value.length;
}
return 0;
},
moment(date, format) {
return moment(date).format(format);
},
eq(a, b, options) {
return a === b ? options.fn(this) : options.inverse(this);
},
};
module.exports = function registerHelpers(hbs, {queues}) {
_.each(helpers, (fn, helper) => {
hbs.registerHelper(helper, fn);
});
hbs.registerHelper('useCdn', () => {
return queues.useCdn;
});
};