Skip to content

Commit

Permalink
Add entryNodes function (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
amcdnl authored Mar 5, 2021
1 parent 6b8746e commit a91af2f
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Nodes in the graph are just simple strings with optional data associated with th
- `directDependenciesOf(name)` - get an array containing the direct dependencies of the specified node
- `directDependantsOf(name)` (aliased as `directDependentsOf`) - get an array containing the nodes that directly depend on the specified node
- `overallOrder(leavesOnly)` - construct the overall processing order for the dependency graph. If `leavesOnly` is true, only nodes that do not depend on any other nodes will be returned.
- `entryNodes()` - array of nodes that have no dependants (i.e. nothing depends on them).

Dependency Cycles are detected when running `dependenciesOf`, `dependantsOf`, and `overallOrder` and if one is found, a `DepGraphCycleError` will be thrown that includes what the cycle was in the message as well as the `cyclePath` property: e.g. `Dependency Cycle Found: a -> b -> c -> a`. If you wish to silence this error, pass `circular: true` when instantiating `DepGraph` (more below).

Expand All @@ -51,6 +52,7 @@ Dependency Cycles are detected when running `dependenciesOf`, `dependantsOf`, an

graph.overallOrder(); // ['c', 'b', 'a']
graph.overallOrder(true); // ['c']
graph.entryNodes(); // ['a']

graph.addNode('d', 'data');

Expand Down
9 changes: 9 additions & 0 deletions lib/dep_graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,15 @@ DepGraph.prototype = {

return result;
}
},
/**
* get an array of nodes that have no dependants (i.e. nothing depends on them).
*/
entryNodes: function () {
var self = this;
return Object.keys(this.nodes).filter(function (node) {
return self.incomingEdges[node].length === 0;
});
}
};

Expand Down
5 changes: 5 additions & 0 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ declare module 'dependency-graph' {
*/
dependentsOf(name: string, leavesOnly?: boolean): string[];

/**
* Get an array of nodes that have no dependants (i.e. nothing depends on them).
*/
entryNodes(): string[];

/**
* Construct the overall processing order for the dependency graph. If leavesOnly is true, only nodes that do not depend on any other nodes will be returned.
* @param {boolean} leavesOnly
Expand Down
13 changes: 13 additions & 0 deletions specs/dep_graph_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,19 @@ describe("DepGraph", function () {
expect(graph.dependenciesOf("a")).toEqual(["b", "c"]);
});

it("should find entry nodes", function () {
var graph = new DepGraph();

graph.addNode("a");
graph.addNode("b");
graph.addNode("c");

graph.addDependency("a", "b");
graph.addDependency("a", "c");

expect(graph.entryNodes()).toEqual(["a"]);
});

it("should throw an error if a node does not exist and a dependency is added", function () {
var graph = new DepGraph();

Expand Down

0 comments on commit a91af2f

Please sign in to comment.