1 | const chalk = require('chalk');
|
2 |
|
3 | class CallChain {
|
4 | constructor(name, parent) {
|
5 | this.name = name;
|
6 | this.parent = parent;
|
7 | }
|
8 |
|
9 | static create(name) {
|
10 | return new CallChain(name);
|
11 | }
|
12 |
|
13 | add(name) {
|
14 | return new CallChain(name, this);
|
15 | }
|
16 |
|
17 | hasParentName(name) {
|
18 | return this.name === name || (this.parent && this.parent.hasParentName(name));
|
19 | }
|
20 |
|
21 | hasCyclic() {
|
22 | const parent = this.parent;
|
23 | if (parent) {
|
24 | return parent.hasParentName(this.name);
|
25 | }
|
26 | return false;
|
27 | }
|
28 |
|
29 | getHighlightedName() {
|
30 | return chalk.yellow(this.name);
|
31 | }
|
32 |
|
33 | getPath(highlight) {
|
34 | let path = '';
|
35 | if (this.parent) {
|
36 | path = `${this.parent.getPath()} -> `;
|
37 | }
|
38 | if (highlight) {
|
39 | path += this.getHighlightedName();
|
40 | } else {
|
41 | path += this.name;
|
42 | }
|
43 | return path;
|
44 | }
|
45 | }
|
46 |
|
47 | module.exports = CallChain;
|