1 | # Dependency Graph
|
---|
2 |
|
---|
3 | Simple dependency graph
|
---|
4 |
|
---|
5 | ## Overview
|
---|
6 |
|
---|
7 | This is a simple dependency graph useful for determining the order to do a list of things that depend on certain items being done before they are.
|
---|
8 |
|
---|
9 | To use, `npm install dependency-graph` and then `require('dependency-graph').DepGraph`
|
---|
10 |
|
---|
11 | ## API
|
---|
12 |
|
---|
13 | ### DepGraph
|
---|
14 |
|
---|
15 | Nodes in the graph are just simple strings with optional data associated with them.
|
---|
16 |
|
---|
17 | - `addNode(name, data)` - add a node in the graph with optional data. If `data` is not given, `name` will be used as data
|
---|
18 | - `removeNode(name)` - remove a node from the graph
|
---|
19 | - `hasNode(name)` - check if a node exists in the graph
|
---|
20 | - `size()` - return the number of nodes in the graph
|
---|
21 | - `getNodeData(name)` - get the data associated with a node (will throw an `Error` if the node does not exist)
|
---|
22 | - `setNodeData(name, data)` - set the data for an existing node (will throw an `Error` if the node does not exist)
|
---|
23 | - `addDependency(from, to)` - add a dependency between two nodes (will throw an `Error` if one of the nodes does not exist)
|
---|
24 | - `removeDependency(from, to)` - remove a dependency between two nodes
|
---|
25 | - `clone()` - return a clone of the graph. Any data attached to the nodes will only be *shallow-copied*
|
---|
26 | - `dependenciesOf(name, leavesOnly)` - get an array containing the nodes that the specified node depends on (transitively). If `leavesOnly` is true, only nodes that do not depend on any other nodes will be returned in the array.
|
---|
27 | - `dependantsOf(name, leavesOnly)` (aliased as `dependentsOf`) - get an array containing the nodes that depend on the specified node (transitively). If `leavesOnly` is true, only nodes that do not have any dependants will be returned in the array.
|
---|
28 | - `directDependenciesOf(name)` - get an array containing the direct dependencies of the specified node
|
---|
29 | - `directDependantsOf(name)` (aliased as `directDependentsOf`) - get an array containing the nodes that directly depend on the specified node
|
---|
30 | - `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.
|
---|
31 | - `entryNodes()` - array of nodes that have no dependants (i.e. nothing depends on them).
|
---|
32 |
|
---|
33 | 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).
|
---|
34 |
|
---|
35 | ## Examples
|
---|
36 |
|
---|
37 | var DepGraph = require('dependency-graph').DepGraph;
|
---|
38 |
|
---|
39 | var graph = new DepGraph();
|
---|
40 | graph.addNode('a');
|
---|
41 | graph.addNode('b');
|
---|
42 | graph.addNode('c');
|
---|
43 |
|
---|
44 | graph.size() // 3
|
---|
45 |
|
---|
46 | graph.addDependency('a', 'b');
|
---|
47 | graph.addDependency('b', 'c');
|
---|
48 |
|
---|
49 | graph.dependenciesOf('a'); // ['c', 'b']
|
---|
50 | graph.dependenciesOf('b'); // ['c']
|
---|
51 | graph.dependantsOf('c'); // ['a', 'b']
|
---|
52 |
|
---|
53 | graph.overallOrder(); // ['c', 'b', 'a']
|
---|
54 | graph.overallOrder(true); // ['c']
|
---|
55 | graph.entryNodes(); // ['a']
|
---|
56 |
|
---|
57 | graph.addNode('d', 'data');
|
---|
58 |
|
---|
59 | graph.getNodeData('d'); // 'data'
|
---|
60 |
|
---|
61 | graph.setNodeData('d', 'newData');
|
---|
62 |
|
---|
63 | graph.getNodeData('d'); // 'newData'
|
---|
64 |
|
---|
65 | var circularGraph = new DepGraph({ circular: true });
|
---|
66 |
|
---|
67 | circularGraph.addNode('a');
|
---|
68 | circularGraph.addNode('b');
|
---|
69 | circularGraph.addNode('c');
|
---|
70 | circularGraph.addNode('d');
|
---|
71 |
|
---|
72 | circularGraph.addDependency('a', 'b');
|
---|
73 | circularGraph.addDependency('b', 'c'); // b depends on c
|
---|
74 | circularGraph.addDependency('c', 'a'); // c depends on a, which depends on b
|
---|
75 | circularGraph.addDependency('d', 'a');
|
---|
76 |
|
---|
77 | circularGraph.dependenciesOf('b'); // ['a', 'c']
|
---|
78 | circularGraph.overallOrder(); // ['c', 'b', 'a', 'd']
|
---|