-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.js
107 lines (97 loc) · 2.81 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
const path = require('path');
const fs = require('fs');
const Handlebars = require('handlebars');
Handlebars.registerHelper('gte', function (v1, v2, options) {
if (v1 >= v2) {
return options.fn(this);
}
return options.inverse(this);
});
Handlebars.registerHelper('lt', function (v1, v2, options) {
if (v1 < v2) {
return options.fn(this);
}
return options.inverse(this);
});
Handlebars.registerHelper('lte', function (v1, v2, options) {
if (v1 <= v2) {
return options.fn(this);
}
return options.inverse(this);
});
Handlebars.registerHelper('len', function (arg) {
if (Array.isArray(arg)) {
return arg.length;
}
return 0;
});
Handlebars.registerHelper('empty', function (arg) {
if (Array.isArray(arg)) {
return arg.length === 0;
}
return true;
});
Handlebars.registerHelper('last', function (arg) {
if (Array.isArray(arg)) {
return arg[arg.length - 1];
}
throw new Error('last must be called with an array');
});
Handlebars.registerHelper('after', function (arg, index, length) {
if (Array.isArray(arg)) {
return arg.slice(index, typeof length === 'number' ? index + length : undefined);
}
throw new Error('after must be called with an array');
});
Handlebars.registerHelper('pick', function (arg, index, offset = 0) {
if (Array.isArray(arg)) {
return arg.slice(offset)[index];
}
throw new Error('pick must be called with an array');
});
Handlebars.registerHelper('include', function (arg) {
const baseDir = arg.data.root.__dirname;
const fn = path.resolve(baseDir, arg.hash.src);
const context = {
...arg.data.root,
...(arg.hash.context || {}),
};
const keys = Object.keys(arg.hash).filter((k) => k !== 'src' && k !== 'context');
keys.forEach((key) => (context[key] = arg.hash[key]));
const buf = fs.readFileSync(fn).toString();
return new Handlebars.SafeString(Handlebars.compile(buf)(context));
});
Handlebars.registerHelper('ifCond', function (v1, operator, v2, options) {
switch (operator) {
case '==':
return v1 == v2 ? options.fn(this) : options.inverse(this);
case 'eq':
case '===':
return v1 === v2 ? options.fn(this) : options.inverse(this);
case '!=':
return v1 != v2 ? options.fn(this) : options.inverse(this);
case 'neq':
case '!==':
return v1 !== v2 ? options.fn(this) : options.inverse(this);
case 'lt':
case '<':
return v1 < v2 ? options.fn(this) : options.inverse(this);
case 'lte':
case '<=':
return v1 <= v2 ? options.fn(this) : options.inverse(this);
case 'gt':
case '>':
return v1 > v2 ? options.fn(this) : options.inverse(this);
case 'gte':
case '>=':
return v1 >= v2 ? options.fn(this) : options.inverse(this);
case 'and':
case '&&':
return v1 && v2 ? options.fn(this) : options.inverse(this);
case 'or':
case '||':
return v1 || v2 ? options.fn(this) : options.inverse(this);
default:
return options.inverse(this);
}
});