パッケージの詳細

dependency-graph

jriecken28mMIT1.0.0

Simple dependency graph.

dependency, graph

readme

Dependency Graph

Simple dependency graph

Overview

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.

To use, npm install dependency-graph and then require('dependency-graph').DepGraph

API

DepGraph

Nodes in the graph are just simple strings with optional data associated with them.

  • addNode(name, data) - add a node in the graph with optional data. If data is not given, name will be used as data
  • removeNode(name) - remove a node from the graph
  • hasNode(name) - check if a node exists in the graph
  • size() - return the number of nodes in the graph
  • getNodeData(name) - get the data associated with a node (will throw an Error if the node does not exist)
  • setNodeData(name, data) - set the data for an existing node (will throw an Error if the node does not exist)
  • addDependency(from, to) - add a dependency between two nodes (will throw an Error if one of the nodes does not exist)
  • removeDependency(from, to) - remove a dependency between two nodes
  • clone() - return a clone of the graph. Any data attached to the nodes will only be shallow-copied
  • 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.
  • 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.
  • 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).

Examples

var DepGraph = require('dependency-graph').DepGraph;

var graph = new DepGraph();
graph.addNode('a');
graph.addNode('b');
graph.addNode('c');

graph.size() // 3

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

graph.dependenciesOf('a'); // ['c', 'b']
graph.dependenciesOf('b'); // ['c']
graph.dependantsOf('c'); // ['a', 'b']

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

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

graph.getNodeData('d'); // 'data'

graph.setNodeData('d', 'newData');

graph.getNodeData('d'); // 'newData'

var circularGraph = new DepGraph({ circular: true });

circularGraph.addNode('a');
circularGraph.addNode('b');
circularGraph.addNode('c');
circularGraph.addNode('d');

circularGraph.addDependency('a', 'b');
circularGraph.addDependency('b', 'c'); // b depends on c
circularGraph.addDependency('c', 'a'); // c depends on a, which depends on b
circularGraph.addDependency('d', 'a');

circularGraph.dependenciesOf('b'); // ['a', 'c']
circularGraph.overallOrder(); // ['c', 'b', 'a', 'd']

更新履歴

Dependency Graph Changelog

1.0.0 (Dec 5, 2023)

  • Switched to use Map/Set rather than using raw objects as pseudo-Maps/Sets. (Fixes #46)
    • This is also the reason for the major version bump. While there are no functional changes, this library previously did not have any special requirements of the runtime. It now requires a runtime that supports Map/Set (which should be almost everything now in 2023).
  • Ensure circular property is cloned during clone - thanks andrew-healey and tintinthong!

0.11.0 (March 5, 2021)

  • Add entryNodes method that returns the nodes that nothing depends on - thanks amcdnl!

0.10.0 (January 9, 2021)

  • Add directDependenciesOf and directDependantsOf methods for retrieving direct dependency information. (Fixes #40)
  • Add aliases dependentsOf and directDependentsOf.

0.9.0 (February 10, 2020)

  • Rewrite the topological sort DFS to be more efficient (and work!) on large graphs.
    • No longer uses recursion to avoid stack overflows with large/deep graphs
    • No longer is accidentally O(N^2) (thanks willtennien for pointing this out!)

0.8.1 (December 3, 2019)

  • Ensure all nodes are included in overallOrder when cycles are allowed. (Fixes #33)

0.8.0 (December 11, 2018)

  • Add a DepGraphCycleError with cyclePath property - thanks jhugman!

0.7.2 (August 30, 2018)

  • Make constructor parameter optional in Typescript definition. (Fixes #26)

0.7.1 (June 5, 2018)

  • Fix Typescript definition to include the new constructor arguments added in 0.7.0 - thanks tbranyen!

0.7.0 (January 17, 2018)

  • Allow circular dependencies by passing in {circular: true} into the constructor - thanks tbranyen!

0.6.0 (October 22, 2017)

  • Add a size method that will return the number of nodes in the graph.
  • Add a clone method that will clone the graph. Any custom node data will only be shallow-copied. (Fixes #14)

0.5.2 (October 22, 2017)

  • Add missing parameter in TypeScript definition. (Fixes #19)

0.5.1 (October 7, 2017)

  • Now exposes Typescript type definition - thanks vangorra!

0.5.0 (April 26, 2016)

  • Add optional data parameter for the addNode method. (Fixes #12)
  • Add methods getNodeData and setNodeData to manipulate the data associated with a node name. (Fixes #12)
  • Change the hasNode method to be able to cope with falsy node data. (Fixes #12)

0.4.1 (Sept 3, 2015)

  • Check all nodes for potential cycles when calculating overall order. (Fixes #8)

0.4.0 (Aug 1, 2015)

  • Better error messages
    • When a cycle is detected, the error message will now include the cycle in it. E.g Dependency Cycle Found: a -> b -> c -> a (Fixes #7)
    • When calling addDependency if one of the nodes does not exist, the error will say which one it was (instead of saying that "one" of the two nodes did not exist and making you manually determine which one)
  • Calling overallOrder on an empty graph will no longer throw an error about a dependency cycle. It will return an empty array.

0.3.0 (July 24, 2015)

  • Fix issue where if you call addNode twice with the same name, it would clear all edges for that node. Now it will do nothing if a node with the specified name already exists. (Fixes #3)

0.2.1 (July 3, 2015)

  • Fixed removeNode leaving references in outgoingEdges and reference to non-existent var edges - thanks juhoha! (Fixes #2)

0.2.0 (May 1, 2015)

  • Removed dependency on Underscore - thanks myndzi! (Fixes #1)

0.1.0 (May 18, 2013)

  • Initial Release - extracted out of asset-smasher