-
-
Notifications
You must be signed in to change notification settings - Fork 534
/
Copy pathregisterHandlebarHelpers.ts
111 lines (99 loc) · 3.89 KB
/
registerHandlebarHelpers.ts
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
import camelCase from 'camelcase';
import Handlebars from 'handlebars/runtime';
import { EOL } from 'os';
import type { Enum } from '../client/interfaces/Enum';
import type { Model } from '../client/interfaces/Model';
import type { HttpClient } from '../HttpClient';
import { unique } from './unique';
export const registerHandlebarHelpers = (root: {
httpClient: HttpClient;
useOptions: boolean;
useUnionTypes: boolean;
}): void => {
Handlebars.registerHelper('ifdef', function (this: any, ...args): string {
const options = args.pop();
if (!args.every(value => !value)) {
return options.fn(this);
}
return options.inverse(this);
});
Handlebars.registerHelper(
'equals',
function (this: any, a: string, b: string, options: Handlebars.HelperOptions): string {
return a === b ? options.fn(this) : options.inverse(this);
}
);
Handlebars.registerHelper(
'notEquals',
function (this: any, a: string, b: string, options: Handlebars.HelperOptions): string {
return a !== b ? options.fn(this) : options.inverse(this);
}
);
Handlebars.registerHelper(
'containsSpaces',
function (this: any, value: string, options: Handlebars.HelperOptions): string {
return /\s+/.test(value) ? options.fn(this) : options.inverse(this);
}
);
Handlebars.registerHelper(
'union',
function (this: any, properties: Model[], parent: string | undefined, options: Handlebars.HelperOptions) {
const type = Handlebars.partials['type'];
const types = properties.map(property => type({ ...root, ...property, parent }));
const uniqueTypes = types.filter(unique);
let uniqueTypesString = uniqueTypes.join(' | ');
if (uniqueTypes.length > 1) {
uniqueTypesString = `(${uniqueTypesString})`;
}
return options.fn(uniqueTypesString);
}
);
Handlebars.registerHelper(
'intersection',
function (this: any, properties: Model[], parent: string | undefined, options: Handlebars.HelperOptions) {
const type = Handlebars.partials['type'];
const types = properties.map(property => type({ ...root, ...property, parent }));
const uniqueTypes = types.filter(unique);
let uniqueTypesString = uniqueTypes.join(' & ');
if (uniqueTypes.length > 1) {
uniqueTypesString = `(${uniqueTypesString})`;
}
return options.fn(uniqueTypesString);
}
);
Handlebars.registerHelper(
'enumerator',
function (
this: any,
enumerators: Enum[],
parent: string | undefined,
name: string | undefined,
options: Handlebars.HelperOptions
) {
if (!root.useUnionTypes && parent && name) {
return `${parent}.${name}`;
}
return options.fn(
enumerators
.map(enumerator => enumerator.value)
.filter(unique)
.join(' | ')
);
}
);
Handlebars.registerHelper('escapeComment', function (value: string): string {
return value
.replace(/\*\//g, '*')
.replace(/\/\*/g, '*')
.replace(/\r?\n(.*)/g, (_, w) => `${EOL} * ${w.trim()}`);
});
Handlebars.registerHelper('escapeDescription', function (value: string): string {
return value.replace(/\\/g, '\\\\').replace(/`/g, '\\`').replace(/\${/g, '\\${');
});
Handlebars.registerHelper('escapeTitle', function (value: string): string {
return value.replace(/\\/g, '\\\\').replace(/`/g, '\\`').replace(/\${/g, '\\${');
});
Handlebars.registerHelper('camelCase', function (value: string): string {
return camelCase(value);
});
};