-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelpers.ts
37 lines (32 loc) · 1.3 KB
/
helpers.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
import { registerHelper, SafeString, HelperOptions } from 'handlebars';
import { Skill } from './skills';
registerHelper('ifCategoriesNotEqual', function(this: any, categories1: object[], categories2: object[], options: HelperOptions) {
if (categories1 && categories2 && categories1[categories1.length - 1] !== categories2[categories2.length - 1]) {
return options.fn(this);
} else {
return options.inverse(this);
}
});
registerHelper('ifEquals', function(this: any, arg1: string, arg2: string, options: HelperOptions) {
return (arg1 === arg2) ? options.fn(this) : options.inverse(this);
});
registerHelper('ifNotEquals', function(this: any, arg1: string, arg2: string, options: HelperOptions) {
return (arg1 !== arg2) ? options.fn(this) : options.inverse(this);
});
registerHelper('nodes', (skills: Skill[]) => {
return new SafeString(JSON.stringify(skills.map(skill => ({
id: skill.slug,
label: skill.name,
group: skill.parent.slug,
})), null, 2));
});
registerHelper('edges', (skills: Skill[]) => {
return new SafeString(JSON.stringify(skills.reduce<{from: string, to: string, arrows: string}[]>((acc, skill) => [
...acc,
...(skill.requires || []).map(connectedSkill => ({
from: connectedSkill.slug,
to: skill.slug,
arrows: 'to',
})),
], []), null, 2));
});