-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblem.ts
97 lines (88 loc) · 3.58 KB
/
problem.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
import { EmployeeRole, Unit } from './model';
import { bold, dim, greenBright, yellow } from 'ansis';
/**
* Processes the organizational structure and optionally prints it.
*
* @param unit - The organizational unit (department or employee).
* @param printReport - If `true`, prints the organization structure; otherwise, just returns the total duration.
* @param indent - The current indentation level.
* @param isLast - Indicates if the current unit is the last child of its parent.
* @returns The total sum of all task durations.
*
* @example
* const totalDuration = processUnits(PUSH_BASED); // Returns the total duration.
* console.log(`Total Task Duration: ${totalDuration} hours`);
*
* @example
* processUnits(PUSH_BASED, true); // Prints the organization structure and returns the total duration.
* // Output:
* 🏢 Head Office
* ├── 👩💼 Alice ≺🎖️Manager≻ | 2🛠️
* ├── 🏢 Engineering 💻
* │ ├── 👩⚕️ Bob ≺🎖️Supervisor≻ | 1🛠️
* │ ├── 👩💻 Carol ≺🎖Engineer≻ | 2🛠️
* │ ├── 👤 Sutri ≺🎖Engineer≻ | 3🛠️
* │ └── 👩💻 Mardim ≺🎖️Worker≻ | 2🛠️
* └── 🏢 Sales 💼
* └── 👩⚕️ Dave ≺🎖️Supervisor≻ | 1🛠️
*/
export function processUnits(unit: Unit, printReport = false, indent = '', isLast = true): number {
// Adjusted Color Palette
const colors = {
department: bold.gray, // Bold gray for departments
employee: bold, // Default bold for employee names
role: yellow, // Yellow for roles (light and visible)
task: greenBright, // Bright green for task durations
tree: dim, // Dim gray for tree lines
};
// Icons & Styling
const departmentEmoji = '🏢'; // Unified emoji for departments
const employeeEmojis: Record<Extract<EmployeeRole, 'A' | 'B' | 'C'>, string> = {
A: '👩💻', // Engineer
B: '👩⚕️', // Supervisor
C: '👩💼', // Manager
};
const roleDecorators = { start: '≺🎖️', end: '≻' }; // Role bracket styling
const taskSeparator = colors.tree('|'); // Task separator
const tasksEmoji = '🛠️'; // Icon for tasks
let totalDuration = 0;
// Prefix for tree structure
let prefix = indent;
if (indent) {
prefix += isLast ? colors.tree('└── ') : colors.tree('├── ');
}
if (unit.type === 'department') {
if (printReport) {
console.log(`${prefix}${colors.department(departmentEmoji + ' ' + unit.name)}`);
}
const newIndent = indent + (isLast ? ' ' : colors.tree('│ '));
unit.children.forEach((child, index) => {
const childIsLast = index === unit.children.length - 1;
totalDuration += processUnits(child, printReport, newIndent, childIsLast);
});
} else if (unit.type === 'employee') {
const roleEmoji = employeeEmojis[unit.role] || ' 👤';
const roleText = colors.role(
`${roleDecorators.start}${
unit.role === 'A'
? 'Engineer'
: unit.role === 'B'
? 'Supervisor'
: unit.role === 'C'
? 'Manager'
: 'Contractor'
}${roleDecorators.end}`
);
const totalTasks = unit.tasks.length;
const employeeDuration = unit.tasks.reduce((sum, task) => sum + task.duration, 0);
totalDuration += employeeDuration;
if (printReport) {
console.log(
`${prefix}${colors.employee(roleEmoji + ' ' + unit.name)} ${roleText} ${taskSeparator} ${colors.task(
totalTasks + tasksEmoji
)}`
);
}
}
return totalDuration;
}