diff --git a/lib/dep_graph.js b/lib/dep_graph.js index aa3163a..1508aed 100644 --- a/lib/dep_graph.js +++ b/lib/dep_graph.js @@ -15,20 +15,20 @@ function createDFS(edges, leavesOnly, result) { var currentPath = []; var visited = {}; - return function DFS(name) { - visited[name] = true; - currentPath.push(name); - edges[name].forEach(function (edgeName) { - if (!visited[edgeName]) { - DFS(edgeName); - } else if (currentPath.indexOf(edgeName) >= 0) { - currentPath.push(edgeName); + return function DFS(currentNode) { + visited[currentNode] = true; + currentPath.push(currentNode); + edges[currentNode].forEach(function (node) { + if (!visited[node]) { + DFS(node); + } else if (currentPath.indexOf(node) >= 0) { + currentPath.push(node); throw new Error('Dependency Cycle Found: ' + currentPath.join(' -> ')); } }); currentPath.pop(); - if ((!leavesOnly || edges[name].length === 0) && result.indexOf(name) === -1) { - result.push(name); + if ((!leavesOnly || edges[currentNode].length === 0) && result.indexOf(currentNode) === -1) { + result.push(currentNode); } }; } @@ -38,33 +38,31 @@ function createDFS(edges, leavesOnly, result) { */ var DepGraph = exports.DepGraph = function DepGraph() { this.nodes = {}; - this.outgoingEdges = {}; // Node name -> [Dependency Node name] - this.incomingEdges = {}; // Node name -> [Dependant Node name] + this.outgoingEdges = {}; // Node -> [Dependency Node] + this.incomingEdges = {}; // Node -> [Dependant Node] }; DepGraph.prototype = { /** - * Add a node to the dependency graph. If a node with the specified - * name already exists, this method will do nothing. + * Add a node to the dependency graph. If a node already exists, this method will do nothing. */ - addNode:function (name) { - if (!this.hasNode(name)) { - this.nodes[name] = name; - this.outgoingEdges[name] = []; - this.incomingEdges[name] = []; + addNode:function (node) { + if (!this.hasNode(node)) { + this.nodes[node] = node; + this.outgoingEdges[node] = []; + this.incomingEdges[node] = []; } }, /** - * Remove a node from the dependency graph. If a node with the specified - * name does not exist, this method will do nothing. + * Remove a node from the dependency graph. If a node does not exist, this method will do nothing. */ - removeNode:function (name) { - if (this.hasNode(name)) { - delete this.nodes[name]; - delete this.outgoingEdges[name]; - delete this.incomingEdges[name]; + removeNode:function (node) { + if (this.hasNode(node)) { + delete this.nodes[node]; + delete this.outgoingEdges[node]; + delete this.incomingEdges[node]; [this.incomingEdges, this.outgoingEdges].forEach(function (edgeList) { Object.keys(edgeList).forEach(function (key) { - var idx = edgeList[key].indexOf(name); + var idx = edgeList[key].indexOf(node); if (idx >= 0) { edgeList[key].splice(idx, 1); } @@ -75,8 +73,8 @@ DepGraph.prototype = { /** * Check if a node exists in the graph */ - hasNode:function (name) { - return !!this.nodes[name]; + hasNode:function (node) { + return !!this.nodes[node]; }, /** * Add a dependency between two nodes. If either of the nodes does not exist, @@ -124,19 +122,19 @@ DepGraph.prototype = { * If `leavesOnly` is true, only nodes that do not depend on any other nodes will be returned * in the array. */ - dependenciesOf:function (name, leavesOnly) { - if (this.hasNode(name)) { + dependenciesOf:function (node, leavesOnly) { + if (this.hasNode(node)) { var result = []; var DFS = createDFS(this.outgoingEdges, leavesOnly, result); - DFS(name); - var idx = result.indexOf(name); + DFS(node); + var idx = result.indexOf(node); if (idx >= 0) { result.splice(idx, 1); } return result; } else { - throw new Error('Node does not exist: ' + name); + throw new Error('Node does not exist: ' + node); } }, /** @@ -146,18 +144,18 @@ DepGraph.prototype = { * * If `leavesOnly` is true, only nodes that do not have any dependants will be returned in the array. */ - dependantsOf:function (name, leavesOnly) { - if (this.hasNode(name)) { + dependantsOf:function (node, leavesOnly) { + if (this.hasNode(node)) { var result = []; var DFS = createDFS(this.incomingEdges, leavesOnly, result); - DFS(name); - var idx = result.indexOf(name); + DFS(node); + var idx = result.indexOf(node); if (idx >= 0) { result.splice(idx, 1); } return result; } else { - throw new Error('Node does not exist: ' + name); + throw new Error('Node does not exist: ' + node); } }, /**