1.0.0 • Published 1 year ago

dependency-graph v1.0.0

Weekly downloads
2,694,948
License
MIT
Repository
github
Last release
1 year ago

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']
@oscarltz/workspace-dev-runnerdynamic-tsconfiggulp-require-order@nodesoccoop/angular-ncr-srcstemp-eleventy-tempudk-mod@oscarltz/monorepo-build@identance/sdkpostcssify-icsspostcssify2@foreman/dependantrp-json-ref-resolver@kaviar/security-bundlebrowserdeps@raysphase/wizard@prodam/prodam-types@paiondata/nextwikiopea-bootstraap@everything-registry/sub-chunk-1468talksuite-clisymbltailwind-templatesystem-compositiontest-iki-mini-appwhalifyweb-elements-iconsweb-test-runnerwowluawx2wx2anywx2swanwx2swan-proscannarrreload-server-requirerunsv@resc/graph@castrum-nubis/bulmameta-releasemodular-css-coremistermitelink-installlaravel-mix-manifest-pathshes-agentgraphql-codegen-visitor-plugin-commonjoi-transformjson-ref-resolverinvalidate-modulemongoose-plugin-seedmonotoolhotter-requirehot-module-reloadiiiinframodgraphql-codegen-pydanticgraphql-codegen-typescript-apollo-angulargraphql-codegen-typescript-react-apollo-namedgraphql-codegen-typescript-stencil-apollogulp-npmworkspacegulp-inheritancegulp-bower-srcgql-codegen-pydanticgulp-wph-eyekarma-referencejazzcssmongoose-dependent-seedmy-css-modulesifymaniajsparcel-plugin-custom-dist-structureorion-dependency-injectionopea-bootstrappolopelletier.serverpostcss-clipostcss-cli-recursivepostcss-replace-css-prefixprisma-fixturesnode-docker-composeng-search-dropdownnger-clingrx-undo-v2node-injectorpurescript-webpack-pluginnimbumodulappmultiversumreact-flow-highly-customizable-edgesreact-rect-progressbarng-packagrng-packagr-custom-externalng-packagr-rollup-postcssneural_compressor_ext_lab_customizedneural_compressor_ext_lab_customized_2northbrookocraise-ocredis-lua-dependency-managerreflow-buildregulation-parserstyletools
1.0.0

1 year ago

0.11.0

4 years ago

0.10.0

4 years ago

0.9.0

5 years ago

0.8.1

5 years ago

0.8.0

6 years ago

0.7.2

6 years ago

0.7.1

7 years ago

0.7.0

7 years ago

0.6.0

7 years ago

0.5.2

7 years ago

0.5.1

7 years ago

0.5.0

9 years ago

0.4.1

9 years ago

0.4.0

9 years ago

0.3.0

9 years ago

0.2.1

9 years ago

0.2.0

10 years ago

0.1.0

12 years ago