-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrender-organisation.ts
More file actions
111 lines (103 loc) · 3 KB
/
render-organisation.ts
File metadata and controls
111 lines (103 loc) · 3 KB
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 { Department, Employee, EmployeeRole } from '../model';
import { bold, dim, greenBright, yellow } from 'ansis';
type OrganisationStyle = {
department: (text: string) => string;
employee: (text: string) => string;
role: (text: string) => string;
task: (text: string) => string;
};
type OrganisationDecoration = {
department: string;
employees: Record<Extract<EmployeeRole, 'A' | 'B' | 'C'>, string>;
unknownEmployee: string;
task: string;
taskSeparator: string;
roleDecorators: { start: string; end: string };
};
type OrganisationTransformation = {
role: (role: EmployeeRole) => string;
};
export type OrganisationConfig = {
style: OrganisationStyle;
decoration: OrganisationDecoration;
transformation: OrganisationTransformation;
};
export type RenderOrganisation = {
renderDepartment(department: Department): string;
renderEmployee(employee: Employee): string;
};
/**
* Creates an organisation renderer with the given configuration.
*
* @param config
* @returns RenderOrganisation
*
* @example
* ```ts
* const renderer = createOrganisationRenderer();
*
* const department = { name: 'Engineering' };
* console.log(renderer.renderDepartment(department)); // 🏢 Engineering
*
* const employee = { name: 'Alice', role: 'C', tasks: [1, 2] };
* console.log(renderer.renderEmployee(employee)); // 👩💼 Alice ≺🎖️Manager≻ | 2🛠️
* ```
*/
export function createOrganisationRenderer(
config: Partial<OrganisationConfig> = {
style: {
department: bold.gray,
employee: bold,
role: yellow,
task: greenBright,
},
decoration: {
department: '🏢',
employees: {
A: '👩💻', // Engineer
B: '👩⚕️', // Supervisor
C: '👩💼', // Manager
},
unknownEmployee: '👤',
task: '🛠️',
roleDecorators: { start: '≺🎖️', end: '≻' },
taskSeparator: dim('|'),
},
transformation: {
role: (role: EmployeeRole) => {
return role === 'A'
? 'Engineer'
: role === 'B'
? 'Supervisor'
: role === 'C'
? 'Manager'
: role === 'X'
? 'Contractor'
: 'Unknown Role';
},
},
}
): RenderOrganisation {
return {
renderDepartment(department: Department) {
return `${config.style.department(
config.decoration.department + ' ' + department.name
)}`;
},
renderEmployee(employee: Employee) {
const roleEmoji =
config.decoration.employees[employee.role] ||
config.decoration.unknownEmployee;
const roleText = config.style.role(
`${config.decoration.roleDecorators.start}${config.transformation.role(
employee.role
)}${config.decoration.roleDecorators.end}`
);
return `${config.style.employee(
roleEmoji + ' ' + employee.name
)} ${roleText} ${config.decoration.taskSeparator} ${config.style.task(
employee.tasks.length + config.decoration.task
)}`;
},
};
}