* Normalizes the a circular dependency by ensuring that the path starts with the first * node in alphabetical order. Since the path array represents a cycle, we can make a * specific node the first element in the path that represents the cycle. * * This method is helpful because the path of circ
(path: CircularDependency)
| 76 | * be the first node in the path that represents the cycle. |
| 77 | */ |
| 78 | function normalizeCircularDependency(path: CircularDependency): CircularDependency { |
| 79 | if (path.length <= 1) { |
| 80 | return path; |
| 81 | } |
| 82 | |
| 83 | let indexFirstNode: number = 0; |
| 84 | let valueFirstNode: string = path[0]; |
| 85 | |
| 86 | // Find a node in the cycle path that precedes all other elements |
| 87 | // in terms of alphabetical order. |
| 88 | for (let i = 1; i < path.length; i++) { |
| 89 | const value = path[i]; |
| 90 | if (value.localeCompare(valueFirstNode, 'en') < 0) { |
| 91 | indexFirstNode = i; |
| 92 | valueFirstNode = value; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | // If the alphabetically first node is already at start of the path, just |
| 97 | // return the actual path as no changes need to be made. |
| 98 | if (indexFirstNode === 0) { |
| 99 | return path; |
| 100 | } |
| 101 | |
| 102 | // Move the determined first node (as of alphabetical order) to the start of a new |
| 103 | // path array. The nodes before the first node in the old path are then concatenated |
| 104 | // to the end of the new path. This is possible because the path represents a cycle. |
| 105 | return [...path.slice(indexFirstNode), ...path.slice(0, indexFirstNode)]; |
| 106 | } |
| 107 | |
| 108 | /** Checks whether the specified circular dependencies are equal. */ |
| 109 | function isSameCircularDependency(actual: CircularDependency, expected: CircularDependency) { |
no test coverage detected