UNPKG

3.69 kBJavaScriptView Raw
1/**
2 * This module registers different convenient Handlebars helpers.
3 * @module HandlebarsHelper
4 */
5
6const _ = require('lodash');
7const Handlebars = require('handlebars');
8
9/**
10 * Compares two values.
11 */
12Handlebars.registerHelper('equal', (lvalue, rvalue, options) => {
13 if (arguments.length < 3)
14 throw new Error('Handlebars Helper equal needs 2 parameters');
15 if (lvalue!=rvalue) {
16 return options.inverse(this);
17 }
18
19 return options.fn(this);
20});
21
22/**
23 * Checks if a string ends with a provided value.
24 */
25Handlebars.registerHelper('endsWith', (lvalue, rvalue, options) => {
26 if (arguments.length < 3)
27 throw new Error('Handlebars Helper equal needs 2 parameters');
28 if (lvalue.lastIndexOf(rvalue) !== lvalue.length-1 || lvalue.length-1 < 0) {
29 return options.inverse(this);
30 }
31 return options.fn(this);
32});
33
34/**
35 * Checks if a method is a valid HTTP method.
36 */
37Handlebars.registerHelper('validMethod', (method, options) => {
38 const authorized_methods = ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'COPY', 'HEAD', 'OPTIONS', 'LINK', 'UNLIK', 'PURGE', 'LOCK', 'UNLOCK', 'PROPFIND'];
39
40 if (arguments.length < 3)
41 throw new Error('Handlebars Helper validMethod needs 1 parameter');
42 if (authorized_methods.indexOf(method.toUpperCase()) === -1) {
43 return options.inverse(this);
44 }
45
46 return options.fn(this);
47});
48
49/**
50 * Checks if a collection of responses contains no error responses.
51 */
52Handlebars.registerHelper('ifNoErrorResponses', (responses, options) => {
53 const codes = responses ? Object.keys(responses) : [];
54 if (codes.find(code => Number(code) >= 400)) return options.inverse(this);
55
56 return options.fn(this);
57});
58
59/**
60 * Checks if a collection of responses contains no success responses.
61 */
62Handlebars.registerHelper('ifNoSuccessResponses', (responses, options) => {
63 const codes = responses ? Object.keys(responses) : [];
64 if (codes.find(code => Number(code) >= 200 && Number(code) < 300)) return options.inverse(this);
65
66 return options.fn(this);
67});
68
69/**
70 * Checks if a string matches a RegExp.
71 */
72Handlebars.registerHelper('match', (lvalue, rvalue, options) => {
73 if (arguments.length < 3)
74 throw new Error('Handlebars Helper match needs 2 parameters');
75 if (!lvalue.match(rvalue)) {
76 return options.inverse(this);
77 }
78
79 return options.fn(this);
80});
81
82/**
83 * Provides different ways to compare two values (i.e. equal, greater than, different, etc.)
84 */
85Handlebars.registerHelper('compare', (lvalue, rvalue, options) => {
86 if (arguments.length < 3) throw new Error('Handlebars Helper "compare" needs 2 parameters');
87
88 const operator = options.hash.operator || '==';
89 const operators = {
90 '==': (l,r) => { return l == r; },
91 '===': (l,r) => { return l === r; },
92 '!=': (l,r) => { return l != r; },
93 '<': (l,r) => { return l < r; },
94 '>': (l,r) => { return l > r; },
95 '<=': (l,r) => { return l <= r; },
96 '>=': (l,r) => { return l >= r; },
97 typeof: (l,r) => { return typeof l == r; }
98 };
99
100 if (!operators[operator]) throw new Error(`Handlebars Helper 'compare' doesn't know the operator ${operator}`);
101
102 const result = operators[operator](lvalue,rvalue);
103
104 if (result) {
105 return options.fn(this);
106 }
107
108 return options.inverse(this);
109});
110
111/**
112 * Capitalizes a string.
113 */
114Handlebars.registerHelper('capitalize', (str) => {
115 return _.capitalize(str);
116});
117
118/**
119 * Converts a string to its camel-cased version.
120 */
121Handlebars.registerHelper('camelCase', (str) => {
122 return _.camelCase(str);
123});
124
125/**
126 * Converts a multi-line string to a single line.
127 */
128Handlebars.registerHelper('inline', (str) => {
129 return str ? str.replace(/\n/g, '') : '';
130});