diff --git a/README.md b/README.md index 1a4f01f..763aaf8 100644 --- a/README.md +++ b/README.md @@ -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). @@ -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'); diff --git a/lib/dep_graph.js b/lib/dep_graph.js index af2b9f8..4de1355 100644 --- a/lib/dep_graph.js +++ b/lib/dep_graph.js @@ -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; + }); } }; diff --git a/lib/index.d.ts b/lib/index.d.ts index a0a5c74..ef158b2 100644 --- a/lib/index.d.ts +++ b/lib/index.d.ts @@ -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 diff --git a/specs/dep_graph_spec.js b/specs/dep_graph_spec.js index 00f735c..07aa68e 100644 --- a/specs/dep_graph_spec.js +++ b/specs/dep_graph_spec.js @@ -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();