jriecken / dependency-graph

A simple dependency graph for Node.js
http://jriecken.github.io/dependency-graph/
MIT License
333 stars 49 forks source link

Add a way to remove a node and all dependant nodes from a DepGraph instance #6

Closed waddlesplash closed 7 years ago

waddlesplash commented 9 years ago

As in title. Say I have a node that's in the "middle" -- it has some dependents and some dependencies. I want to remove this node and all of its dependent nodes (and all of their dependent nodes) from the dependency graph.

I have a semi-hacky solution that does this, but it'd be nice if dependency-graph had a function to do it.

jriecken commented 9 years ago

The dependenciesOf and dependantsOf function both return the full set of nodes that depend on, or are depended on by the given node (i.e it's not just the direct dependencies/dependants)

Seems like you could do this pretty easily with the existing API - E.g.

// Make a graph that looks like
//   /-- 2 -- 4 --\
//  1              6
//   \-- 3 -- 5 --/

var g = new DepGraph();
g.addNode('1');
g.addNode('2');
g.addNode('3');
g.addNode('4');
g.addNode('5');
g.addNode('6');
g.addDependency('1', '2');
g.addDependency('1', '3');
g.addDependency('2', '4');
g.addDependency('3', '5');
g.addDependency('4', '6');
g.addDependency('5', '6');

// Remove Node 5 and everything that depends on it

g.dependantsOf('5').forEach(function (n) { g.removeNode(n); });
g.removeNode('5');

// The graph is now
//       2 -- 4 --\
//                 6
//   
waddlesplash commented 9 years ago

That's an OK workaround, I suppose.