Skip to content
This repository has been archived by the owner on Oct 15, 2023. It is now read-only.

fix: should find all cycles info when cycle exists #2

Merged
merged 2 commits into from
May 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 13 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,23 @@ function getCycles/*::<T>*/(currDepsMap /*: Graph<T> */, visited /*: Graph<T> */

// For each dep,
for (let dep of deps) {
// Check if this dep creates a cycle. We know it's a cycle if the first
// item is the same as our dep.
if (cycle[0] === dep) {
cycles.push(cycle);
// Skip if this dep has already been visited.
if (visitedDeps.includes(dep)) {
continue;
}

visitedDeps.push(dep);

// Check if this dep creates a cycle. We know it's a cycle If dep is already in cycle.
const idx = cycle.indexOf(dep);
if (idx > -1) {
cycles.push(cycle.slice(idx));
continue;
}

// If an item hasn't been visited, visit it (and pass an updated
// potential cycle)
if (!visitedDeps.includes(dep)) {
visitedDeps.push(dep);
visit(dep, cycle.concat(dep));
}
visit(dep, cycle.concat(dep));
}
}

Expand Down