diff --git a/lib/dep_graph.js b/lib/dep_graph.js index 1954a62..8ffe7ef 100644 --- a/lib/dep_graph.js +++ b/lib/dep_graph.js @@ -178,6 +178,41 @@ DepGraph.prototype = { } } }, + /** + * Combines all outgoingEdges of list of nodes + */ + getNodesNextLevel: function(nodes) { + var source = this; + var nodesInLevel = [] + for (i = 0; i < nodes.length; i++) { + nodesInLevel = nodesInLevel.concat(source.outgoingEdges[nodes[i]]) + } + return [...new Set(nodesInLevel)] + }, + /** + * Get nodes at each level + * .e.g getNodesEachLevel() + */ + getNodesEachLevel: function(nodes, out, level) { + if(this.circular){ + throw new Error("getNodesEachLevel only works for trees") + } + if(out == undefined){ + out = [] + } + if(nodes == undefined){ + nodes = [Object.keys(this.nodes)[0]] //set root + out.push(nodes) + level = 0 + } + if(this.getNodesNextLevel(nodes).length == 0){ + return out + }else{ + out.push(this.getNodesNextLevel(nodes)) + } + level++ + return this.getNodesEachLevel(out[level], out, level) + }, /** * Return a clone of the dependency graph. If any custom data is attached * to the nodes, it will only be shallow copied.