View on GitHub

Dependency-graph

A simple dependency graph for Node.js

Download this project as a .zip file Download this project as a tar.gz file

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.

Dependency Cycles are detected when running dependenciesOf, dependantsOf, and overallOrder and if one is found, an error will be thrown that includes what the cycle was in the message: e.g. Dependency Cycle Found: a -> b -> c -> a.

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.addNode('d', 'data');

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

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

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