Giter VIP home page Giter VIP logo

dependency-graph's Introduction

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
  • 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
  • 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) - 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.
  • 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.

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.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'

dependency-graph's People

Contributors

jriecken avatar nponiros avatar zolaemil avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.