UNPKG

1.09 kBPlain TextView Raw
1import { IHtmlEngineHelper, IHandlebarsOptions } from './html-engine-helper.interface';
2
3export class CompareHelper implements IHtmlEngineHelper {
4 public helperFunc(
5 context: any,
6 a: any,
7 operator: string,
8 b: any,
9 options: IHandlebarsOptions
10 ): string {
11 if (arguments.length < 4) {
12 throw new Error('handlebars Helper {{compare}} expects 4 arguments');
13 }
14
15 let result;
16 switch (operator) {
17 case 'indexof':
18 result = b.indexOf(a) !== -1;
19 break;
20 case '===':
21 result = a === b;
22 break;
23 case '!==':
24 result = a !== b;
25 break;
26 case '>':
27 result = a > b;
28 break;
29 default: {
30 throw new Error('helper {{compare}}: invalid operator: `' + operator + '`');
31 }
32 }
33
34 if (result === false) {
35 return options.inverse(context);
36 }
37 return options.fn(context);
38 }
39}