Skip to content

Commit

Permalink
Merge pull request #11 from zolaemil/improve-readability
Browse files Browse the repository at this point in the history
Improve readability
  • Loading branch information
jriecken committed Jan 19, 2016
2 parents 371b23f + 6c41a18 commit 9eef03d
Showing 1 changed file with 37 additions and 39 deletions.
76 changes: 37 additions & 39 deletions lib/dep_graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
};
}
Expand All @@ -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);
}
Expand All @@ -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,
Expand Down Expand Up @@ -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);
}
},
/**
Expand All @@ -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);
}
},
/**
Expand Down

0 comments on commit 9eef03d

Please sign in to comment.